Created
January 11, 2016 03:59
-
-
Save lili668668/83bbdd05b6eb8de31485 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; | |
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