androidsvg icon indicating copy to clipboard operation
androidsvg copied to clipboard

Make map of id -> SvgElem public (or protected at least?)

Open shaurya947 opened this issue 7 years ago • 5 comments
trafficstars

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!

shaurya947 avatar Mar 29 '18 13:03 shaurya947

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).

BigBadaboom avatar Mar 29 '18 14:03 BigBadaboom

See also: issue #95

BigBadaboom avatar Mar 29 '18 14:03 BigBadaboom

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.

shaurya947 avatar Mar 30 '18 01:03 shaurya947

+1

ShaMan123 avatar Jun 15 '19 23:06 ShaMan123

How about using the layer idea this way?

  1. splitting the SVG element into bits
  2. rendering each bit as an independent SVGImageView
  3. 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?

ShaMan123 avatar Jun 16 '19 05:06 ShaMan123