Android的附加ImageView的选择编程 [英] android add ImageView selector programmatically

查看:147
本文介绍了Android的附加ImageView的选择编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用选择的状态编程不使用XML文件来改变ImageView的背景(在我的情况渐变色)。

I want to change ImageView background (in my case gradient color) using selector state programmatically not by using xml file.

我想pressed和默认状态,以我的ImageView

I want pressed and default state to my ImageView

有关创建渐变背景我用这个code:

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?

感谢

推荐答案

这是从我的应用程序:

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)

这篇关于Android的附加ImageView的选择编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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