大佬教程收集整理的这篇文章主要介绍了在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(); } } }); } },step);
我为计数器使用数组的原因是因为它需要在Runnable中被访问,但是我需要修改该值.这使用较少的cpu,但仍然超过我喜欢.
EDIT2:Aaa和第三次尝试.感谢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); } } });
谢谢!
那么在当前解中,循环将在O(log n)而不是O(n)中完成.
以上是大佬教程为你收集整理的在Android中实现亮度逐渐衰减的清洁方式?全部内容,希望文章能够帮你解决在Android中实现亮度逐渐衰减的清洁方式?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。