需要为图像视图设置色调…我使用它的方式如下:

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);

其他回答

在我使用的java中

imageView.setImageTintList(ColorStateList.valueOf(ContextCompat.getColor(context, R.color.red)));

正如@milosmns所说,你应该使用 imageView.setColorFilter (getResouces () .getColor (R.color.blue) android.graphics.PorterDuff.Mode.MULTIPLY);

这个API需要颜色值而不是颜色资源id,这就是为什么你的语句没有工作的根本原因。

在棒棒糖开始,有一个方法叫做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);
Random random=new Random;
ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
ColorFilter cf = new PorterDuffColorFilter(Color.rgb(random.nextInt(255), random.nextInt(255), random.nextInt(255)),Mode.OVERLAY);

imageView.setImageResource(R.drawable.ic_bg_box);
imageView.setColorFilter(cf);

简单一行

imageView.setColorFilter(activity.getResources().getColor(R.color.your_color));