在Android中实现亮度逐渐减弱的干净方法?
目前我有淡入淡出亮度调整的代码,看起来像这样:
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);
}
}
});
谢谢!
At the moment I have code to fade brightness adjustments which looks something like this:
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();
I'm not sure if that's considered good methodology (I considered using ASyncTask, but I can't see the benefits in this case). Is there a better way to achieve backlight fading?
EDIT: I'm now using a TimerTask as follows:
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);
The reason I use an array for the counter is because it needs to be final
to be accessed in the Runnable
, but I need to modify the value. This uses less CPU, but still more than I like.
EDIT2: Aaaand a third attempt. Thanks to CommonsWare for the advice! (I hope I applied it correctly!)
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在每次迭代中将亮度降低一半怎么样?
然后循环将在 O(log n) 内完成,而不是当前解决方案中的 O(n) 。
how about reducing the brightness to half in each iteration.
Then loop will complete in O(log n) rather than O(n) in current solution.
在 Honeycomb 中,您可以使用属性动画来完成此操作。这篇 Android 开发者博客上的帖子详细讨论了这一切< /a>.
From Honeycomb you can do things lie this using Property Animation. This post on the Android Developers blog talks about it all in some detail.