クリックした後、ボタンの色を徐々に変えたい。つまり、ボタンには、たとえば次の色のセットが必要です。デフォルトでは、濃い濃い青、次に濃い青、次に青、次に薄い青、そして最後に最も明るい青です。これは単なる例です。実際には、次のコードのように、ボタンの色を周期的に変更したいと考えています。しかし、なぜ中間色が表示されないのか理解できません。最初の色と最後の色のみを表示します。
これを改善するにはどうすればよいですか?
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;
}
}