Android - 单击时淡入淡出图像按钮

发布于 2024-12-18 03:05:55 字数 103 浏览 1 评论 0原文

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

I have an image button in a layout and implemented the onclick functionality for the same. Now, i want to fade the image button during "on click". How to do fading the image programatically in android. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

错々过的事 2024-12-25 03:05:55

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

You need to implement an animation on a button. You must have a View animation with a alpha element. Read this : http://developer.android.com/guide/topics/resources/animation-resource.html and if you need any help please write here...

仙女 2024-12-25 03:05:55

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

Like this (I havent tested it, but should work), but you need to have "faded" version of 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);
自此以后,行同陌路 2024-12-25 03:05:55

您可以按照以下方式进行操作:

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

This way is how you do it:

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;
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文