Android 使用 ShapedDrawable 将 ColorFilter 应用于 ImageView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10114420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:32:19  来源:igfitidea点击:

Applying ColorFilter to ImageView with ShapedDrawable

androiddrawableandroid-imageviewcolorfiltershapedrawable

提问by aplavin

I have an ImageViewwith android:srcset to a ShapedDrawable, namely a white circle. What I want is to colorize this ImageViewin runtime responding to some events. imgView.setColorFilterseems to be solution, but after using this (tried different parameters) the image becomes invisible (I don't see it at the screen).

我有一个ImageViewwithandroid:src设置为 a ShapedDrawable,即一个白色圆圈。我想要的是ImageView在响应某些事件的运行时为它着色。imgView.setColorFilter似乎是解决方案,但是在使用这个(尝试了不同的参数)之后,图像变得不可见(我在屏幕上看不到它)。

How to solve this? And are there better ways to have color circles?

如何解决这个问题?有没有更好的方法来制作色环?

回答by MH.

Alright, I had a quick play with this and noticed your issue of the circles disappearing. Without you describing whatexactly you tried, I assume you haven't tried setting the color filter to the Drawableitself yet? (as opposed to the ImageView, which only seems to work with BitmapDrawables).

好的,我对此进行了快速播放,并注意到您的圆圈消失的问题。没有你描述到底尝试了什么,我假设你还没有尝试将滤色器设置为它Drawable本身?(相对于ImageView,它似乎只适用于BitmapDrawables)。

The following statements work perfectly fine for an xml-declared ShapeDrawablewith white as initial color:

以下语句对于ShapeDrawable以白色作为初始颜色的 xml 声明完全正常:

ImageView redCircle = (ImageView) findViewById(R.id.circle_red_imageview);
ImageView greenCircle = (ImageView) findViewById(R.id.circle_green_imageview);
ImageView blueCircle = (ImageView) findViewById(R.id.circle_blue_imageview);

// we can create the color values in different ways:
redCircle.getDrawable().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY );
greenCircle.getDrawable().setColorFilter(0xff00ff00, PorterDuff.Mode.MULTIPLY );
blueCircle.getDrawable().setColorFilter(getResources().getColor(R.color.blue), PorterDuff.Mode.MULTIPLY );

The ShapeDrawablefor completeness: (I set the size on the ImageView, see below)

ShapeDrawable物品是否完整:(我设置的大小ImageView,见下文)

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
    <solid android:color="@android:color/white" />
</shape>

And one of the ImageViews as example:

ImageViews之一为例:

<ImageView
    android:id="@+id/circle_red_imageview"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:padding="5dp"
    android:src="@drawable/circle_white" />

Visual result:

视觉结果:

Screenshot of colored circles

彩色圆圈的屏幕截图

回答by Vishwas Vaishnav

If you want to change Image Color use

如果你想改变图像颜色使用

PorterDuff.Mode.SRC_ATOP instead
PorterDuff.Mode.MULTIPLY

in above example.

在上面的例子中。

回答by Volodymyr Yatsykiv

You can use attribute android:tintin ImageView in xml.

您可以android:tint在 xml 中的 ImageView 中使用属性。

Example:

例子:

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/your_drawable"
    android:tint="@color/your_color" />

Tested on Android 4.1.2 and 6.0.1

在 Android 4.1.2 和 6.0.1 上测试

回答by Vansuita Jr.

You can do it very very simple using this library: https://github.com/jrvansuita/IconHandler

您可以使用此库非常简单地完成此操作:https: //github.com/jrvansuita/IconHandler

It will working like this:

它将像这样工作:

Icon.on(yourImageView).color(R.color.your_color).icon(R.mipmap.your_icon).put();