编程更新的Andr​​oid矢量绘制对象矢量、对象、Andr、oid

2023-09-05 01:25:28 作者:怪我心窄

我有一个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)

推荐答案

在短期:

您的不要有直接访问内部元素的VectorDrawable. AnimatedVectorDrawable只能访问内部元素。 使用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。您没有访问

纵观source code 的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。 AnimatedVectorDrawable只能访问

2. AnimatedVectorDrawable only has access

getTargetByName 只被使用的AnimatedVectorDrawable,因为我们可以通过searching对于用途的方法。

getTargetByName is only being used by AnimatedVectorDrawable, as we can find by searching for usages of the method.

3。使用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>

和使用的方法描述here.

注意

如果上面的还是不适合你的选择,你可以尝试以下方法:

If the above is still not an option for you, you can try the following:

复制整个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 to mutate the object first.