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;
}
}
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...
Here my approach working fine
Step-1 set svg to imageView using glide (in my case it.menuItemIcon is ImageView object)
Glide.with(it.context).`as`(PictureDrawable::class.java).load(itemData.imageURL)
.addListener(object: RequestListener<PictureDrawable> {
override fun onLoadFailed(
e: GlideException?,
model: Any?,
target: Target<PictureDrawable>?,
isFirstResource: Boolean
): Boolean {
it.menuItemIcon.visibility = View.INVISIBLE
return false
}
override fun onResourceReady(
resource: PictureDrawable?,
model: Any?,
target: Target<PictureDrawable>?,
dataSource: DataSource?,
isFirstResource: Boolean
): Boolean {
it.menuItemIcon.visibility = View.VISIBLE
return false
}
})
.addListener(SvgSoftwareLayerListener()).into(it.menuItemIcon)
Step-2 Now on button click call these two methods
if(it.menuItemIcon.drawable!=null){
val bitmap = getBitMap(it.menuItemIcon)
setBitmapAndTint(bitmap,holder.itemView.context,it.menuItemIcon,itemData.isSelected)
}
Step-3 implement getBitMap() for getting Bitmap from ImageView object
private fun getBitMap(view: AppCompatImageView): Bitmap {
val returnedBitmap = Bitmap.createBitmap(view.width, view.height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(returnedBitmap)
val bgDrawable: Drawable = view.drawable
bgDrawable.draw(canvas)
view.draw(canvas)
return returnedBitmap
}
Step-4 Now implement setBitmapAndTint() to set bitmap and colorFilter(Tint) in imageView
private fun setBitmapAndTint(bitmap: Bitmap, context: Context, menuItemIcon: AppCompatImageView, selected: Boolean) {
menuItemIcon.setImageBitmap(bitmap)
menuItemIcon.setColorFilter(ContextCompat.getColor(context,R.color.ColorBlack))
}
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.