Reputation: 151
I tried to load PNG image form drawable into ImageView, and set tint color for this ImageView with below code ⇒ it's working:
imageView1.setImageResource(R.drawable.pngFile);
imageView1.setColorFilter(getResources().getColor(R.color.colorAccent), android.graphics.PorterDuff.Mode.MULTIPLY);
I want to load SVG image into ImageView using Glide and set tint color for it. But after successfully loading SVG image into imageView, setColorFilter NOT WORKING.
I could load SVG image into another ImageView using Glide with the below code:
// Setup RequestBuilder
requestBuilder = Glide.with(mActivity)
.using(Glide.buildStreamModelLoader(Uri.class, mActivity),
InputStream.class)
.from(Uri.class)
.as(SVG.class)
.transcode(new SvgDrawableTranscoder(), PictureDrawable.class)
.sourceEncoder(new StreamEncoder())
.cacheDecoder(new FileToStreamDecoder<SVG>(new SvgDecoder()))
.decoder(new SvgDecoder())
.placeholder(R.drawable.ic_facebook)
.error(R.drawable.ic_web)
.animate(android.R.anim.fade_in)
.listener(new SvgSoftwareLayerSetter<Uri>());
// Use RequestBuilder with uri
Uri uri = Uri.parse("http://upload.wikimedia.org/wikipedia/commons/e/e8/Svg_example3.svg");
requestBuilder
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.load(uri)
.into(imageView2);
Now, I want to change tint color of mageView2 (after successfully loading SVG image into imageView2), I tried the below code but it's not working:
imageView2.setColorFilter(getResources().getColor(R.color.colorAccent), android.graphics.PorterDuff.Mode.MULTIPLY);
Upvotes: 11
Views: 9359
Reputation: 1
To change icon color, we can use tint
<androidx.appcompat.widget.AppCompatImageView
// ...
android:tint="#9997E1" />
Upvotes: 0
Reputation: 194
The complete guide how to download SVG via Glide:
1) Include this libraries in your gradle file:
implementation "com.github.bumptech.glide:glide:$glideVersion"
kapt "com.github.bumptech.glide:compiler:$glideVersion"
implementation 'com.caverock:androidsvg-aar:1.4
2) Download files from the Glide SVG sampler.
3) Modify the SvgDrawableTranscoder class to convert PictureDrawable to BitmapDrawable, because PictureDrawable is not able to apply a color filter:
public class SvgDrawableTranscoder implements ResourceTranscoder<SVG, Drawable> {
@Nullable
@Override
public Resource<Drawable> transcode(
@NonNull Resource<SVG> toTranscode, @NonNull Options options) {
SVG svg = toTranscode.get();
Picture picture = svg.renderToPicture();
PictureDrawable drawable = new PictureDrawable(picture);
Bitmap returnedBitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
canvas.drawPicture(drawable.getPicture());
Drawable d = new BitmapDrawable(PlatformApp.Companion.getInstance().getResources(), returnedBitmap);
return new SimpleResource<>(d);
}
}
Upvotes: 7
Reputation: 151
you can set the layer paint of imageview. this is the only method that i found working !
val paint = Paint()
val colorFilter = PorterDuffColorFilter(Color.BLUE, PorterDuff.Mode.SRC_ATOP)
paint.colorFilter = colorFilter
imageView.setLayerPaint(paint)
this code is in kotlin, just add a few new keywords to make it work in java :)
if you are using kotlin, add this extension to make it easier
fun ImageView.paintColor(colorString: String) {
val paint = Paint()
val colorFilter = PorterDuffColorFilter(Color.parseColor(colorString), PorterDuff.Mode.SRC_ATOP)
paint.colorFilter = colorFilter
setLayerPaint(paint)
}
then you only need to call paintColor and voila !
imageView.paintColor("#FFC4C4C4")
Upvotes: 4
Reputation: 2954
You can use android.support.v7.widget.AppCompatImageView
replace ImageView
and using
DrawableCompat.setTint(imvageView.getDrawable(), getResources().getColor(R.color.yourcolor));
Upvotes: 2
Reputation: 1385
If you are using an icon maybe this can be usefull:
android:tint="@color/colorAccent"
else you can try modify the class:
ImageView imageViewIcon = (ImageView) listItem.findViewById(R.id.imageViewIcon);
imageViewIcon.setColorFilter(getContext().getResources().getColor(R.color.blue));
more info in this thread Is it possible to change material design icon color from xml in Android? Material design icon colour from xml
Upvotes: 3