Reputation: 11
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);
}
}
Upvotes: 0
Views: 2115
Reputation: 101820
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:
Use Sharp's festure to change the colour when rendering. See their demo at: 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.
Upvotes: 0
Reputation: 457
Edit the stream of svg file when you received of api, and set into imageview
By this sample:
Upvotes: 1