Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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