2011-06-14 7 views
2

xml 파일을 사용하지 않고 프로그래밍 방식으로 선택기 상태를 사용하여 ImageView 백그라운드 (내 경우 그래디언트 색상)를 변경하고 싶습니다.android add ImageView selector 프로그래밍 방식으로

나는이 코드를 사용 프레스 원하는 기본 상태 그라데이션 배경을 만들어 내 이미지 뷰

에 :

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) 
관련 문제