如何获得活动的当前屏幕亮度?
通过调用setBrightness(100)
:
private void setBrightness(int brightness) {
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.screenBrightness = brightness/100.0f;
getWindow().setAttributes(layoutParams);
}
我试图实现以下目标:
非常感谢!
答案 0 :(得分:15)
尝试
int curBrightnessValue = android.provider.Settings.System.getInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS);
然后
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.screenBrightness = curBrightnessValue/100.0f;
getWindow().setAttributes(layoutParams);
答案 1 :(得分:5)
将亮度重置为默认值
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.screenBrightness = -1f;
getWindow().setAttributes(layoutParams);
默认值小于0表示使用首选屏幕亮度。
参考http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#screenBrightness
答案 2 :(得分:0)
我在Android 17+上使用它(可能适用于较低的API)
private void setScreenBrightnessTo(float brightness) {
WindowManager.LayoutParams lp = getActivity().getWindow().getAttributes();
if (lp.screenBrightness == brightness) {
return;
}
lp.screenBrightness = brightness;
getActivity().getWindow().setAttributes(lp);
}
用途:
要将屏幕设置为MAX亮度(例如,当您显示条形码或其他内容时):
setScreenBrightnessTo(BRIGHTNESS_OVERRIDE_FULL);
要重新回到用户之前的任何内容:
setScreenBrightnessTo(BRIGHTNESS_OVERRIDE_NONE);
答案 3 :(得分:0)
有不同的亮度值: 1)如果您已经为活动亮度设置了一些值,则可以获取此值。如果未设置,则为-1F 2)亮度系统值 获取示例:
val activityBrightness = requireActivity().window.attributes.screenBrightness
val systemBrightness = Settings.System.getInt(requireActivity().contentResolver, Settings.System.SCREEN_BRIGHTNESS) / 255
currentBrightness = if (activityBrightness == -1F) systemBrightness.toFloat() * 100 else activityBrightness * 100
为活动窗口设置亮度的示例代码(请记住,该值可以在0.0到1.0之间):
currentBrightness = if (currentBrightness < 0) 0.0F else if (currentBrightness > 100) 100.0F else currentBrightness
val layoutParams = activity?.window?.attributes
if (layoutParams != null) {
layoutParams.screenBrightness = currentBrightness / 100
requireActivity().window.attributes = layoutParams
}