Android -图像淡出按钮点击



我在布局中有一个图像按钮,并实现了相同的onclick功能。现在,我想在"点击"期间淡化图像按钮。如何在android中以编程方式实现图像褪色。提前感谢。

你需要在按钮上实现动画。你必须有一个带有alpha元素的View动画。请阅读:http://developer.android.com/guide/topics/resources/animation-resource.html,如果你需要任何帮助,请写在这里…

像这样(我还没有测试过,但应该工作),但是你需要有"褪色"版本的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);

你应该这样做:

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

最新更新