请告诉我,有没有一种简单的方法可以在 Android 中闪烁/切换 ImageButton
的背景或图像?
我基本上是试图让用户直观地知道何时对 ImageButton
执行单击操作。因此闪烁按钮会有帮助。
我尝试研究这个主题,但没有得出任何结果。唯一想到的是使用 Handler
实现一个 Timer
来在两个可绘制资源之间切换。但这似乎太过分了!
感谢你们对这位 Android 新手的耐心指导。
最佳答案
我建议您使用animation-list
作为ImageButton
的背景。
1) 创建flash_background.xml
文件:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/first_background" android:duration="200" />
<item android:drawable="@drawable/second_background" android:duration="200" />
</animation-list>
2)然后从代码
yourButton.setBackgroundResource(R.drawable.flash_background);
AnimationDrawable anim = (AnimationDrawable) yourButton.getBackground();
anim.start();
注意:
- 您可以使用
android:duration
来加快/减慢动画速度。
关于Android Studio : ImageButton image/background flash,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35468903/