4

I am trying to resolve following task: smooth change of brightness from 100% to 0%, but can't gain effect of smoothness. Want to emphasize that I am using following approach of brightness change. The recommended one.

WindowManager.LayoutParams lp = window.getAttributes();

lp.screenBrightness = floatPercent;

window.setAttributes(lp);

Well, it obviously works, but not smooth. I will describe logic:

I have a thread that changes brightness:

            while (isRunning()) { 
                Thread.sleep(sleepTime); 
                spentTime+=sleepTime; 
                view.post(new Runnable() { 
                    public void run() { 
                        changeBrightness(); 
                    } 
                }); 
            } 

I have duration of brightness change, for example 10 seconds. I calculate next value of floatPercent (see code snippet above) the way, sleepTime should always be less than 50ms. So looks like it had to be smooth. But I always get not smooth transition. It relates specially the range of 0% - 5% of brightness. Smoothness is completely lost on this brightness range.

I have already posted this question on Google Android Developer group, but anyway possibly somebody have already investigated in this area.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Lyubomyr Dutko
  • 4,486
  • 7
  • 32
  • 39

3 Answers3

15

From your code snippet, it would appear you are using Thread.sleep() on the UI thread.

Please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please do not use Thread.sleep() in the UI thread.

Since you are using post(), just switch to postDelayed(), passing it the time to wait before executing the Runnable.

In fact, Thread.sleep() may be cause of the problem you are experiencing, since Thread.sleep() on the UI says "Hey, Android! Please freeze the UI for a while!", so your brightness change may not take place until you return control to Android (e.g., return from whatever callback triggered your code).

Furthermore, what Ms. Hackborn told you on the Google Groups is absolutely correct: this is dependent on hardware. Not all hardware will offer the smoothness you seek. That may be your 0-5% problem -- the hardware only goes to, say, 5% brightness, then makes a jump to having the backlight be off, or something.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    if you add '----' underneith the line "please please please...", it will make the text bold. you may not need so many pleases if you do that ;) – Ape-inago Jul 16 '09 at 13:19
  • Hi, thanks a lot for your reply. It was a good set of fresh ideas about improvement, but unfortunatelly I failed to manage to resolve my task. Each approach, mentioned in this thread, gives almost the same result, and I can't gain smoothness of brightness transition. Screen brightness trembles anyway, but if you use tried to quickly change brightness in Android Settings, you will see similar behavior. Anyway, thanks a lot for help. – Lyubomyr Dutko Jul 17 '09 at 09:34
2

edit: check out commonsware.com's post. I didn't realize you may be trying to sleep the GUI...


Likely problem:

Thread.sleep's argument is only a minimum value. you may be telling it to sleep 50ms, but it may sleep for 80ms one time, 50ms the next, and then 120ms after that. it largely depends on when the processor decides to schedule you.


The best solution:

The best way to get this done would be to have some type of interrupt based on a kernel timer... that is to say, you'd need to get the operating system to call your program whenever some external timer goes off, and have your program handle that call using an interrupt function. TBH, I don't think android was designed to do this, but there may be a different way of handling your situation that approximates it.


The easiest solution:

is instead of

spentTime+=sleepTime; 

do something like this (these functions don't necessarily exist, this is just an example):

   float startTime = get_current_time();
   sleep(50);
   float endTime = get_current_time();
   sleepTime = startTime - endTime;
   spentTime += sleepTime;
   change_brightness.increase_by_amount(sleepTime/spentTime);

... this way the amount of sleep is proportional to the actual sleeping time. Of course it will be delayed by one go through the loop, but that shouldn't matter visually.

CJBS
  • 15,147
  • 6
  • 86
  • 135
Ape-inago
  • 1,870
  • 1
  • 13
  • 27
1

Please note that [Thread.sleep](http://developer.android.com/reference/java/lang/Thread.html#sleep(long) doesn't guarantee precision, i.e. it may block shorter or longer than 50ms. You can try to increase precision and see if that helps you achieve the desired smoothness.

Josef Pfleger
  • 74,165
  • 16
  • 97
  • 99