Dynamically change SVG image color in android(在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图像颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在android中动态改变SVG图像颜色
基础教程推荐
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01