[英]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我跟着这个样本: 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使用 Glide 4.0.0-RC0 成功加载 SVG 文件后,我想为 ImageView 设置色调颜色,但 setColorFilter 不起作用
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我将 PNG 图像(从 res/drawable)加载到 imageViewPNG 中,并为 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)我将 SVG 图像(从 res/raw)加载到 imageViewSVG 和 setColorFilter for imageViewSVG ⇒ NOT WORKING(成功加载 SVG 文件但无法 setColorFilter)
Please help me set tint color for imageViewSVG.请帮我设置 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?我尝试像下面的代码一样进行编辑以制作生成 BitmapDrawable 的 SvgDrawableTranscoder(然后将 setColorFilter ),但它不起作用,帮帮我吗?
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
. Glide SVG 加载器生成一个
PictureDrawable
。 Last I checked, PictureDrawables do not support the setColorFilter()
method.最后我检查,PictureDrawables 不支持
setColorFilter()
方法。
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.您可以尝试制作您自己的
SvgDrawableTranscoder
版本,该版本生成BitmapDrawable
并使用 AndroidSVG 的renderToCanvas()
方法绘制到该版本中。
Update更新
AndroidSVG 1.4+ now supports passing extra CSS at render time. AndroidSVG 1.4+ 现在支持在渲染时传递额外的 CSS。 Create a
RenderOptions
object and supply some CSS rules using .css()
.创建一个
RenderOptions
对象并使用.css()
提供一些 CSS 规则。 You can then pass that RenderOptions
object to renderToPicture()
in your SvgDrawableTranscoder
.然后,您可以传递
RenderOptions
反对renderToPicture()
在你的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)步骤 1使用 glide 将 svg 设置为 imageView(在我的情况下 it.menuItemIcon 是 ImageView 对象)
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 Step-2现在点击按钮调用这两个方法
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 Step-3实现 getBitMap() 从 ImageView 对象获取 Bitmap
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 Step-4现在实现 setBitmapAndTint() 在 imageView 中设置位图和 colorFilter(Tint)
private fun setBitmapAndTint(bitmap: Bitmap, context: Context, menuItemIcon: AppCompatImageView, selected: Boolean) {
menuItemIcon.setImageBitmap(bitmap)
menuItemIcon.setColorFilter(ContextCompat.getColor(context,R.color.ColorBlack))
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.