android – 我怎样才能使button闪光?

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

有几个,取决于你是什么样的闪光你的意思。 例如,您可以使用alphaanimation,并在您的button第一次出现时启动它。 而当用户点击button,在你的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(); } }); } 

你可以使用这个代码,也可以通过mAnimation.setDuration(200)来决定button的闪烁时间 代码如下。

 @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(); } }); } 

你也可以使用帧animation