我创建了一个按钮,在被触摸时淡入,在未触摸时淡出。我通过使用 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/