【发布时间】:2014-12-24 22:31:12
【问题描述】:
我知道使用第三方库,可以在 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文件中动态改变颜色。
【问题讨论】:
-
确保此颜色
0xFF9FBF3B存在于您的 svg 中(只需在文本编辑器中打开它并搜索此值)。请记住,颜色更改仅在加载文件时发生,而不是动态发生。因此,只需重新加载文件即可应用颜色更改。
标签: android svg imageview android-view svg-filters