开发者

android add ImageView selector programmatically

开发者 https://www.devze.com 2023-03-13 04:11 出处:网络
I want to change ImageView background (in my case gradient color) using selector state program开发者_开发问答matically not by using xml file.

I want to change ImageView background (in my case gradient color) using selector state program开发者_开发问答matically not by using xml file.

I want pressed and default state to my ImageView

For creating gradient background I use this code:

 GradientDrawable gd = new GradientDrawable(
                    GradientDrawable.Orientation.BOTTOM_TOP,
                    new int[] {Color.parseColor(startColour), Color.parseColor(endColour)});

How can I implement selector for this?

Thanks


this is from my app:

public static Drawable getButtonDrawableByScreenCathegory(Context con,
        ScreenCategory screenCategory, ButtonType buttonType) {

    int normalStateResID = (int) GXConstants.NOT_ID;
    int pressedStateResID = R.drawable.button_header_pressed;

    switch (screenCategory) {
    ...
    }

    Drawable state_normal = con.getResources()
            .getDrawable(normalStateResID).mutate();

    Drawable state_pressed = con.getResources()
            .getDrawable(pressedStateResID).mutate();

    StateListDrawable drawable = new StateListDrawable();

    drawable.addState(new int[] { android.R.attr.state_pressed },
            state_pressed);
    drawable.addState(new int[] { android.R.attr.state_enabled },
            state_normal);

    return drawable;
}

To setup background for, say, button I call:

button.setBackgroundDrawable(GXUtils.getButtonDrawableByScreenCathegory(
            this, mScreenCategory, ButtonType.MENU)
0

精彩评论

暂无评论...
验证码 换一张
取 消