如何在android中以编程方式为图像视图设置色调?

本文翻译自:How to set tint for an image view programmatically in android?

Need to set tint for an image view... I am using it the following way: 需要为图像视图设置色调...我使用以下方式:

imageView.setColorFilter(R.color.blue,android.graphics.PorterDuff.Mode.MULTIPLY);

But it doesn't change... 但它没有改变......


#1楼

参考:https://stackoom.com/question/1MQdO/如何在android中以编程方式为图像视图设置色调


#2楼

You can change the tint, quite easily in code via: 您可以通过以下方式在代码中轻松更改色调:

imageView.setColorFilter(Color.argb(255, 255, 255, 255)); // White Tint //白色调

If you want color tint then 如果你想要色彩

imageView.setColorFilter(ContextCompat.getColor(context, R.color.COLOR_YOUR_COLOR), android.graphics.PorterDuff.Mode.MULTIPLY);

For Vector Drawable 对于Vector Drawable

imageView.setColorFilter(ContextCompat.getColor(context, R.color.COLOR_YOUR_COLOR), android.graphics.PorterDuff.Mode.SRC_IN);

UPDATE : 更新
@ADev has newer solution in his answer here , but his solution requires newer support library - 25.4.0 or above. @ADev在他的回答新的解决方案在这里 ,但他的解决方案需要新的支持库- 25.4.0或更高版本。


#3楼

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

#4楼

@Hardik has it right. @Hardik没错。 The other error in your code is when you reference your XML-defined color. 代码中的另一个错误是当您引用XML定义的颜色时。 You passed only the id to the setColorFilter method, when you should use the ID to locate the color resource, and pass the resource to the setColorFilter method. 您只传递了ID的setColorFilter方法,当你应该使用ID来定位的色彩资源,以及资源传递给setColorFilter方法。 Rewriting your original code below. 重写下面的原始代码。

If this line is within your activity: 如果此行在您的活动中:

imageView.setColorFilter(getResources().getColor(R.color.blue), android.graphics.PorterDuff.Mode.MULTIPLY);

Else, you need to reference your main activity: 否则,您需要参考您的主要活动:

Activity main = ...
imageView.setColorFilter(main.getResources().getColor(R.color.blue), android.graphics.PorterDuff.Mode.MULTIPLY);

Note that this is also true of the other types of resources, such as integers, bools, dimensions, etc. Except for string, for which you can directly use getString() in your Activity without the need to first call getResources() (don't ask me why). 请注意,其他类型的资源也是如此,例如整数,bool,尺寸等。除了字符串之外,您可以直接在Activity中使用getString()而无需先调用getResources() (don不要问我为什么。

Otherwise, your code looks good. 否则,您的代码看起来不错。 (Though I haven't investigated the setColorFilter method too much...) (虽然我没有过多研究过setColorFilter方法......)


#5楼

Beginning with Lollipop, there is also a tint method for BitmapDrawables that works with the new Palette class: 从Lollipop开始,还有一个适用于新Palette类的BitmapDrawables的tint方法:

public void setTintList (ColorStateList tint) public void setTintList(ColorStateList tint)

and

public void setTintMode (PorterDuff.Mode tintMode) public void setTintMode(PorterDuff.Mode tintMode)

On older versions of Android, you can now use the DrawableCompat library 在旧版本的Android上,您现在可以使用DrawableCompat


#6楼

Not exact answer but a simpler alternative: 不是确切的答案,但更简单的替代方案:

  • Place another view on top of the image 将另一个视图放在图像的顶部
  • Change the alpha value of the view however you want (programmatically) to get the desired effect. 根据需要(以编程方式)更改视图的Alpha值以获得所需的效果。

Here is a snippet for that: 这是一个代码片段:

<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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值