使用Glide 4.0.0-RC0成功加载SVG文件后,我想为ImageView设置色调颜色,但是setColorFilter NOT WORKING
我的来源:
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);
}
}
在我的来源:
我将PNG图像(从res / drawable)加载到imageViewPNG和setColorFilter forimageViewPNG⇒WORKING
我将SVG图像(从res / raw)加载到imageViewSVG和setColorFilter forimageViewSVG⇒NOTINGING(已成功加载SVG文件但不能设置setColorFilter)
请帮我设置imageViewSVG的色调。
我尝试编辑下面的代码来制作一个生成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;
}
}
答案 0 :(得分:1)
Glide SVG加载程序生成PictureDrawable
。最后我查了一下,PictureDrawables不支持setColorFilter()
方法。
您可以尝试制作自己的SvgDrawableTranscoder
版本,而不是生成BitmapDrawable
,并使用AndroidSVG的renderToCanvas()
方法进行绘制。
答案 1 :(得分:0)
这里我的方法工作正常
Step-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 现在点击按钮调用这两个方法
if(it.menuItemIcon.drawable!=null){
val bitmap = getBitMap(it.menuItemIcon)
setBitmapAndTint(bitmap,holder.itemView.context,it.menuItemIcon,itemData.isSelected)
}
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 现在实现 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))
}