Android 工作室:ImageButton image/background 闪光灯

Android Studio : ImageButton image/background flash

请告诉我,有没有简单的方法来 flash/toggle Android 中 ImageButton 的背景或图像?

我主要是试图让用户直观地知道何时在 ImageButton 上执行单击操作。所以闪烁按钮会有所帮助。

我已尝试研究该主题,但没有得出任何结论。唯一想到的是实现 TimerHandler 以在两个可绘制资产之间切换。但这似乎太过分了!

感谢你们在这个 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 来加速 up/down 动画。

您可以为该图像设置一个 Backgorund,使用您想要的任何颜色作为闪光颜色,然后当用户单击按钮时,您可以在该图像的底部、上部、右侧和左侧进行填充图像环绕主图像。如果你不知道如何实现,你可以评论添加一些代码:)