Status Update
Comments
vi...@google.com <vi...@google.com> #2
We have shared this with our product and engineering team and will update this issue with more information as it becomes available.
lb...@gmail.com <lb...@gmail.com> #3
It should also allow to find them by name (and get multiple results if there are multiple with the same name).
lb...@gmail.com <lb...@gmail.com> #4
val drawable =
androidx.vectordrawable.graphics.drawable.VectorDrawableCompat.create(resources, R.drawable.boy, null)!!.mutate()
val vectorStateField = drawable.javaClass.getDeclaredField("mVectorState")
vectorStateField.isAccessible = true
val vectorState = vectorStateField.get(drawable)!!
val vPathRendererField = vectorState.javaClass.getDeclaredField("mVPathRenderer")
vPathRendererField.isAccessible = true
val vPathRenderer = vPathRendererField.get(vectorState)!!
val rootGroupField = vPathRenderer.javaClass.getDeclaredField("mRootGroup")
rootGroupField.isAccessible = true
val rootGroup = rootGroupField.get(vPathRenderer)
//can't go further, as rootGroup seems not to have any children...
lb...@gmail.com <lb...@gmail.com> #5
val drawable = resources.getDrawable(R.drawable.boy, null) as VectorDrawable
val vectorState = drawable.constantState!!
val rootGroupField = vectorState.javaClass.getDeclaredField("mRootGroup")
This will cause an exception...
lb...@gmail.com <lb...@gmail.com> #6
el...@gmail.com <el...@gmail.com> #7
vi...@google.com <vi...@google.com> #8
There are no plans to add more features to VectorDrawable or AnimatedVectorDrawable at this time.
However, there are already mechanisms to do some of what you are looking for:
- Compose Jetpack Compose parses VD files directly, caching the properties of those files in publicly accessible properties. For example, this code (inserted into a noop app created with the "Basic Compose Activity" template) stores the Compose structure for the VectorDrawable in the imageVector variable, which can be queried for the various elements in that VD, including the root group in which all of the nested path data is stored (in PathNode objects, which expose the path data in a way that the standard Path API in Android does not).
val imageVector = ImageVector.vectorResource(id = R.drawable.ic_launcher_foreground)
val vectorGroup = imageVector.root
Image(imageVector,
contentDescription = "launcher icon vector drawable",
modifier = Modifier.background(Color.Green)
)
- VectorDrawableCompat If you specifically create a compat drawable, you can also get the data that it stores when it parses the xml file:
val vdCompat = VectorDrawableCompat.create(resources, R.drawable.ic_launcher_foreground, null)
For example, you can get the underlying Path data for the object from vdCompat. Path objects are opaque, however there is a new AndroidX library (:graphics:graphics-path) which allows you to query the path data.
lb...@gmail.com <lb...@gmail.com> #9
@8
-
I don't see where you set here anything to a specific part of the vector-drawable, not based on ID, and not based on going in the hierarchy manually. Also how can I use it in the normal View framework?
-
Where are the docs, sample, tutorial about it? How can I do what I've described here using this? I can only find the classes pages:
Description
Changing colors could be useful for dynamic theming that are not supported by Android, for example.
Examples of things that could be nice to be able to do:
1. Find path/group by name, including multiple of them and being able to iterate over them all. Could also be nice to find using wildcards (meaning: "layer*" and not specifically "layer_test", "layer_world",...).
2. Get/Set tint color for what we've found
2. Get&Set all properties of what we've reached: fillColor, fillAlpha, strokeAlpha,strokeWidth ...
There are some third party libraries that do it, but they are very old and I'm not sure how long they work, and even if they can work today...
There is also Lottie animation, but converting from SVG in its website seems to remove the names of the layers inside the SVG.