2011-06-14 6 views
2

xmlファイルを使用せずにプログラムでセレクタステートを使用して、ImageViewの背景(私の場合はグラデーションカラー)を変更したいと思います。アンドロイドにプログラムで画像ビューアセレクタを追加する

私はこのコードを使用してグラデーション背景を作成するための私のImageViewの

に押圧し、デフォルトの状態にしたい:

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

どのように私はこのためにセレクタを実装することができますか?

おかげ

答えて

5

これは私のアプリからである:

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

たとえば、セットアップ用の背景には、ボタンは私が呼ん:

button.setBackgroundDrawable(GXUtils.getButtonDrawableByScreenCathegory(
      this, mScreenCategory, ButtonType.MENU) 
関連する問題