需要为图像视图设置色调…我使用它的方式如下:
imageView.setColorFilter(R.color.blue,android.graphics.PorterDuff.Mode.MULTIPLY);
但这并没有改变……
需要为图像视图设置色调…我使用它的方式如下:
imageView.setColorFilter(R.color.blue,android.graphics.PorterDuff.Mode.MULTIPLY);
但这并没有改变……
当前回答
在棒棒糖开始,有一个方法叫做imageview# setImageTintList(),你可以使用…其优点是,它需要一个ColorStateList,而不是只有一个颜色,从而使图像的色调状态感知。
在之前的lollipop设备上,你可以通过着色drawable来获得相同的行为,然后将它设置为ImageView的image drawable:
ColorStateList csl = AppCompatResources.getColorStateList(context, R.color.my_clr_selector);
Drawable drawable = DrawableCompat.wrap(imageView.getDrawable());
DrawableCompat.setTintList(drawable, csl);
imageView.setImageDrawable(drawable);
其他回答
kotlin中的一个扩展函数,用于设置和取消设置着色。
fun ImageView.setTint(@ColorRes color: Int?) {
if (color == null) {
ImageViewCompat.setImageTintList(this, null)
} else {
ImageViewCompat.setImageTintList(this, ColorStateList.valueOf(ContextCompat.getColor(context, color)))
}}
用法:yourImageView.setTint(R.color.white)用于设置和删除:
对我来说,这个代码是有效的。我使用它与卡片和图像视图,但我认为它可以在任何视图改变他们的色调颜色。 cardBookmark是我的cardView。
var cardDrawable: Drawable = binding.cardBookmark.background
cardDrawable = DrawableCompat.wrap(cardDrawable)
DrawableCompat.setTint(cardDrawable, resources.getColor(R.color.shuffleColor))
binding.cardBookmark.background = cardDrawable
从棒棒糖开始,还有一个用于BitmapDrawables的着色方法,它与新的Palette类一起工作:
(ColorStateList tint)
and
setTintMode (portterduff . setintmode)模式tintMode)
在旧版本的Android上,你现在可以使用DrawableCompat库
在我使用的java中
imageView.setImageTintList(ColorStateList.valueOf(ContextCompat.getColor(context, R.color.red)));
如果你想将选择器设置为你的色调:
ImageViewCompat.setImageTintList(iv, getResources().getColorStateList(R.color.app_icon_click_color));