androidsvg
androidsvg copied to clipboard
Make map of id -> SvgElem public (or protected at least?)
I'm working on an app where I not only have to display a complex svg (for which this library is great!), but also parse the svg elements to do touch detection on individual shapes later..
I looked through the source files and it seems like this library is already internally doing what I would need to do for the parsing. Does it not make sense to somehow expose idToElementMap from SVG so that this use case could also be met. If not through a getter, then perhaps make it protected so that someone could extend SVG to gain access to it?
Happy to submit a PR!
If we did that, it would make most sense to expose SVG.getElementById() I think. idToElementMap only contains those elements that have already been looked up with this function. I presume you would also need me to make all of the element classes like SVG.Path public as well?
How were you planning to do the touch-to-element detection? And why would you need this map for that?
I wasn't planning to get this in the 1.3 release (it's been delayed enough already :), But I suppose I could consider adding it. The way I was planning to implement this was to return the id attribute of the touched element (if it has one).
See also: issue #95
You're right -- idToElementMap doesn't get populated unless you do something like renderViewToCanvas. What I ended up doing was exposing rootElement, and all necessary internal classes like Svg, Group, Rect, Polygon. I believe exposing getElementById and other internal classes would have the same end result.
My svg only contains polygon elements which made my life a little easier in terms of the touch detection. Gaining access to rootElement helped me gain access to the all children polygons. Then, for each polygon, I created a corresponding Java Polygon object using https://github.com/mondain/android-components/blob/master/src/org/gregoire/android/graphics/Polygon.java
Finally, when the user taps, I call Polygon.contains() with the x and y of the tap for all the polygons until I find one.
+1
How about using the layer idea this way?
- splitting the
SVGelement into bits - rendering each bit as an independent
SVGImageView - reassembling in a
ViewGroup
I guess this will allow to intercept touches on each part and will be responsive for transforms as any other view.
Will this work?