How could you increase the screen brightness in Android using a seek bar?

家住魔仙堡 提交于 2019-12-08 14:19:46

问题


I would like to increase the brightness of the screen using a seek bar within my Android application, but I'm not sure how to do this. How can I add this functionality?


回答1:


Using this you can set the screen brightness:

WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = 100 / 100.0f;
getWindow().setAttributes(lp);

Now, as you want to implement progress bar like control, i would suggest you to implement SeekBar, so using this you can easily increase/decrease based on tracking value of seekbar.

Here is the full example implemented using the SeekBar.




回答2:


Use this to change the brightness (system-wide)

 ContentResolver cr = getContentResolver();
 int brightness = Settings.System.getInt(cr,Settings.System.SCREEN_BRIGHTNESS);            
 Settings.System.putInt(cr, Settings.System.SCREEN_BRIGHTNESS, brightness);
 WindowManager.LayoutParams lp = getWindow().getAttributes();
 lp.screenBrightness = brightness / 255.0f;
 getWindow().setAttributes(lp);

You may use this example and call the above code in onProgressChanged(). Default values of progress range from 0 to 100.




回答3:


You can change the screen brightness using the WindowManager.LayoutParams. Get the LayoutParams using getWindow().getAttributes() then set the field screenBrightness to a number between 0 to 1 (0 dark, 1 bright) and then call getWindow().setAttributes() with your modified LayoutParams object.



来源:https://stackoverflow.com/questions/7845458/how-could-you-increase-the-screen-brightness-in-android-using-a-seek-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!