目前我有淡入淡出亮度调整的代码,如下所示:
new Thread() {
public void run() {
for (int i = initial; i < target; i++) {
final int bright = i;
handle.post(new Runnable() {
public void run() {
float currentBright = bright / 100f;
window.getAttributes().screenBrightness = currentBright;
window.setAttributes(window.getAttributes());
});
}
try {
sleep(step);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
我不确定这是否被认为是好的方法(我考虑过使用 ASyncTask,但在这种情况下我看不到好处)。有没有更好的方法来实现背光褪色?
编辑:我现在使用 TimerTask 如下:
new Timer().schedule(new TimerTask() {
@Override
public void run() {
final float currentBright = counter[0] / 100f;
handle.post(new Runnable() {
public void run() {
window.getAttributes().screenBrightness = currentBright;
window.setAttributes(window.getAttributes());
if (++counter[0] <= target) {
cancel();
}
}
});
}
}, 0, step);
我使用数组作为计数器的原因是因为它需要final
可在Runnable
,但我需要修改该值。这使用的 CPU 较少,但仍然超出我的喜好。
编辑2:啊啊第三次尝试。感谢 CommonsWare 的建议! (我希望我应用正确!)
handle.post(new Runnable() {
public void run() {
if (counter[0] < target) {
final float currentBright = counter[0] / 100f;
window.getAttributes().screenBrightness = currentBright;
window.setAttributes(window.getAttributes());
counter[0]++;
handle.postDelayed(this, step);
}
}
});
Thanks!
在每次迭代中将亮度降低一半怎么样?
然后循环将在 O(log n) 内完成,而不是当前解决方案中的 O(n) 。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)