[英]How to change color of svg image in android. Images are downloaded from web api
I have an issue something like, I am downloading a image from web api and successfully set into imageview.我有一个类似的问题,我正在从 web api 下载图像并成功设置为 imageview。 I am unable to change the color of svg image that I downloaded.
我无法更改我下载的 svg 图像的颜色。 I tried tintColor in xml and setFilterColor() from .java file but nothing is work for me.
我尝试了 xml 中的 tintColor 和 .java 文件中的 setFilterColor() 但对我来说没有任何作用。 Is there any solution please reply on this post.
有什么解决办法请在本帖回复。
public static void loadSvgWithColor(final Context context, String url, final int color, final ImageView target) {
if (httpClient == null) {
httpClient = new OkHttpClient.Builder()
.cache(new Cache(context.getCacheDir(), 5 * 1024 * 1014))
.build();
}
if (!url.equals("")) {
Request request = new Request.Builder().url(url).build();
httpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
target.setImageResource(R.drawable.app_icon);
target.setColorFilter(color);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
InputStream stream = response.body().byteStream();
Sharp.loadInputStream(stream).into(target);
stream.close();
target.setColorFilter(color);
}
});
} else {
target.setImageResource(R.drawable.app_icon);
target.setColorFilter(color);
}
}
The Sharp library you are using generates a PictureDrawable
and puts that in your ImageView
.您正在使用的 Sharp 库生成一个
PictureDrawable
并将其放入您的ImageView
。 Last I checked, the android:tint
and setColorFilter()
methods don't work on PictureDrawables.最后我检查,
android:tint
和setColorFilter()
方法在 PictureDrawables 上不起作用。
I can think of a couple of solutions:我可以想到几个解决方案:
Use Sharp's festure to change the colour when rendering.渲染时使用Sharp's festure改变颜色。 See their demo at: https://github.com/Pixplicity/sharp/blob/master/sample-imageview/src/main/java/com/pixplicity/sharp/imageviewdemo/SvgDemoActivity.java
查看他们的演示: https : //github.com/Pixplicity/sharp/blob/master/sample-imageview/src/main/java/com/pixplicity/sharp/imageviewdemo/SvgDemoActivity.java
Use a different library, such as my AndroidSVG , which let's you style the SVG with CSS when rendering.使用不同的库,例如我的AndroidSVG ,它可以让您在渲染时使用 CSS 设置 SVG 样式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.