Java – statelistdrawable to switch color filters

I want to create a custom button for tabhost I've been trying to use the same image resource (PNG), but the color filter changes according to the state So I take this as the layout of custom buttons:

<?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 added such a tab:

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

This is the method of '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;
}

In the selected state, the image should be completely green (0x0000ff00), and in the unselected state, it should be blue (0x000000ff)

The problem is that color filters seem to be completely ignored Under no circumstances can I see the change in color

I also tried to get the same result by setting the Android: tint attribute on < ImageView / >, but obviously you can't use the pair < selector > because it throws a numberformatexception

I can't see what I've done wrong, so any help will be appreciated

Solution

OK, I've never worked on the above code, so this is the last thing I did

First, I subclass 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,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 such a tab:

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 applies to me and is compatible with Android 1.6

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>