Java – Android. Brightness change

I'm trying to solve the following task: smooth changing the brightness from 100% to 0%, but I can't get a smooth effect. It should be emphasized that I'm using the following brightness method to change. One recommended

WindowManager.LayoutParams lp = window.getAttributes();

lp.screenBrightness = floatPercent;

window.setAttributes(lp);

Well, it obviously works, but it doesn't go well. I'll describe the logic:

I have a thread to change the brightness:

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

I have the duration of brightness change, for example, 10 seconds. I calculate the next value of floatpercent (see the code snippet above). Sleeptime should always be less than 50ms. So it looks like it must be smooth. But I always have to make a smooth transition. It especially involves the range of 0% – 5% of brightness. In this case, the smoothness brightness range is completely lost

I've posted this question on Google's Android Developer group, but someone may have investigated this field anyway

resolvent:

From your code snippet, you might use thread. Sleep () on the UI thread

Please, please, please, please, please, please, please, please, please, please, please, please, please, please, please don't use thread.sleep in the UI thread

Since you are using post (), just switch to postdelayed (), and pass the time to runnable before executing it

In fact, thread. Sleep() may be the cause of your problem, because thread. Sleep() on the UI says "Hey, Android! Please pause the UI for a while!", so your brightness may not change until you return control to Android (for example, from any callback that triggers your code)

In addition, what Ms. hackborn told you on Google Groups is absolutely correct: it depends on the hardware. Not all hardware can provide the smoothness you seek. This may be your 0-5% problem - the hardware will only reach 5% brightness, and then jump to turn off the backlight or other things

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>