Open AndroidDeveloperLB opened 1 year ago
This can be done through Java reflection.
First reflect call VectorDrawable.getTargetByName or VectorDrawableCompat.getTargetByName method to get VFullPath:
Object path = ReflectUtil.invoke(vectorDrawable, "getTargetByName", new Class[]{String.class}, new Object[]{pathName});
Then Reflect calls the VFullPath.setFillColor method to set the color:
ReflectUtil.invoke(path, "setFillColor", new Class[]{int.class}, new Object[]{color});
@micstart You can't use reflection on VectorDrawable, which the VectorDrawableCompat generates. It will crash because it's blocked.
@micstart You can't use reflection on VectorDrawable, which the VectorDrawableCompat generates. It will crash because it's blocked.
You can see VectorImage also use reflection.
@micstart If you check its code, it uses reflection on the VectorDrawableCompat itself, similar to here on this library. Not on VectorDrawable. It does it nicely though.
Here it's safer, does it directly, and allows you to modify it. In any case, I've made a fork for it and published it, together with some other features: https://github.com/AndroidDeveloperLB/VectorChildFinder
Here's the proof that what you've mentioned uses reflection on VectorDrawableCompat:
private fun createDrawable(
context: Context,
@DrawableRes vectorResId: Int
): VectorDrawableCompat? {
try {
@SuppressLint("ResourceType") val parser: XmlPullParser =
context.resources.getXml(vectorResId)
val attrs = Xml.asAttributeSet(parser)
var type: Int
while (parser.next().also { type = it } != 2 && type != 1) {
}
if (type != 2) {
throw XmlPullParserException("No start tag found")
}
return VectorDrawableCompat.createFromXmlInner(context.resources, parser, attrs, null)
} catch (var6: XmlPullParserException) {
Log.e("VectorDrawableCompat", "parser error", var6)
} catch (var7: IOException) {
Log.e("VectorDrawableCompat", "parser error", var7)
}
return null
}
Hello, please check the latest code changes. Reflection has been removed and you can now continue using the library. Use the latest version https://github.com/devendroid/VectorChildFinder/releases/tag/1.0.1
@devmike01 Thank you. Can you please compare to the fork I've made, which has a few extra features, such as more finding functions, iterations (including deep iteration), and clicking ? I don't remember if I used reflection though.
Your source code is a zip file. Could you put it on a public remote repository so that I can take a look? Thanks.
@devmike01 I can't find the ZIP file. I've provided a repository, which is public: https://github.com/AndroidDeveloperLB/VectorChildFinder You can't access it?
I have seen the source code. Would take a look at it. Thanks
I tried to add this dependency, and I got this error when trying to build&run:
Any way to fix this?
VectorDrawableChangerTest.zip
https://github.com/devendroid/VectorChildFinder/files/11108955/studio64_2023-03-30_10-26-26.zip