为指定的View设置透明度效果

希望为View实现的效果是,按下之后显示半透明效果。呈现disable的效果。

 

代码片段如下:

   /**

     * 为按钮点击设置透明化效果。
     * 
     * @param p_view
     *            指定的按钮。
     * @param p_enable
     *            表示按钮是否可点击,true为可点击,false为不可点击。
     */
    private void _setInfoImageViewAlpha(ImageView p_view, Boolean p_enable)
    {
        p_view.setEnabled(p_enable);

        int m_disableColor = Color.argb(150, 255, 75, 75);
        Drawable bkDrawable = p_view.getBackground().mutate();

        if (null != bkDrawable)
        {
            if (p_enable)
            {
                bkDrawable.clearColorFilter();
            }
            else
            {
                bkDrawable.setColorFilter(m_disableColor, PorterDuff.Mode.DST_OUT);
            }

        }

        p_view.setBackgroundDrawable(bkDrawable);
    }


说明:
m_disableColor = Color.argb(168, 255, 75, 75);这就是BUTTON不可能时盖在背景上面的颜色。
如果不可用,enable == false时,就给背景drawable设置color filter,如果enable == true时,就把color filter清了,调用clearColorFilter方法。
请注意第一句话中的方法mutate(),Drawable::muteate方法的说明是:
Make this drawable mutable. This operation cannot be reversed. A mutable drawable is guaranteed to not share its state with any other drawable. This is especially useful when you need to modify properties of drawables loaded from resources. By default, all drawables instances loaded from the same resource share a common state; if you modify the state of one instance, all the other instances will receive the same modification. Calling this method on a mutable Drawable will have no effect.
翻译过来就是:
使这个drawable变得状态不定。这个操作不能还原(变为不定后就不能变为原来的状态)。一个状态不定的drawable可以保证它不与其他任何一个drawabe共享它的状态。这对于你需要更改从同一资源加载来的drawable的属性时非常有用。默认情况下,所有的从同一资源(R.drawable.XXX)加载来的drawable实例都共享一个共用的状态,如果你更改一个实例的状态,其他所有的实例都会收到相同的通知。这个方法对于已经是mutable的drawable没有效果。

根据上面的说明,drawable如果是从同一个资源加载来的,这些drawable虽说不是同一实例,但他们共享同一状态,setColorFilter其实就是在改它们的状态,我测试用的例子中的ImageButton,它们的背景资源是同一个,那么这两个ImageButton中的背景drawable就共享同一状态。

换成大白话说就是:如果没有mutable,就会连带其他的drawable一起被修改透明度。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值