43

有什么办法,在代码中,使按钮不断闪烁,然后在按下时停止闪烁?

4

2 回答 2

124

有几种,取决于您的意思是哪种闪烁。例如,您可以使用 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();
        }
    });
}
于 2011-01-31T15:11:47.763 回答
12

您可以使用此代码,也可以通过 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();


        }
    });

}
于 2012-12-10T05:20:34.130 回答