With the new android support update, vector drawables get backward compatibility. I have a vector image with various paths. I want the color of the paths to change on click of a button or programmatically based on an input value. Is it possible to access the name parameter of the vector path? And then change the color.
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
You can change the color of individual path at runtime, without using reflection.
VectorMaster introduces dynamic control over vector drawables. Each and every aspect of a vector drawable can be controlled dynamically (via Java instances), using this library.
Just add the following dependency in your app's build.gradle
In your case you need a simple color change:
Vector example: your_vector.xml
XML:
Java:
From: https://github.com/harjot-oberai/VectorMaster, licensed under MIT.
You have now full control over vector drawables.
You can achieve this without a library, and combine Data Binding with Kotlin.
ButtonBinding.kt
You can directly use setCompoundDrawablesWithIntrinsicBounds or setCompoundDrawables( but you have to define the current intrinsic bound, check out this article here
my_fragment.xml
For setup click listener, you can do this following as well:
YourFragment.kt
ViewModel.kt
Define the binding adapter with integer resource instead of using @color/ or @drawable. If you prefer using @ annotation (e.g:
drawable:start="@{@drawable/id}"
) on the XML.Change the data binding like this
Also, don't forget to set up this from @aminography
Important Point in Using Android Vector Drawable When you are using an android vector drawable and want to have backward compatibility for API below 21, add the following codes to
In app level build.gradle:
In Application class:
Thanks to this answer here, @Varun here explained any other alternative, the original answer for my case here
There are several ways of doing the same stuff, but this works for both Vector Drawables as well as SVG (Local/Network).
(change R.color.headPink with color of your choice)
You can use this method to change color in lower API to change vector color in fragment
in place of getActivity you should use MainActivity.this for changing vector color in activity
Use this to change a path color in your vector drawable
Library is here: https://github.com/devsideal/VectorChildFinder
As stated by @Eyal in this post https://stackoverflow.com/a/32007436/4969047
You cannot change the color of individual path at runtime. Looking at the source code of
VectorDrawableCompat
, the only method to expose the inner element by name isgetTargetByName
which is present in inner private state classVectorDrawableCompatState
ofVectorDrawableCompat
.Since it is a package private (default) - you can't use it (unless you use reflection).