需要为图像视图设置色调…我使用它的方式如下:
imageView.setColorFilter(R.color.blue,android.graphics.PorterDuff.Mode.MULTIPLY);
但这并没有改变……
需要为图像视图设置色调…我使用它的方式如下:
imageView.setColorFilter(R.color.blue,android.graphics.PorterDuff.Mode.MULTIPLY);
但这并没有改变……
当前回答
更新: @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);
其他回答
不是确切的答案,而是一个更简单的选择:
在图像上方放置另一个视图 您可以随意更改视图的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>
简单一行
imageView.setColorFilter(activity.getResources().getColor(R.color.your_color));
从棒棒糖开始,还有一个用于BitmapDrawables的着色方法,它与新的Palette类一起工作:
(ColorStateList tint)
and
setTintMode (portterduff . setintmode)模式tintMode)
在旧版本的Android上,你现在可以使用DrawableCompat库
在棒棒糖开始,有一个方法叫做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);
这对我很有效
mImageView.setColorFilter(ContextCompat.getColor(getContext(), R.color.green_500));