java - Android:按钮淡出问题

标签 java android animation button fade

我创建了一个按钮,在被触摸时淡入,在未触摸时淡出。我通过使用 java 中的 setAlpha 来实现这一点。代码及问题如下所示:

    buttonPressed.getBackground().setAlpha(0);

    buttonPressed.setOnTouchListener(new View.OnTouchListener() {

        public boolean onTouch(View view, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                buttonPressed.getBackground().setAlpha(255);
                buttonPressed.startAnimation(animationFadeIn);
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                buttonPressed.startAnimation(animationFadeOut);
                return true;
            }
            return false;
        }
    });

我遇到的问题是,每当我释放按钮时,在 animationFadeOut 能够完全播放之前,alpha 就会设置为 0,因此按钮不会淡出。

如果我删除该行:

buttonPressed.getBackground().setAlpha(0);

然后 animationFadeOut 将播放,但会立即返回到 setAlpha(255)

当用户停止触摸按钮时,如何让 animationFadeOut 充分播放并使按钮 alpha 为 0

最佳答案

我认为使用 setInterpolator() 进行淡入和淡出动画可以解决您的问题。 例如: 动画animationFadeIn = new AlphaAnimation(0, 1); AnimationFadeIn.setInterpolator(new DecelerateInterpolator()); AnimationFadeIn.setDuration(1000);

Animation animationFadeOut = new AlphaAnimation(1, 0);
animationFadeOut.setInterpolator(new AccelerateInterpolator());
animationFadeOut.setDuration(1000);

我从 this 得到了他的解决方案链接,您可以了解有关 AccelerateInterpolator here 的更多信息.

目前无法测试。但似乎很有希望。希望这有帮助!

关于java - Android:按钮淡出问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31665100/

相关文章:

java - 以编程方式导入团队项目集

java - 无法删除 jackrabbit 中的节点

html - Angular 动画 : div enter behind another div

jquery - 使用动画更改 Logo

java - 如何在Java中向一个JFrame添加2个或更多对象?

Java AES : Generate Key using a single id as source

android - 我对 drawable-hdpi 和 drawable-mdpi 感到困惑

java - 在 Android Native 应用程序中,无法创建客户端套接字。错误代码 :14 Unable to create socket: Permission denied

android - 创建查询以获取等于当前用户 ID 的值

windows - 如何在 Windows 桌面上创建暴风雪?