android - 如何让按钮闪烁? [英] android - How can I make a button flash?

查看:28
本文介绍了android - 如何让按钮闪烁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么办法,在代码中,让按钮持续闪烁,然后按下时停止闪烁?

Is there any way, in code, to make a button flash continually and then stop flashing when pressed?

推荐答案

有几种,取决于您指的是哪种闪烁.例如,您可以使用 alpha 动画并在按钮第一次出现时启动它.当用户单击按钮时,在您的 OnClickListener 中只需执行 clearAnimation().

There are several, depending on what kind of flashing you mean. You can, for example, use alpha animation and start it as your button first appears. And when the user clicks button, in your OnClickListener just do clearAnimation().

示例:

public void onCreate(Bundle savedInstanceState) {
    final Animation animation = new AlphaAnimation(1, 0); // Change alpha from fully visible to invisible
    animation.setDuration(500); // duration - half a second
    animation.setInterpolator(new LinearInterpolator()); // do not alter animation rate
    animation.setRepeatCount(Animation.INFINITE); // Repeat animation infinitely
    animation.setRepeatMode(Animation.REVERSE); // Reverse animation at the end so the button will fade back in
    final Button btn = (Button) findViewById(R.id.your_btn);
    btn.startAnimation(animation);
    btn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(final View view) {
            view.clearAnimation();
        }
    });
}

这篇关于android - 如何让按钮闪烁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆