StateListDrawable切换colorfilters [英] StateListDrawable to switch colorfilters

查看:132
本文介绍了StateListDrawable切换colorfilters的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建自定义按钮在TabHost使用。我还没在试图只使用相同的图像资源(PNG),但根据不同的状态colorfilter变化。所以我做了这个位作为自定义按钮的布局:

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT机器人:layout_height =FILL_PARENT>
    < ImageView的机器人:ID =@ + ID / tab_icon
        机器人:layout_centerInParent =真正的机器人:layout_alignParentTop =真
        机器人:layout_centerHorizo​​ntal =真正的机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT/>
    < TextView的机器人:ID =@ + ID / tab_text机器人:layout_below =@ ID / tab_icon
        机器人:layout_centerHorizo​​ntal =真正的机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT/>
< / RelativeLayout的>
 

在我的活动,我添加的标签是这样的:

<$p$p><$c$c>tabHost.addTab(tabHost.newTabSpec(TAB_NAME_NEWS).setIndicator(buildTab(R.drawable.tab_icon_news, R.string.news))           .setContent(newsIntent));

这是buildTab'方法:

 私人最终静态INT [] =选择新的INT [] {android.R.attr.state_selected};
私人最终静态INT [] IDLE =新INT [] {-​​android.R.attr.state_selected};

私人查看buildTab(INT图标,诠释标签){
    LayoutInflater充气= LayoutInflater.from(本);
    查看查看= inflater.inflate(R.layout.tab_button,NULL);
    StateListDrawable绘制=新StateListDrawable();

    绘制对象选择= getResources()getDrawable(图标)。
    selected.mutate();
    selected.setBounds(0,0,selected.getIntrinsicWidth(),selected.getIntrinsicHeight());
    selected.setColorFilter(新LightingColorFilter(0xFFFFFFFF的,0x0000FF00));
    drawable.addState(评选,评选);

    可绘制闲置= getResources()getDrawable(图标)。
    idle.mutate();
    idle.setColorFilter(新LightingColorFilter(0xFFFFFFFF的,0x000000FF));
    drawable.addState(空闲,空闲);

    ((ImageView的)view.findViewById(R.id.tab_icon))setImageDrawable(绘制);
    ((TextView中)view.findViewById(R.id.tab_text))的setText(的getString(标签))。
    返回查看;
}
 

在选择的状态下,形象应该是完全绿色( 0x0000FF00 ),并在非选择状态,它应该是蓝色的( 0x000000FF )。

的问题是,所述colorfilters似乎被完全忽视。我看不出颜色在任何情况下更改。

我也试着通过设置的Andr​​oid得到了相同的结果:在&LT色调属性; ImageView的/&GT; ,但显然你不能使用引用&LT;选择&GT; 在那里,因为它抛出一个 NumberFormatException异常

我不明白我在做什么错了,所以任何帮助,将AP preciated。

解决方案

OK,我从来没有得到上述code的工作,所以这里就是我终于实现了。

首先,我子类LayerDrawable:

 公共类StateDrawable扩展LayerDrawable {

    公共StateDrawable(可绘制[]层){
        超(层);
    }

    @覆盖
    保护布尔onStateChange(INT []州){
        对于(INT状态:状态){
            如果(国家== android.R.attr.state_selected){
                super.setColorFilter(Color.argb(255,255,195,0),PorterDuff.Mode.SRC_ATOP);
            } 其他 {
                super.setColorFilter(Color.GRAY,PorterDuff.Mode.SRC_ATOP);
            }
        }
        返回super.onStateChange(州);
    }

    @覆盖
    公共布尔isStateful(){
        返回true;
    }

}
 

我改变了 buildTab()的方法如下:

 私人查看buildTab(INT图标,诠释标签){
    LayoutInflater充气= LayoutInflater.from(本);
    查看查看= inflater.inflate(R.layout.tab_button,NULL);
    ((ImageView的)view.findViewById(R.id.tab_icon))。setImageDrawable(新StateDrawable(新绘制对象[] {getResources()
          .getDrawable(图标)}));
    ((TextView中)view.findViewById(R.id.tab_text))的setText(的getString(标签))。
    返回查看;
}
 

我还添加标签是这样的:

 意图fooIntent =新意图()setClass(这一点,FooActivity.class)。
tabHost.addTab(tabHost.newTabSpec(TAB_NAME_INFO).setIndicator(buildTab(R.drawable.tab_icon_info,R.string.info))setContent(infoIntent));
 

这对我的作品,与Android 1.6兼容。

I want to create custom buttons to use in a TabHost. I haven been trying to just use the same image resource (png), but have the colorfilter change depending on the state. So I made this bit to serve as the layout for the custom button:

<?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>

In my activity, I add the tabs like this:

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

And this is the 'buildTab' method:

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, 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;
}

In the selected state, the image should be completely green (0x0000FF00), and in the non-selected state, it should be blue (0x000000FF).

The problem is that the colorfilters appear to be be completely ignored. I can not see the colors change under any circumstances.

I've also tried to get the same result by setting the android:tint property on the <ImageView/>, but apparently you cannot use a reference to a <selector> there, since it throws a NumberFormatException.

I don't see what I'm doing wrong so any help would be appreciated.

解决方案

OK, I never got the above code to work, so here's what I ended up doing.

First, I subclassed 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;
    }

}

I changed the buildTab() method to the following:

private View buildTab(int icon, int label) {
    LayoutInflater inflater = LayoutInflater.from(this);
    View view = inflater.inflate(R.layout.tab_button, 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;
}

I still add the tabs like this:

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

This works for me, compatible with android 1.6.

这篇关于StateListDrawable切换colorfilters的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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