[英]Screen brightness value in Android
How do you get the current screen brightness of your activity?您如何获得活动的当前屏幕亮度?
Following method works fine for setting the brightness to max by calling setBrightness(100)
:以下方法可以很好地通过调用
setBrightness(100)
将亮度设置为最大值:
private void setBrightness(int brightness) {
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.screenBrightness = brightness/100.0f;
getWindow().setAttributes(layoutParams);
}
Im trying to achieve the following:我试图实现以下目标:
Many thanks!非常感谢!
Try尝试
int curBrightnessValue = android.provider.Settings.System.getInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS);
and then进而
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.screenBrightness = curBrightnessValue/100.0f;
getWindow().setAttributes(layoutParams);
To reset brightness to default value将亮度重置为默认值
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.screenBrightness = -1f;
getWindow().setAttributes(layoutParams);
A value of less than 0, the default, means to use the preferred screen brightness.默认值小于 0 表示使用首选屏幕亮度。
Refer http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#screenBrightness参考http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#screenBrightness
I use this on Android 17+ (Probably works w/lower APIs)我在 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);
}
Usages:用法:
To set screen to MAX Brightness (for example when you show a Barcode or something):将屏幕设置为最大亮度(例如,当您显示条形码或其他内容时):
setScreenBrightnessTo(BRIGHTNESS_OVERRIDE_FULL);
To RESET back to whatever the user had before :重置回用户之前的任何内容:
setScreenBrightnessTo(BRIGHTNESS_OVERRIDE_NONE);
There are distinct brightness values: 1) if you already set something for activity brightness, you can get this value.有不同的亮度值:1)如果您已经为活动亮度设置了一些东西,您可以获得此值。 If not set it will be -1F 2) system value for brightness Example to get it:
如果未设置,它将是 -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
Example code to set brightness for activity window (bear in mind that value can be from 0.0 to 1.0):为活动窗口设置亮度的示例代码(请记住,该值可以从 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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.