Clean way to implement gradual fading of brightness in Android?
Asked Answered
Q

2

11

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!

Quinn answered 24/8, 2011 at 12:15 Comment(9)
You want to put device to sleep ?Assignable
No, I just want to gradually change the backlight brightness.Quinn
as far as I know there's no specific Android Animation Resource that you can use that would fulfill your needs :-/ sorry.Vierra
Hmm, I'll try an ObjectAnimator. Thanks for the link.Quinn
What's wrong with the way you've implemented it? Looks good to me...Julenejulep
I don't like it because I need the CPU usage to be as low as possible. I've attempted it again using a TimerTask, I'll update the question now.Quinn
You can put a transparent layout over the whole window and animate its alpha from transparent to opaque. But it will not cover the status bar.Assignable
I don't think you quite understand the question. Besides, there's better methods of creating transparent overlays than that.Quinn
FYI, if my device has Automatic brigtness checked in Display options, then window.getAttributes().screenBrightness does nothing! This may be same on more devices.Hangeron
D
2

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.

Dance answered 15/9, 2011 at 15:47 Comment(3)
I like the way you think! The problem is that the brightness fade would be really jumpy initially, so it looks like it's lagging.Quinn
I tried it out, and it works quite well if the step size is small enough. Cheers!Quinn
@Quinn I am happy it worked for you && i was my first bounty hereDance
P
1

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.

Phylloquinone answered 15/9, 2011 at 16:1 Comment(1)
I have tried this, but I can't actually test it since I don't have a Honeycomb device. Perhaps if we had access to the source, I could implement it for other Android platforms.Quinn

© 2022 - 2024 — McMap. All rights reserved.