微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在android中动态更改SVG图像颜色

我知道使用第三方库,可以在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);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐