Skip to content

Instantly share code, notes, and snippets.

@lili668668
Created January 11, 2016 03:59
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/83bbdd05b6eb8de31485 to your computer and use it in GitHub Desktop.
Save lili668668/83bbdd05b6eb8de31485 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;
Thread timer = new Thread(new Runnable() {
int count = 0;
@Override
public void run() {
while (true) {
try {
bg.setBackgroundColor(colors[count]);
count = (count + 1) % size;
Thread.sleep(milltime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
if (timer.isAlive()) {
handler.removeCallbacks(timer);
}
handler.post(timer);
}
// 第二種結果程式當掉
public static void autoChangeColors(final ImageView bg, int time, final int[] colors) {
final int size = colors.length;
final long milltime = time * 1000;
bg.post(new Runnable() {
int count = 0;
@Override
public void run() {
while (true) {
try {
bg.setBackgroundColor(colors[count]);
count = (count + 1) % size;
Thread.sleep(milltime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
}
// 第三種結果閃退
public static void autoChangeColors(final ImageView bg, int time, final int[] colors) {
final int size = colors.length;
final long milltime = time * 1000;
bg.getHandler().post(new Runnable() {
int count = 0;
@Override
public void run() {
while (true) {
try {
bg.setBackgroundColor(colors[count]);
count = (count + 1) % size;
Thread.sleep(milltime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment