i wan't to edit the fillColor from a vector-file in Android programmatically.
我不想以编程方式从Android中的矢量文件编辑fillColor。
In the xml-file i can write my color with the attribut android:fillColor but i wan't to change the color in my runtime.
在xml文件中,我可以使用attribut android:fillColor编写我的颜色,但我不想改变运行时的颜色。
Any examples for that? Thanks.
有什么例子吗?谢谢。
3 个解决方案
#1
22
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:
这正是您所需要的。致帖子的作者@emmaguy。我刚刚添加了支持库23.4+的完全支持,它使您能够在运行时停止生成png:
// Gradle Plugin 2.0+
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
And if this line is set on your Activity's or Application's onCreate:
如果在您的Activity或Application的onCreate上设置了这一行:
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
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.
您不仅可以将SVG用于srcCompat,还可以将其与其他属性(如drawViewLeft,background等)一起用于TextView,ToggleButton等。如果在选择器上使用它也有效。
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
.
注意:我修改了代码以使用VectorDrawableCompat.create而不是ResourcesCompat.getDrawable。否则它将无法工作并抛出org.xmlpull.v1.XmlPullParserException:二进制XML文件行#2:无效的可绘制标记向量。
Medium post content:
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>
Then, in the VectorDrawable, set the parts we want to dynamically change to use these attributes:
然后,在VectorDrawable中,设置我们想要动态更改的部分以使用这些属性:
<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>
Use the drawable in an ImageView:
在ImageView中使用drawable:
final ContextThemeWrapper wrapper = new ContextThemeWrapper(this, R.style.DefaultScene);
final Drawable drawable = VectorDrawableCompat.create(getResources(), R.drawable.christmas, wrapper.getTheme());
imageView.setImageDrawable(drawable);
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.
而已!当您想要更改颜色时,只需设置不同的主题,您的drawable就会更新。有关完整样本,请参阅GitHub仓库。
#2
14
If you want to change the whole color, you could apply a PorterduffColorFilter. But this does not work for a single <path>
. Only for the whole drawable.
如果要更改整个颜色,可以应用PorterduffColorFilter。但这不适用于单个
public void applyThemeToDrawable(Drawable image) {
if (image != null) {
PorterDuffColorFilter porterDuffColorFilter = new PorterDuffColorFilter(Color.BLUE,
PorterDuff.Mode.SRC_ATOP);
image.setColorFilter(porterDuffColorFilter);
}
}
VectorDrawable extends the Drawable class. See Docs
VectorDrawable扩展了Drawable类。见文档
#3
3
add setColorFilter()
method to your image content vector (is added in api level 8) like this:
将setColorFilter()方法添加到您的图像内容向量(在api级别8中添加),如下所示:
imgshare = (Imageview) findviewbyId(R.id.imageshare);
imgshare.setColorFilter(color);
#1
22
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:
这正是您所需要的。致帖子的作者@emmaguy。我刚刚添加了支持库23.4+的完全支持,它使您能够在运行时停止生成png:
// Gradle Plugin 2.0+
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
And if this line is set on your Activity's or Application's onCreate:
如果在您的Activity或Application的onCreate上设置了这一行:
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
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.
您不仅可以将SVG用于srcCompat,还可以将其与其他属性(如drawViewLeft,background等)一起用于TextView,ToggleButton等。如果在选择器上使用它也有效。
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
.
注意:我修改了代码以使用VectorDrawableCompat.create而不是ResourcesCompat.getDrawable。否则它将无法工作并抛出org.xmlpull.v1.XmlPullParserException:二进制XML文件行#2:无效的可绘制标记向量。
Medium post content:
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>
Then, in the VectorDrawable, set the parts we want to dynamically change to use these attributes:
然后,在VectorDrawable中,设置我们想要动态更改的部分以使用这些属性:
<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>
Use the drawable in an ImageView:
在ImageView中使用drawable:
final ContextThemeWrapper wrapper = new ContextThemeWrapper(this, R.style.DefaultScene);
final Drawable drawable = VectorDrawableCompat.create(getResources(), R.drawable.christmas, wrapper.getTheme());
imageView.setImageDrawable(drawable);
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.
而已!当您想要更改颜色时,只需设置不同的主题,您的drawable就会更新。有关完整样本,请参阅GitHub仓库。
#2
14
If you want to change the whole color, you could apply a PorterduffColorFilter. But this does not work for a single <path>
. Only for the whole drawable.
如果要更改整个颜色,可以应用PorterduffColorFilter。但这不适用于单个
public void applyThemeToDrawable(Drawable image) {
if (image != null) {
PorterDuffColorFilter porterDuffColorFilter = new PorterDuffColorFilter(Color.BLUE,
PorterDuff.Mode.SRC_ATOP);
image.setColorFilter(porterDuffColorFilter);
}
}
VectorDrawable extends the Drawable class. See Docs
VectorDrawable扩展了Drawable类。见文档
#3
3
add setColorFilter()
method to your image content vector (is added in api level 8) like this:
将setColorFilter()方法添加到您的图像内容向量(在api级别8中添加),如下所示:
imgshare = (Imageview) findviewbyId(R.id.imageshare);
imgshare.setColorFilter(color);