Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set tint color for ImageView loaded SVG image

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);
like image 815
Sonzero Avatar asked May 15 '17 06:05

Sonzero


3 Answers

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);
  }
}
like image 67
Vadim Shved Avatar answered Oct 16 '22 11:10

Vadim Shved


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

like image 44
Dharma Sai Seerapu Avatar answered Oct 16 '22 11:10

Dharma Sai Seerapu


You can use android.support.v7.widget.AppCompatImageView replace ImageView and using

DrawableCompat.setTint(imvageView.getDrawable(), getResources().getColor(R.color.yourcolor));
like image 2
RoShan Shan Avatar answered Oct 16 '22 11:10

RoShan Shan