在android中动态改变SVG图像颜色 [英] Dynamically change SVG image color in android

查看:35
本文介绍了在android中动态改变SVG图像颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道使用第三方库,可以在 Android 中使用 SVG 图像.类似的库:svg-android

I know that using third party library, it is possible to use SVG image in Android. Library like: svg-android

加载SVG图像的代码如下:

The code to load SVG image is like below:

 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 图像的颜色.为此,我尝试了相同项目描述中提到的以下代码.

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.

  // 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文件中动态改变颜色.

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.

推荐答案

我知道问题出在哪里.问题在于我在 svg 文件中使用的颜色代码.它不完全是 0xFF9FBF3B 而是 #9FBF3B
但是在 Java 代码中,你必须用 ARGB 值(例如 0xFF9FBF3B)来编写它.我已经更新了它,它现在工作正常.我可以使用相同的代码更改 svg 文件的颜色.

I got where is the problem. The issue is with the color code i am using in svg file. Its not exactly 0xFF9FBF3B but #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 图像的颜色时识别实际情况.

Hope this will also help others to identify the actual case while changing the color of the SVG image at runtime.

这篇关于在android中动态改变SVG图像颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆