我希望能够在点击按钮后逐渐改变按钮颜色。我的意思是,按钮必须具有以下一组颜色:默认情况下为深深蓝色,然后是深蓝色,接着是蓝色,然后是浅蓝色,最后是最浅的蓝色。这只是一个例子,实际上我想循环改变按钮颜色,就像下面的代码一样。但是,我不明白为什么它不显示中间颜色。它只显示第一种颜色和最后一种颜色。
如何改进呢?
public class ActivityExample extends Activity {
private changeColorBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_animations);
changeColorBtn = (Button) findViewById(R.id.btn_change_color);
changeColorBtn.setBackgroundColor(Color.BLACK);
changeColorBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
changeButtonColor(v);
}
});
}
private void changeButtonColor(View v) {
// How many intermediate color will be, and delay in millisecond between them
int count = 20, delay = 100;
for (int i = 0; i < count; i++) {
try {
int color = ((ColorDrawable) changeColorBtn.getBackground())
.getColor();
int blue = Color.blue(color), red = Color.red(color), green = Color.green(color);
changeColorBtn.setBackgroundColor(Color.rgb(red+10, green+5, blue+3));
Thread.sleep(delay);
} catch (InterruptedException inE) {
}
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Thread.sleep()
,这会导致你的应用程序出现延迟... - mbrenon