在Android中实现亮度逐渐减弱的干净方法?

发布于 2024-12-01 07:39:37 字数 1900 浏览 2 评论 0原文

目前我有淡入淡出亮度调整的代码,看起来像这样:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦旅人picnic 2024-12-08 07:39:37

在每次迭代中将亮度降低一半怎么样?

然后循环将在 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.

幸福%小乖 2024-12-08 07:39:37

在 Honeycomb 中,您可以使用属性动画来完成此操作。这篇 Android 开发者博客上的帖子详细讨论了这一切< /a>.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文