ImageView的 - 绘制半透明色顶部的聚焦状态? [英] ImageView - draw translucent color on top for its focused state?

查看:199
本文介绍了ImageView的 - 绘制半透明色顶部的聚焦状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ImageView的,我想一个选择,这样当用户点击或以其他方式具有焦点,半透明的颜色画在Ima​​geView的内容之上。我不知道这是可能的选择(我已经定义好的过去用静态可绘制)。

I have an ImageView, I'd like a selector such that when the user clicks or otherwise has focus, a translucent color is drawn on top of the ImageView content. I'm not sure if this is possible with selectors (which I've been defining ok in the past with static drawables).

但基本上我有每一行中的ImageView实例的列表视图,并希望做一些事情,如:

But basically I have a listview with imageview instances in each row, and wanted to do something like:

ImageView iv = ...;
iv.setBitmapDrawable(bitmapLoadedFromInternets()); // dynamic content
iv.setClickStateOverlayColor(0x33ff0000); // ?

通常的ImageView只需要通过setBitmapDrawable()一个绘制,而是选择(1)交换了绘制了不同的点击状态,而我只想要一个颜色叠加得出,和(2)的关闭状态是动态位图,所以我不能引用从选择定义。

Usually ImageView only takes a single drawable via setBitmapDrawable(), but a selector (1) swaps out the drawable for the different click states, whereas I just want a color overlay drawn, and (2) the off state is a dynamic bitmap, so I can't reference that from a selector definition.

感谢

推荐答案

您可以做pretty的轻松,如果你让自己的的ImageView 的子类。使用的方法是<一个href="http://developer.android.com/reference/android/widget/ImageView.html#setColorFilter%28int,%20android.graphics.PorterDuff.Mode%29">setColorFilter.像这样的东西会做到这一点:

You can do that pretty easily if you make your own subclass of ImageView. The method to use is setColorFilter. Something like this will do it:

public class ImageView extends android.widget.ImageView
{
    public ImageView(Context context)
    {
        super(context);
    }

    public ImageView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public ImageView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        if(event.getAction() == MotionEvent.ACTION_DOWN && isEnabled())
            setColorFilter(0x33ff0000, PorterDuff.Mode.MULTIPLY); //your color here

        if(event.getAction() == MotionEvent.ACTION_UP)
            setColorFilter(null);

        return super.onTouchEvent(event);
    }
}

您可以使用它在你的XML布局只是参考它完全有资​​格像 my.package.ImageView ,而不是仅仅 ImageView的

You can then use it in your XML layouts just by referring to it fully qualified like my.package.ImageView instead of just ImageView.

这篇关于ImageView的 - 绘制半透明色顶部的聚焦状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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