简体   繁体   English

如何在android中更改svg图像的颜色。 图片是从 web api 下载的

[英]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);
    }
}

Edit the stream of svg file when you received of api, and set into imageview By this sample:收到api时编辑svg文件流,并设置成imageview通过这个示例: 没有编辑颜色

编辑颜色

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:tintsetColorFilter()方法在 PictureDrawables 上不起作用。

I can think of a couple of solutions:我可以想到几个解决方案:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM