Sonzero
Sonzero

Reputation: 151

Android - Glide 4.0.0-RC0: how to set tint color for ImageView after successfully loading SVG

I followed this sample: https://github.com/bumptech/glide/tree/master/samples/svg/src/main/java/com/bumptech/glide/samples/svg

After successfully loading SVG file with Glide 4.0.0-RC0, i want to set tint color for ImageView, but setColorFilter NOT WORKING

My source:

package com.example.quangson.glidesvg;

import static com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions.withCrossFade;

import android.content.ContentResolver;
import android.graphics.PorterDuff;
import android.graphics.drawable.PictureDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import com.bumptech.glide.RequestBuilder;
import com.example.quangson.glidesvg.glide.GlideApp;
import com.example.quangson.glidesvg.glide.SvgSoftwareLayerSetter;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "SVGActivity";
    private ImageView imageViewSVG;
    private ImageView imageViewPNG;
    private RequestBuilder<PictureDrawable> requestBuilder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageViewSVG = (ImageView) findViewById(R.id.svg_image_view1);
        imageViewPNG = (ImageView) findViewById(R.id.svg_image_view2);

        imageViewPNG.setImageResource(R.drawable.image_mylogo);

        requestBuilder = GlideApp.with(this)
                .as(PictureDrawable.class)
                .error(R.drawable.image_error)
                .transition(withCrossFade())
                .listener(new SvgSoftwareLayerSetter());
        Uri uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/"  + R.raw.android_toy_h);
        requestBuilder.load(uri).into(imageViewSVG);

        imageViewSVG.setColorFilter(getResources().getColor(R.color.colorAccent), PorterDuff.Mode.SRC_IN);
        imageViewPNG.setColorFilter(getResources().getColor(R.color.colorAccent), PorterDuff.Mode.SRC_IN);
    }
}

In my source:

I loaded PNG image (from res/drawable) into imageViewPNG and setColorFilter for imageViewPNG ⇒ WORKING

I loaded SVG image (from res/raw) into imageViewSVG and setColorFilter for imageViewSVG ⇒ NOT WORKING (Loaded SVG file successfully but can't setColorFilter)

Please help me set tint color for imageViewSVG.


I tried edit like below code to making SvgDrawableTranscoder that generates a BitmapDrawable (and will setColorFilter after that), but it's not working, help me?

package com.sonzero.chibiz.glide;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Picture;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.PictureDrawable;

import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.load.resource.SimpleResource;
import com.bumptech.glide.load.resource.transcode.ResourceTranscoder;
import com.caverock.androidsvg.SVG;

/**
 * Convert the {@link SVG}'s internal representation to an Android-compatible one
 * ({@link Picture}).
 */
public class SvgDrawableTranscoder implements ResourceTranscoder<SVG, BitmapDrawable> {
    @Override
    public Resource<BitmapDrawable> transcode(Resource<SVG> toTranscode) {
        SVG svg = toTranscode.get();
        Picture picture = svg.renderToPicture();
        PictureDrawable drawable = new PictureDrawable(picture);
        Bitmap bitmap = asBitmap(drawable);
        BitmapDrawable mDrawable = new BitmapDrawable(bitmap);
        return new SimpleResource<BitmapDrawable>(mDrawable);
    }

    public Bitmap asBitmap(PictureDrawable pd) {
        Bitmap bitmap = Bitmap.createBitmap(pd.getIntrinsicWidth(),pd.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawPicture(pd.getPicture());
        return bitmap;
    }
}

Upvotes: 2

Views: 4396

Answers (2)

Mahesh Kumar Prajapati
Mahesh Kumar Prajapati

Reputation: 160

Here my approach working fine

Step-1 Create method for updating imageview color

fun ImageView.paintColor(colorString: String) {
    val paint = Paint()
    val colorFilter = PorterDuffColorFilter(Color.parseColor(colorString), PorterDuff.Mode.SRC_ATOP)
    paint.colorFilter = colorFilter
    setLayerPaint(paint)
}


                      

Step-2 simple call this function where you wants to update this image color

 yourImageView.paintColor("#ff0000")

Upvotes: 0

Paul LeBeau
Paul LeBeau

Reputation: 101918

The Glide SVG loader produces a PictureDrawable. Last I checked, PictureDrawables do not support the setColorFilter() method.

What you could try is making your own version of the SvgDrawableTranscoder that generates a BitmapDrawable instead and draws into that using AndroidSVG's renderToCanvas() method.

Update

AndroidSVG 1.4+ now supports passing extra CSS at render time. Create a RenderOptions object and supply some CSS rules using .css(). You can then pass that RenderOptions object to renderToPicture() in your SvgDrawableTranscoder.

RenderOptions  renderOptions = RenderOptions.create().css("path { fill: red; }");
Picture picture = svg.renderToPicture(renderOptions);
...etc...

RenderOptions documentation

Upvotes: 4

Related Questions