android 使用动态的svg资源,在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文件中动态更改颜色.

解决方法:

例:

imageView.setColorFilter(getResources().getColor(android.R.color.black), PorterDuff.Mode.SRC_IN);

标签:svg-filters,android,svg,android-view,imageview

来源: https://codeday.me/bug/20191005/1855815.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值