在android中动态更改SVG图像颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26626640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dynamically change SVG image color in android
提问by Shreyash Mahajan
I know that using third party library, it is possible to use SVG image in Android. Library like: svg-android
我知道使用第三方库,可以在 Android 中使用 SVG 图像。库如:svg-android
The code to load SVG image is like below:
加载SVG图像的代码如下:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create a new ImageView
ImageView imageView = new ImageView(this);
// Set the background color to white
imageView.setBackgroundColor(Color.WHITE);
// Parse the SVG file from the resource
SVG svg = SVGParser.getSVGFromResource(getResources(), R.raw.android);
// Get a drawable from the parsed SVG and set it as the drawable for the ImageView
imageView.setImageDrawable(svg.createPictureDrawable());
// Set the ImageView as the content view for the Activity
setContentView(imageView);
}
It's working fine. I'm able to see the image. But now I want to change the color for the svg image at runtime. For that I tried the code below as mentioned in the same project description.
它工作正常。我能够看到图像。但是现在我想在运行时更改 svg 图像的颜色。为此,我尝试了相同项目描述中提到的以下代码。
// 0xFF9FBF3B is the hex code for the existing Android green, 0xFF1756c9 is the new blue color
SVG svg = SVGParser.getSVGFromResource(getResources(), R.raw.android, 0xFF9FBF3B, 0xFF1756c9);
But with that I am not able to see the change in the color. So I would like to know how it is possible to change the color dynamically in Java file.
但是这样我就看不到颜色的变化。所以我想知道如何在 Java 文件中动态更改颜色。
采纳答案by Shreyash Mahajan
I got where is the problem.
The issue is with the color code i am using in svg file.
Its not exactly 0xFF9FBF3Bbut #9FBF3B
But during java code you have to write it with ARGB value (e.g. 0xFF9FBF3B).
I have updated it and its work fine now. I can able to change the color of svg file with same code.
我知道问题出在哪里了。问题在于我在 svg 文件中使用的颜色代码。它不完全是0xFF9FBF3B,而是#9FBF3B
但是在 Java 代码中,您必须使用 ARGB 值(例如 0xFF9FBF3B)编写它。我已经更新了它,它现在工作正常。我可以使用相同的代码更改 svg 文件的颜色。
Hope this will also help others to identify the actual case while changing the color of the SVG image at runtime.
希望这也能帮助其他人在运行时更改 SVG 图像的颜色时识别实际情况。
回答by Antlip Dev
I know it's kind of late but I also had this issue and was able to fix this issue using the setColorFilter(int color, PorterDuff.Mode mode)method.
我知道这有点晚了,但我也遇到了这个问题,并且能够使用setColorFilter(int color, PorterDuff.Mode mode)方法解决这个问题。
Example:
例子:
imageView.setColorFilter(getResources().getColor(android.R.color.black), PorterDuff.Mode.SRC_IN);
回答by CoolMind
Using the answer of Antlip Devin Kotlin.
在Kotlin 中使用Antlip Dev的答案。
package com.example.... // Your package.
import android.graphics.PorterDuff
import android.widget.ImageView
import androidx.annotation.ColorRes
import androidx.core.content.ContextCompat
fun ImageView.setSvgColor(@ColorRes color: Int) =
setColorFilter(ContextCompat.getColor(context, color), PorterDuff.Mode.SRC_IN)
Usage:
用法:
view.your_image.setSvgColor(R.color.gray)
回答by Alessandro Mautone
what @Antlip Dev said is correct, but that method is deprecated now. This is the updated version:
@Antlip Dev 说的是正确的,但现在不推荐使用该方法。这是更新的版本:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
drawable.setColorFilter(new BlendModeColorFilter(color, BlendMode.SRC_ATOP));
} else {
drawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
}
回答by Code_Life
You can use: drawableTint to change the color
您可以使用:drawableTint 来改变颜色