我知道使用第三方库,可以在Android中使用SVG图像.
图书馆如:svg-android
加载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);
}
它工作正常.我能看到图像.但现在我想在运行时更改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);
但有了这个,我无法看到颜色的变化.所以我想知道如何在Java文件中动态更改颜色.
解决方法:
我知道这有点晚了,但我也有这个问题,并且能够使用setColorFilter(int color, PorterDuff.Mode mode)方法解决这个问题.
例:
imageView.setColorFilter(getResources().getColor(android.R.color.black), PorterDuff.Mode.SRC_IN);