Android - 单击时淡入图像按钮

时间:2011-11-25 06:51:49

标签: android android-layout imagebutton

我在布局中有一个图像按钮,并为其实现了onclick功能。现在,我希望在“点击”期间淡出图像按钮。如何在android中以编程方式淡化图像。提前致谢。

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

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

答案 2 :(得分: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;
    }
});