使用 ShapedDrawable 将 ColorFilter 应用于 ImageView [英] Applying ColorFilter to ImageView with ShapedDrawable

查看:29
本文介绍了使用 ShapedDrawable 将 ColorFilter 应用于 ImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

推荐答案

好的,我很快玩了这个,注意到你的圆圈消失的问题.如果没有您描述什么,您究竟尝试过什么,我假设您还没有尝试将颜色过滤器设置为 Drawable 本身?(相对于 ImageView,它似乎只适用于 BitmapDrawables).

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

以下语句对于以白色为初始颜色的 xml 声明的 ShapeDrawable 非常适用:

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

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

ShapeDrawable 的完整性:(我在 ImageView 上设置了大小,见下文)

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

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

ImageView 之一为例:

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

视觉效果:

这篇关于使用 ShapedDrawable 将 ColorFilter 应用于 ImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆