在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性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...