我尝试将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);
答案 0 :(得分:2)
如果您使用的是图标,这可能很有用:
android:tint="@color/colorAccent"
否则你可以尝试修改类:
ImageView imageViewIcon = (ImageView) listItem.findViewById(R.id.imageViewIcon);
imageViewIcon.setColorFilter(getContext().getResources().getColor(R.color.blue));
此主题中的更多信息是否可以在Android中更改xml中的材质设计图标颜色? Material design icon colour from xml
答案 1 :(得分:1)
您可以使用android.support.v7.widget.AppCompatImageView
替换ImageView
并使用
DrawableCompat.setTint(imvageView.getDrawable(), getResources().getColor(R.color.yourcolor));
答案 2 :(得分:0)
您可以设置imageview的图层绘制。这是我发现可行的唯一方法!
val paint = Paint()
val colorFilter = PorterDuffColorFilter(Color.BLUE, PorterDuff.Mode.SRC_ATOP)
paint.colorFilter = colorFilter
imageView.setLayerPaint(paint)
此代码在kotlin中,只需添加几个 new 关键字即可使其在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和voila即可!
imageView.paintColor("#FFC4C4C4")
答案 3 :(得分:0)
有关如何通过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
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);
}
}