如何让 Button 在 Android 中闪烁?
How to make a Button blink in Android?
如果用户(在我的问答游戏中)选择了错误答案,正确答案的按钮应该呈绿色闪烁。
到目前为止,我是这样做的:
if(answerTrue)
for (int i = 0; i < 2000; i = i + 250) {
handler.postDelayed(rbl_blinkNormal, i);
i = i + 250;
handler.postDelayed(rbl_blinkGreen, i);
}
和可运行的:
绿色:
rbl_blinkGreen= new Runnable() {
@Override
public void run() {
btn_richtig.setBackgroundResource(R.drawable.color_green_btn);
}
};
正常:
rbl_blinkNormal= new Runnable() {
@Override
public void run() {
btn_richtig.setBackgroundResource(R.drawable.color_black_btn);
}
};
它工作正常但像这样我每 250 毫秒调用一次 postDelayed()。这会影响我的应用程序性能吗?有更好的方法吗?
将按钮颜色设置为绿色后,您就可以为按钮设置动画。我是说,
if(answerTrue){
// Set the color of the button to GREEN once.
// Next, animate its visibility with the set color - which is GREEN as follows:
Animation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(50); //You can manage the blinking time with this parameter
anim.setStartOffset(20);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(Animation.INFINITE);
button.startAnimation(anim);
}
同样,您可以为另一个按钮设置动画并在需要时停止动画。
来源: Blinking Text in android view
如果你只想让图像闪烁,这里有一个例子。
Button bt_notes = (Button) findViewById(R.id.bt_notes);
int bt_notes_blink = 0;
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
int DrawableImage[] = {R.drawable.picto_keys, R.drawable.picto_blank};
Resources res = getApplicationContext().getResources();
bt_notes.setCompoundDrawablesWithIntrinsicBounds(null, null, null, res.getDrawable(DrawableImage[bt_notes_blink]));
bt_notes_blink++;
if (bt_notes_blink == 2) { bt_notes_blink = 0; }
handler.postDelayed(this, 500);
}
});
}
}, 0);
如果用户(在我的问答游戏中)选择了错误答案,正确答案的按钮应该呈绿色闪烁。 到目前为止,我是这样做的:
if(answerTrue)
for (int i = 0; i < 2000; i = i + 250) {
handler.postDelayed(rbl_blinkNormal, i);
i = i + 250;
handler.postDelayed(rbl_blinkGreen, i);
}
和可运行的: 绿色:
rbl_blinkGreen= new Runnable() {
@Override
public void run() {
btn_richtig.setBackgroundResource(R.drawable.color_green_btn);
}
};
正常:
rbl_blinkNormal= new Runnable() {
@Override
public void run() {
btn_richtig.setBackgroundResource(R.drawable.color_black_btn);
}
};
它工作正常但像这样我每 250 毫秒调用一次 postDelayed()。这会影响我的应用程序性能吗?有更好的方法吗?
将按钮颜色设置为绿色后,您就可以为按钮设置动画。我是说,
if(answerTrue){
// Set the color of the button to GREEN once.
// Next, animate its visibility with the set color - which is GREEN as follows:
Animation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(50); //You can manage the blinking time with this parameter
anim.setStartOffset(20);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(Animation.INFINITE);
button.startAnimation(anim);
}
同样,您可以为另一个按钮设置动画并在需要时停止动画。
来源: Blinking Text in android view
如果你只想让图像闪烁,这里有一个例子。
Button bt_notes = (Button) findViewById(R.id.bt_notes);
int bt_notes_blink = 0;
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
int DrawableImage[] = {R.drawable.picto_keys, R.drawable.picto_blank};
Resources res = getApplicationContext().getResources();
bt_notes.setCompoundDrawablesWithIntrinsicBounds(null, null, null, res.getDrawable(DrawableImage[bt_notes_blink]));
bt_notes_blink++;
if (bt_notes_blink == 2) { bt_notes_blink = 0; }
handler.postDelayed(this, 500);
}
});
}
}, 0);