应用ColorFilter到的ImageView与ShapedDrawable [英] Applying ColorFilter to ImageView with ShapedDrawable

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

问题描述

我有一个的ImageView 机器人: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?

推荐答案

好吧,我有一个快速播放这点,注意到你的消失圆的问题。没有你描述的什么的正是你试过了,我想你还没有尝试设置彩色滤光片的绘制对象本身吗? (相对于的ImageView ,它似乎只与 BitmapDrawable 工作S)。

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" />

视觉效果:

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

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