Sunday, 15 September 2013

Dynamically change background color with animated transition

Dynamically change background color with animated transition

I'm trying to generate a random color and set it as the background at the
rate of 1 second. I have created a thread that will handle this change,
now I would like to add a transition between the color changes to make it
blend well.
As a reference, take a look at this app.
My current code is as follows:
new Thread() {
public void run() {
while(true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Main.this.runOnUiThread(new Runnable() {
public void run() {
final LinearLayout mainView = (LinearLayout)
findViewById(R.id.mainLayout);
int red = (int)(Math.random() * 128 + 127);
int green = (int)(Math.random() * 128 + 127);
int blue = (int)(Math.random() * 128 + 127);
int color = 0xff << 24 | (red << 16) |
(green << 8) | blue;
mainView.setBackgroundColor(color);
}
});
}
}
}.start();
}
I've looked into using TransitionDrawable, however I cannot manage to
implement this dynamically. Or perhaps there's an entirely different route
as to getting this color effect- anybody have any ideas?

No comments:

Post a Comment