android - 如何使按钮闪烁?

发布于 2024-10-15 17:48:23 字数 39 浏览 5 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

茶底世界 2024-10-22 17:48:23

有几种,具体取决于您所指的闪烁类型。
例如,您可以使用 alpha 动画并在按钮第一次出现时启动它。当用户单击按钮时,在 OnClickListener 中只需执行 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();
        }
    });
}

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().

Example:

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();
        }
    });
}
温折酒 2024-10-22 17:48:23

您可以使用此代码,也可以通过mAnimation.setDuration(200);来决定按钮的闪烁时间。代码如下。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    select=(Button)findViewById(R.id.bSelect);
    Animation mAnimation = new AlphaAnimation(1, 0);
    mAnimation.setDuration(200);
    mAnimation.setInterpolator(new LinearInterpolator());
    mAnimation.setRepeatCount(Animation.INFINITE);
    mAnimation.setRepeatMode(Animation.REVERSE); 
    select.startAnimation(mAnimation);
    select.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            v.clearAnimation();


        }
    });

}

You can use this code and as well as you can also decide the blink timing of button via mAnimation.setDuration(200);.The code is as follows.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    select=(Button)findViewById(R.id.bSelect);
    Animation mAnimation = new AlphaAnimation(1, 0);
    mAnimation.setDuration(200);
    mAnimation.setInterpolator(new LinearInterpolator());
    mAnimation.setRepeatCount(Animation.INFINITE);
    mAnimation.setRepeatMode(Animation.REVERSE); 
    select.startAnimation(mAnimation);
    select.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            v.clearAnimation();


        }
    });

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文