以编程方式更新android Vector Drawable [英] programmatically update android Vector Drawable

查看:68
本文介绍了以编程方式更新android Vector Drawable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由9个矩形组成的VectorDrawable.这在drawables文件夹中定义为XML.我将此设置为在xml中声明的ImageView的背景. android:src="@drawable/squares00" 我想在运行时以编程方式更改一个或多个正方形的颜色.我知道可以使用VectorDrawable动画来做到这一点.但是我想知道是否有更简单的方法来访问java中的vectorDrawable,更新其属性(为矩形设置一种或多种填充颜色),然后使用更新的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)

推荐答案

简而言之:

  1. 没有没有直接访问 VectorDrawable .
  2. AnimatedVectorDrawable 仅可访问内部元素.
  3. 使用 AnimatedVectorDrawable 来模拟所需的内容.
  1. You don't have a direct access to the inner elements in VectorDrawable.
  2. AnimatedVectorDrawable only has access to inner elements.
  3. Use AnimatedVectorDrawable to simulate what you need.

长:

1.您无权访问

查看 VectorDrawable 的源代码将显示内部元素信息存储在内部 private 状态类VectorDrawableState中.通过 name 公开内部元素的唯一方法是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 使用,我们可以通过

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

3.使用 AnimatedVectorDrawable

3. Use 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>

并使用此处所述的方法.

注意

如果上述方法仍然不适合您,则可以尝试以下操作:

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

  • 复制整个 VectorDrawable 并进行调整(未经测试)
  • getTargetByName使用反射来获取内部元素.您需要确保先mutate该对象.
  • 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.

这篇关于以编程方式更新android Vector Drawable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆