动态改变SVG图像的色彩在android系统 [英] Dynamically change SVG image color in android

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

问题描述

我知道,使用第三方库,也可以在Android中使用SVG图像。
图书馆,如: SVG-的Andr​​oid

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

在code加载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图像的颜色。
对于我试图在同一项目描述中提到低于code。

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.

推荐答案

我哪里出了问题。
问题是同色系code。我正在使用SVG文件。
它不完全的 0xFF9FBF3B ,但#9FBF3B 结果
但Java code期间,您必须将其与ARGB值(例如0xFF9FBF3B)编写。
我现在已经更新了它和它的做工精细。我可以能够改变与同一code 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.

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

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