简体   繁体   中英

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. I am unable to change the color of svg image that I downloaded. I tried tintColor in xml and setFilterColor() from .java file but nothing is work for me. 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: 没有编辑颜色

编辑颜色

The Sharp library you are using generates a PictureDrawable and puts that in your ImageView . Last I checked, the android:tint and setColorFilter() methods don't work on PictureDrawables.

I can think of a couple of solutions:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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