加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – StateListDrawable来切换彩色滤镜

发布时间:2020-12-14 16:39:29 所属栏目:Java 来源:网络整理
导读:我想创建一个用于TabHost的自定义按钮.我一直在试图使用相同的图像资源(png),但是colorfilter会根据状态而改变.所以我把这一点作为自定义按钮的布局: ?xml version="1.0" encoding="utf-8"?RelativeLayout xmlns:android="http://schemas.android.com/apk/r
我想创建一个用于TabHost的自定义按钮.我一直在试图使用相同的图像资源(png),但是colorfilter会根据状态而改变.所以我把这一点作为自定义按钮的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <ImageView android:id="@+id/tab_icon"
        android:layout_centerInParent="true" android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView android:id="@+id/tab_text" android:layout_below="@id/tab_icon"
        android:layout_centerHorizontal="true" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

在我的活动中,我添加了这样的选项卡:

tabHost.addTab(tabHost.newTabSpec(TAB_NAME_NEWS).setIndicator(buildTab(R.drawable.tab_icon_news,R.string.news))
          .setContent(newsIntent));

这是’buildTab’的方法:

private final static int[] SELECTED = new int[] { android.R.attr.state_selected };
private final static int[] IDLE = new int[] { -android.R.attr.state_selected };

private View buildTab(int icon,int label) {
    LayoutInflater inflater = LayoutInflater.from(this);
    View view = inflater.inflate(R.layout.tab_button,null);
    StateListDrawable drawable = new StateListDrawable();

    Drawable selected = getResources().getDrawable(icon);
    selected.mutate();
    selected.setBounds(0,selected.getIntrinsicWidth(),selected.getIntrinsicHeight());
    selected.setColorFilter(new LightingColorFilter(0xFFFFFFFF,0x0000FF00));
    drawable.addState(SELECTED,selected);

    Drawable idle = getResources().getDrawable(icon);
    idle.mutate();
    idle.setColorFilter(new LightingColorFilter(0xFFFFFFFF,0x000000FF));
    drawable.addState(IDLE,idle);

    ((ImageView) view.findViewById(R.id.tab_icon)).setImageDrawable(drawable);
    ((TextView) view.findViewById(R.id.tab_text)).setText(getString(label));
    return view;
}

在选择的状态下,图像应为完全绿色(0x0000FF00),在未选择的状态下,应为蓝色(0x000000FF).

问题是彩色滤镜似乎被完全忽视.在任何情况下,我都看不到颜色的变化.

我也尝试通过在< ImageView />上设置android:tint属性来获得相同的结果,但显然您不能使用对< selector>因为它抛出一个NumberFormatException.

我看不到我做错了什么,所以任何帮助将不胜感激.

解决方法

好的,我从来没有上面的代码工作,所以这是我最后做的.

首先,我将LayerDrawable进行子类化:

public class StateDrawable extends LayerDrawable {

    public StateDrawable(Drawable[] layers) {
        super(layers);
    }

    @Override
    protected boolean onStateChange(int[] states) {
        for (int state : states) {
            if (state == android.R.attr.state_selected) {
                super.setColorFilter(Color.argb(255,255,195,0),PorterDuff.Mode.SRC_ATOP);
            } else {
                super.setColorFilter(Color.GRAY,PorterDuff.Mode.SRC_ATOP);
            }
        }
        return super.onStateChange(states);
    }

    @Override
    public boolean isStateful() {
        return true;
    }

}

我将buildTab()方法更改为以下内容:

private View buildTab(int icon,null);
    ((ImageView) view.findViewById(R.id.tab_icon)).setImageDrawable(new StateDrawable(new Drawable[] { getResources()
          .getDrawable(icon) }));
    ((TextView) view.findViewById(R.id.tab_text)).setText(getString(label));
    return view;
}

我仍然添加这样的选项卡:

Intent fooIntent = new Intent().setClass(this,FooActivity.class);
tabHost.addTab(tabHost.newTabSpec(TAB_NAME_INFO).setIndicator(buildTab(R.drawable.tab_icon_info,R.string.info)).setContent(infoIntent));

这适用于我,兼容android 1.6.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读