![](/img/trans.png)
[英]Android - Glide 4.0.0-RC0: how to set tint color for ImageView after successfully loading SVG
[英]How to set tint color for ImageView loaded SVG image
我尝试将可绘制的 PNG 图像表单加载到 ImageView 中,并使用以下代码为此 ImageView 设置色调颜色⇒它正在工作:
imageView1.setImageResource(R.drawable.pngFile);
imageView1.setColorFilter(getResources().getColor(R.color.colorAccent), android.graphics.PorterDuff.Mode.MULTIPLY);
我想使用 Glide 将 SVG 图像加载到 ImageView 并为其设置色调。 但是在成功将 SVG 图像加载到 imageView 后, setColorFilter NOT WORKING 。
我可以使用 Glide 和以下代码将 SVG 图像加载到另一个 ImageView 中:
// 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);
现在,我想更改 mageView2 的色调颜色(在成功将 SVG 图像加载到 imageView2 后),我尝试了以下代码但它不起作用:
imageView2.setColorFilter(getResources().getColor(R.color.colorAccent), android.graphics.PorterDuff.Mode.MULTIPLY);
如果您使用的是图标,这可能很有用:
android:tint="@color/colorAccent"
否则你可以尝试修改类:
ImageView imageViewIcon = (ImageView) listItem.findViewById(R.id.imageViewIcon);
imageViewIcon.setColorFilter(getContext().getResources().getColor(R.color.blue));
此线程中的更多信息是否可以在 Android 中从 xml 更改材料设计图标颜色? 来自 xml 的材料设计图标颜色
您可以使用android.support.v7.widget.AppCompatImageView
替换ImageView
并使用
DrawableCompat.setTint(imvageView.getDrawable(), getResources().getColor(R.color.yourcolor));
如何通过 Glide 下载 SVG 的完整指南:
1) 将此库包含在您的 gradle 文件中:
implementation "com.github.bumptech.glide:glide:$glideVersion" kapt "com.github.bumptech.glide:compiler:$glideVersion" implementation 'com.caverock:androidsvg-aar:1.4
2) 从Glide SVG 采样器下载文件。
3)修改SvgDrawableTranscoder类,将PictureDrawable转换为BitmapDrawable,因为PictureDrawable无法应用颜色滤镜:
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);
}
}
您可以设置imageview的图层绘制。 这是我发现有效的唯一方法!
val paint = Paint()
val colorFilter = PorterDuffColorFilter(Color.BLUE, PorterDuff.Mode.SRC_ATOP)
paint.colorFilter = colorFilter
imageView.setLayerPaint(paint)
此代码在 kotlin 中,只需添加一些新关键字即可使其在 java 中工作:)
如果您使用的是 kotlin,请添加此扩展以使其更容易
fun ImageView.paintColor(colorString: String) {
val paint = Paint()
val colorFilter = PorterDuffColorFilter(Color.parseColor(colorString), PorterDuff.Mode.SRC_ATOP)
paint.colorFilter = colorFilter
setLayerPaint(paint)
}
那么你只需要调用paintColor和瞧!
imageView.paintColor("#FFC4C4C4")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.