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!

Glitch
  • 2,785
  • 1
  • 27
  • 50
  • You want to put device to sleep ? – Ron Aug 25 '11 at 16:21
  • No, I just want to gradually change the backlight brightness. – Glitch Aug 26 '11 at 06:41
  • 2
    as far as I know there's no specific [Android Animation Resource](http://developer.android.com/guide/topics/resources/animation-resource.html) that you can use that would fulfill your needs :-/ sorry. – AndrewPK Sep 08 '11 at 20:22
  • Hmm, I'll try an ObjectAnimator. Thanks for the link. – Glitch Sep 09 '11 at 04:58
  • What's wrong with the way you've implemented it? Looks good to me... – IncrediApp Sep 10 '11 at 08:09
  • 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. – Glitch Sep 10 '11 at 08:53
  • 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. – Ron Sep 12 '11 at 03:24
  • I don't think you quite understand the question. Besides, there's better methods of creating transparent overlays than that. – Glitch Sep 12 '11 at 07:56
  • FYI, if my device has Automatic brigtness checked in Display options, then window.getAttributes().screenBrightness does nothing! This may be same on more devices. – Pointer Null Sep 15 '11 at 09:10

2 Answers2

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.

MoveFast
  • 3,011
  • 2
  • 27
  • 53
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.

David Webb
  • 190,537
  • 57
  • 313
  • 299
  • 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. – Glitch Sep 16 '11 at 07:45