编程更新的Android矢量绘制对象 [英] programatically update android Vector Drawable
问题描述
我有一个VectorDrawable由9个矩形。这被定义为在所述可绘制文件夹的XML。我有这样的设置为背景的,我在XML中宣布了ImageView的。 机器人:SRC =@可绘制/ squares00
我想改变一个平方以上的颜色编程在运行时。我知道有办法使用VectorDrawable动画做到这一点。但我不知道是否有访问我vectorDrawable在Java中,更新它的属性(设置其中一个的填充色或更多的矩形),然后具有图像背景更新VectoDrawable更新的更简单的方法。我的目标是Android的API 21(棒棒糖)
I have a VectorDrawable consists of 9 rectangles. This is defined as an XML in the the drawables folder. I have this set as the background for an ImageView that I have declared in xml. android:src="@drawable/squares00"
I would like to change the color of one or more of the squares programatically at run time. I know there is way to do this using VectorDrawable animations. But I was wondering if there is simpler way of accessing my vectorDrawable in java, updating its properties (setting one or more of the fill colors for the rectangles) and then having the image background be updated with the updated VectoDrawable. My target is Android API 21 (lollipop)
推荐答案
在短期:
- 您的不要有直接访问内部元素的<一个href="https://developer.android.com/reference/android/graphics/drawable/VectorDrawable.html">VectorDrawable.
- <一个href="https://developer.android.com/reference/android/graphics/drawable/AnimatedVectorDrawable.html">AnimatedVectorDrawable只能访问内部元素。
- 使用<一个href="https://developer.android.com/reference/android/graphics/drawable/AnimatedVectorDrawable.html">AnimatedVectorDrawable模仿你所需要的。
- You don't have a direct access to the inner elements in VectorDrawable.
- AnimatedVectorDrawable only has access to inner elements.
- Use AnimatedVectorDrawable to simulate what you need.
龙:
1。您没有访问
纵观<一href="http://grep$c$c.com/file/repo1.maven.org/maven2/org.robolectric/android-all/5.0.0_r2-robolectric-0/android/graphics/drawable/VectorDrawable.java#VectorDrawable.VectorDrawableState">source code 的<一个href="https://developer.android.com/reference/android/graphics/drawable/VectorDrawable.html">VectorDrawable将显示该内部元素的信息存储在内部的私人状态类 VectorDrawableState
。唯一的方法,通过揭露内部元件的名称的是 getTargetByName
,但不幸的是包专用(默认) - 你不能用它(除非你使用反射)。
Looking at the source code for VectorDrawable will show that the inner elements information is stored in an inner private state class VectorDrawableState
. The only method to expose the inner element by name is getTargetByName
, but unfortunately it is package private (default) - you can't use it (unless you use reflection).
2。 <一href="https://developer.android.com/reference/android/graphics/drawable/AnimatedVectorDrawable.html">AnimatedVectorDrawable只能访问
2. AnimatedVectorDrawable only has access
getTargetByName
只被使用的<一个href="https://developer.android.com/reference/android/graphics/drawable/AnimatedVectorDrawable.html">AnimatedVectorDrawable,因为我们可以通过<一个发现href="http://grep$c$c.com/search/usages?type=method&id=repo1.maven.org%24maven2@org.robolectric%24android-all@5.0.0_r2-robolectric-0@android%24graphics%24drawable@VectorDrawable@getTargetByName%28java.lang.String%29&k=u">searching对于用途的方法。
getTargetByName
is only being used by AnimatedVectorDrawable, as we can find by searching for usages of the method.
3。使用<一个href="https://developer.android.com/reference/android/graphics/drawable/AnimatedVectorDrawable.html">AnimatedVectorDrawable
所以,现在我们看到,这是唯一可行的选择,例如,我们可以尝试以下从改变元素RECT2的白色到黑色的:
So now that we see that this is the only available option, for example, we can try the following to change the color of element "rect2" from white to black:
change_color.xml:
change_color.xml:
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:duration="0"
android:propertyName="fillColor"
android:valueFrom="@color/white"
android:valueTo="@color/black"/>
</set>
animation.xml:
animation.xml:
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/vectordrawable" >
<target
android:name="rect2"
android:animation="@anim/change_color" />
</animated-vector>
和使用的方法描述<一href="https://developer.android.com/reference/android/graphics/drawable/AnimatedVectorDrawable.html">here.
注意
如果上面的还是不适合你的选择,你可以尝试以下方法:
If the above is still not an option for you, you can try the following:
- 复制整个<一个href="https://developer.android.com/reference/android/graphics/drawable/VectorDrawable.html">VectorDrawable并调整它(未测试)
- 使用反射式
getTargetByName
获得内部元件。您需要确保突变
对象第一。
- Copy the entire VectorDrawable and tweak it (not tested)
- Use reflection for
getTargetByName
to get the inner element. You will need to make sure tomutate
the object first.
这篇关于编程更新的Android矢量绘制对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!