Android. Brightness Change

android, hardware, java

Solution

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.

Problem

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.

Original source