我还尝试过获取亮度(Android P,API 28的对数变体)。这是我所做的(请参见下面的代码):
- 从Google的公开可用API进行了逆向工程(链接:https://cs.android.com/),
- 使用反射获取最小和最大亮度值,
- 从MathUtils中获取所需的数学方法。
只需调用getCurrentBrightness来获取百分比亮度值。不幸的是,即使使用了这个设置,我仍然无法确定与Android显示设置中显示的相同亮度值...
我还尝试过使用brightnessIntToFloat(PowerManager / BrightnessSynchronizer;代码也包含在下面),但没有成功。
你有任何想法还可能出了什么问题吗?
public class BrightnessUtils {
public static double getCurrentBrightness(Context mContext) {
float screenBrightness = 0;
try {
screenBrightness = Settings.System.getFloat(mContext.getContentResolver(), "screen_brightness");
} catch (Exception e){
Log.d("Exception", e.toString());
}
return getPercentage((double) (screenBrightness != 0 ? convertLinearToGammaFloat(screenBrightness, getMinimumScreenBrightnessSetting(mContext), getMaximumScreenBrightnessSetting(mContext)) : 0), 0, 65535);
}
public static float norm(float start, float stop, float value) {
return (value - start) / (stop - start);
}
public static float sqrt(float a) {
return (float) Math.sqrt(a);
}
public static float log(float a) {
return (float) Math.log(a);
}
public static float lerp(int start, int stop, float amount) {
return lerp((float) start, (float) stop, amount);
}
public static float lerp(float start, float stop, float amount) {
return start + (stop - start) * amount;
}
public static float constrainedMap(
float rangeMin, float rangeMax, float valueMin, float valueMax, float value) {
return lerp(rangeMin, rangeMax, lerpInvSat(valueMin, valueMax, value));
}
public static float lerpInvSat(float a, float b, float value) {
return saturate(lerpInv(a, b, value));
}
public static float lerpInv(float a, float b, float value) {
return a != b ? ((value - a) / (b - a)) : 0.0f;
}
public static float saturate(float value) {
return constrain(value, 0.0f, 1.0f);
}
public static float constrain(float amount, float low, float high) {
return amount < low ? low : (amount > high ? high : amount);
}
public static float brightnessIntToFloat(int brightnessInt) {
final float minFloat = 0.0f;
final float maxFloat = 1.0f;
final float minInt = 1;
final float maxInt = 255;
return constrainedMap(minFloat, maxFloat, minInt, maxInt, brightnessInt);
}
public static int convertLinearToGammaFloat(float val, float min, float max) {
final int GAMMA_SPACE_MIN = 0;
final int GAMMA_SPACE_MAX = 65535;
final float R = 0.5f;
final float A = 0.17883277f;
final float B = 0.28466892f;
final float C = 0.55991073f;
final float normalizedVal = norm(min, max, val) * 12.0f;
final float ret;
if (normalizedVal <= 1f) {
ret = sqrt(normalizedVal) * R;
} else {
ret = A * log(normalizedVal - B) + C;
}
return Math.round(lerp(GAMMA_SPACE_MIN, GAMMA_SPACE_MAX, ret));
}
private static double getPercentage(double d, int i, int i2) {
if (d > ((double) i2)) {
return 1.0d;
}
double d2 = (double) i;
if (d < d2) {
return 0.0d;
}
return (d - d2) / ((double) (i2 - i));
}
public static float getMinimumScreenBrightnessSetting (Context context) {
final android.content.res.Resources res = android.content.res.Resources.getSystem();
int id = res.getIdentifier("config_screenBrightnessSettingMinimum", "integer", "android");
if (id != 0) {
try {
return res.getInteger(id);
}
catch (android.content.res.Resources.NotFoundException e) {
}
}
return 0;
}
public static float getMaximumScreenBrightnessSetting (Context context) {
final android.content.res.Resources res = android.content.res.Resources.getSystem();
final int id = res.getIdentifier("config_screenBrightnessSettingMaximum", "integer", "android");
if (id != 0) {
try {
return res.getInteger(id);
}
catch (android.content.res.Resources.NotFoundException e) {
}
}
return 255;
}
}