Created
January 11, 2016 08:41
-
-
Save lili668668/6c56258f2c2e666cb56a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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