以编程方式在Android中更改矢量的fillColor [英] Change fillColor of a vector in android programmatically
问题描述
我想以编程方式在Android中编辑矢量文件的填充颜色.
I want to edit the fill Color of a vector-file in Android programmatically.
在xml文件中,我可以使用属性设置颜色 android:fillColor ,但我想在运行时更改颜色.
In the xml-file I can set my color with the attribute android:fillColor but I want to change the color in runtime.
有什么例子吗?谢谢.
推荐答案
这正是您所需要的.感谢帖子的作者 @emmaguy .我刚刚添加了支持库23.4+ 的完整支持,这样一来,您就可以在运行时停止生成png:
This is exactly what you need. Credits to @emmaguy, the author of the post. I just added the full support of Support Library 23.4+, which enables you to stop generating pngs at runtime:
// Gradle Plugin 2.0+
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
并且如果在您的活动"或应用程序"的onCreate上设置了此行:
And if this line is set on your Activity's or Application's onCreate:
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
您不仅可以将SVG与srcCompat
一起使用,还可以与TextView,ToggleButton等中的其他属性(例如drawableLeft
,background
等)一起使用.如果在选择器上使用,它也可以工作.
You can use your SVGs not only with srcCompat
but also with other attributes such as drawableLeft
, background
, etc. in TextView, ToggleButton and so on. It also works if used on selectors.
注意:我将代码修改为使用VectorDrawableCompat.create
而不是ResourcesCompat.getDrawable
.否则它将无法正常工作并抛出org.xmlpull.v1.XmlPullParserException: Binary XML file line #2: invalid drawable tag vector
.
Note: I modified the code to use VectorDrawableCompat.create
instead of ResourcesCompat.getDrawable
. Otherwise it would not work and throw org.xmlpull.v1.XmlPullParserException: Binary XML file line #2: invalid drawable tag vector
.
首先,我们为两种摆设创建属性,因此我们可以更改其颜色:
First, we create attributes for the two kinds of bauble, so we can change their colours:
<declare-styleable name="ChristmasTree">
<attr name="bauble_round" format="color" />
<attr name="bauble_small" format="color" />
</declare-styleable>
然后,在VectorDrawable中,设置我们要动态更改的部分以使用这些属性:
Then, in the VectorDrawable, set the parts we want to dynamically change to use these attributes:
<path
android:fillColor="?attr/bauble_round"
android:pathData="...." />
<path
android:fillColor="?attr/bauble_small"
android:pathData="...." />
...
创建主题并设置您要使用的颜色:
Create themes and set the colours you want to use:
<style name="UpdatedScene" parent="DefaultScene">
<item name="bauble_round">#db486e</item>
<item name="bauble_small">#22c7f7</item>
</style>
<style name="DefaultScene">
<item name="bauble_round">#fec758</item>
<item name="bauble_small">#f22424</item>
</style>
在ImageView中使用可绘制对象:
Use the drawable in an ImageView:
final ContextThemeWrapper wrapper = new ContextThemeWrapper(this, R.style.DefaultScene);
final Drawable drawable = VectorDrawableCompat.create(getResources(), R.drawable.christmas, wrapper.getTheme());
imageView.setImageDrawable(drawable);
就是这样!当您想要更改颜色时,只需设置一个不同的主题,您的可绘制对象就会更新. 有关完整示例,请参见GitHub repo .
That’s it! When you want to change the colours, simply set a different theme and your drawable will update. See the GitHub repo for a full sample.
这篇关于以编程方式在Android中更改矢量的fillColor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!