2011-11-25 7 views
1

레이아웃에 이미지 버튼이 있으며 동일한 버튼에 onclick 기능이 구현되었습니다. 자, "클릭 할 때" 동안 이미지 버튼이 희미 해지기를 원합니다. 안드로이드에서 프로그래밍 방식으로 이미지를 페이드하는 방법. 미리 감사드립니다.Android - 페이드 이미지 버튼 클릭시

답변

1

이렇게 (필자는 테스트했지만 작동해야 함) ImageButton 드로어 블의 "색이 바랜"버전이 필요합니다.

Bitmap iconOn = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon_on);//this should be yours faded button image 
Bitmap iconOff = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon_off); //and this will be normal image 

Drawable iconOnDrawable = new BitmapDrawable(iconOn); 
Drawable iconOffDrawable = new BitmapDrawable(iconOff); 

StateListDrawable states = new StateListDrawable(); 
states.addState(new int[] { android.R.attr.state_pressed },iconOnDrawable); 
states.addState(new int[] { android.R.attr.state_focused },iconOffDrawable); 
states.addState(new int[] { android.R.attr.state_selected },iconOnDrawable); 
states.addState(new int[] {}, iconOffDrawable); 

ImageButton imageButton = (ImageButton) findViewById(R.id.button); 
imageButton.setImageDrawable(states); 
+0

+1, 감사, 난 솔루션을 가지고 .... –

0

이 방법은 당신이 그것을하는 방법이다 :

Button b = view.findViewById(R.id.button); 
    final TransitionDrawable td = new TransitionDrawable(new Drawable[]{new ColorDrawable(0xFFFF0000), new ColorDrawable(0x11FF0000)}); 
    b.setOnTouchListener(new View.OnTouchListener() { 

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     if(event.getActionMasked() == MotionEvent.ACTION_DOWN) { 
      //b.setBackgroundColor(); 
     } 
     if(event.getActionMasked() == MotionEvent.ACTION_UP) { 
      b.setBackgroundDrawable(td); 
      td.startTransition(1000); 
     } 
     return false; 
    } 
}); 
관련 문제