需要为图像视图设置色调…我使用它的方式如下:
imageView.setColorFilter(R.color.blue,android.graphics.PorterDuff.Mode.MULTIPLY);
但这并没有改变……
需要为图像视图设置色调…我使用它的方式如下:
imageView.setColorFilter(R.color.blue,android.graphics.PorterDuff.Mode.MULTIPLY);
但这并没有改变……
当前回答
不是确切的答案,而是一个更简单的选择:
在图像上方放置另一个视图 您可以随意更改视图的alpha值(以编程方式)以获得所需的效果。
以下是其中的一个片段:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="@dimen/height120"
android:contentDescription="@string/my_description"
android:scaleType="fitXY"
android:src="@drawable/my_awesome_image"/>
<View
android:layout_width="match_parent"
android:layout_height="@dimen/height120"
android:alpha="0.5"
android:background="@color/my_blue_color"/>
</FrameLayout>
其他回答
在我尝试了所有的方法后,他们都不适合我。
我通过使用另一个PortDuff.MODE来获得解决方案。
imgEstadoBillete.setColorFilter(context.getResources().getColor(R.color.green),PorterDuff.Mode.SRC_IN);
更新: @ADev在他的回答中有更新的解决方案,但他的解决方案需要更新的支持库- 25.4.0或以上。
你可以改变色调,很容易在代码中通过:
imageView.setColorFilter(颜色。Argb (255, 255, 255, 255));//白色色调
如果你想要色彩鲜艳的话
imageView.setColorFilter(ContextCompat.getColor(context, R.color.COLOR_YOUR_COLOR), android.graphics.PorterDuff.Mode.MULTIPLY);
对于可绘制的向量
imageView.setColorFilter(ContextCompat.getColor(context, R.color.COLOR_YOUR_COLOR), android.graphics.PorterDuff.Mode.SRC_IN);
对我来说,这个代码是有效的。我使用它与卡片和图像视图,但我认为它可以在任何视图改变他们的色调颜色。 cardBookmark是我的cardView。
var cardDrawable: Drawable = binding.cardBookmark.background
cardDrawable = DrawableCompat.wrap(cardDrawable)
DrawableCompat.setTint(cardDrawable, resources.getColor(R.color.shuffleColor))
binding.cardBookmark.background = cardDrawable
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));