ObjectAnimator not fading background colors in a thread
I'm changing the background color of the view every 2 seconds to a newly
generated color and trying to get color1 and color2 to fade into each
other. The ObjectAnimator isn't working quite right.
My code as follows:
public class DynamicColors extends Activity {
public int color1, color2, red1, red2, blue1, blue2, green1, green2;
ObjectAnimator anim;
View v;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.dynamiccolors);
v = findViewById(R.id.view);
// Generate color1 before starting the thread
red1 = (int)(Math.random() * 128 + 127);
green1 = (int)(Math.random() * 128 + 127);
blue1 = (int)(Math.random() * 128 + 127);
color1 = 0xff << 24 | (red1 << 16) |
(green1 << 8) | blue1;
v.setBackgroundColor(color1);
// We haven't initialized color2 yet. Will set this later
anim = ObjectAnimator.ofInt(v, "backgroundColor", color1);
anim.setEvaluator(new ArgbEvaluator());
anim.setDuration(2000);
new Thread() {
public void run() {
while(true) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
DynamicColors.this.runOnUiThread(new Runnable() {
public void run() {
//generate color 2
red2 = (int)(Math.random() * 128 + 127);
green2 = (int)(Math.random() * 128 + 127);
blue2 = (int)(Math.random() * 128 + 127);
color2 = 0xff << 24 | (red2 << 16) |
(green2 << 8) | blue2;
// Update the color values
anim.setIntValues(color1, color2);
anim.start();
// Order the colors
color1 = color2;
}
});
}
}
}.start();
}
}
Any thoughts on what could be going wrong with the ObjectAnimator? I think
the thread is really confusing me on this one.
No comments:
Post a Comment