2011-11-25 75 views

回答

1

像這樣(我還沒有測試過,但應該工作),但你需要有「淡化」版本的ImageButton drawable。

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