业务上需要调节界面亮度,方便二维码扫描,记录一下。
系统的亮度值默认区间是0-255,调节亮度有以下两种
- 当前界面亮度
- 系统亮度
调节当前界面亮度
退出当前界面时,亮度自动调节模式继续生效。
/**
* 设置当前界面亮度值
*
* @param activity
* @param aimBrightness
*/
public static void setCurrentScreenBrightness(Activity activity, @IntRange(from = 0, to = 255) int aimBrightness) {
WindowManager.LayoutParams layoutParams = activity.getWindow().getAttributes();
layoutParams.screenBrightness = Float.valueOf(aimBrightness / 255.0f);
activity.getWindow().setAttributes(layoutParams);
}
调节系统亮度
调节系统亮度时需要申请WRITE_SETTINGS权限( 注意6.0以后的权限申请)
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
注意:只有为手动调节模式时,才能动态设置系统的亮度,所以需要提前对亮度模式进行操作,