Skip to content

Instantly share code, notes, and snippets.

@lili668668
Created January 11, 2016 08:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lili668668/6c56258f2c2e666cb56a to your computer and use it in GitHub Desktop.
Save lili668668/6c56258f2c2e666cb56a to your computer and use it in GitHub Desktop.
public static void autoChangeColors(final ImageView bg, int time, final int[] colors) {
final int size = colors.length;
final long milltime = time * 1000;
final Handler handler = new Handler() {
int count = 0;
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
bg.setBackgroundColor(colors[count]);
count = (count + 1) % size;
break;
}
super.handleMessage(msg);
}
};
Thread timer = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
Message m = new Message();
m.what = 1;
handler.sendMessage(m);
try {
Thread.sleep(milltime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
timer.start();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment