2 usniyo usniyo 于 2012.10.11 13:40 提问

android 关于调节屏幕亮度的问题

请问,设置屏幕亮度的值的范围是多少?网上有说是从0到1,我在设置时,从0.49到1.0这个范围内,每增加或减少0.01,亮度就会改变。但在0.0到0.48这个范围内,值的改变并不影响屏幕亮度,这是为何?

public static void brightnessPreview(Activity activity, float brightness)
{
    Window window = activity.getWindow();
    WindowManager.LayoutParams lp = window.getAttributes();
    lp.screenBrightness = brightness;
    window.setAttributes(lp);
}

2个回答

usniyo
usniyo   2012.12.05 23:16
已采纳

这是我自己封装的:

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.view.Window;
import android.view.WindowManager;

public class ScreenBrightnessTool
{
    /**
     * Activty自动调节亮度模式
     */
    public static final int ACTIVITY_BRIGHTNESS_AUTOMATIC = -1;
    /**
     * 自动调节模式
     */
    public static final int SCREEN_BRIGHTNESS_MODE_AUTOMATIC = Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
    /**
     * 手动调节模式
     */
    public static final int SCREEN_BRIGHTNESS_MODE_MANUAL = Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL;
    /**
     * 默认亮度
     */
    public static final int SCREEN_BRIGHTNESS_DEFAULT = 75;
    /**
     * 最大亮度
     */
    public static final int MAX_BRIGHTNESS = 100;
    /**
     * 最小亮度
     */
    public static final int MIN_BRIGHTNESS = 0;

    private static final int mMaxBrighrness = 255;
    private static final int mMinBrighrness = 120;

    // 当前系统调节模式
    private boolean sysAutomaticMode;
    // 当前系统亮度值
    private int sysBrightness;

    private Context context;

    private ScreenBrightnessTool(Context context, int sysBrightness,
            boolean sysAutomaticMode)
    {
        this.context = context;
        this.sysBrightness = sysBrightness;
        this.sysAutomaticMode = sysAutomaticMode;
    }

    /**
     * 创建屏幕亮度工具
     * 
     * @param context
     * @return
     */
    public static ScreenBrightnessTool Builder(Context context)
    {
        int brightness;
        boolean automaticMode;
        try
        {
            // 获取当前系统亮度值
            brightness = Settings.System.getInt(context.getContentResolver(),
                    Settings.System.SCREEN_BRIGHTNESS);
            // 获取当前系统调节模式
            automaticMode = Settings.System.getInt(
                    context.getContentResolver(),
                    Settings.System.SCREEN_BRIGHTNESS_MODE) == SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
        }
        catch (SettingNotFoundException e)
        {
            return null;
        }

        return new ScreenBrightnessTool(context, brightness, automaticMode);
    }

    /**
     * 返回当前系统亮度调节模式
     * 
     * @return
     */
    public boolean getSystemAutomaticMode()
    {
        return sysAutomaticMode;
    }

    /**
     * 返回当前系统亮度值
     * 
     * @return
     */
    public int getSystemBrightness()
    {
        return sysBrightness;
    }

    /**
     * 设置调节模式
     * 
     * @param mode
     *            调节模式
     */
    public void setMode(int mode)
    {
        if (mode != SCREEN_BRIGHTNESS_MODE_AUTOMATIC
                && mode != SCREEN_BRIGHTNESS_MODE_MANUAL)
            return;

        sysAutomaticMode = mode == SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
        Settings.System.putInt(context.getContentResolver(),
                Settings.System.SCREEN_BRIGHTNESS_MODE, mode);
    }

    /**
     * 设置屏幕亮度
     * 
     * @param brightness
     *            亮度值,值为0至100
     */
    public void setBrightness(int brightness)
    {
        int mid = mMaxBrighrness - mMinBrighrness;
        int bri = (int) (mMinBrighrness + mid * ((float) brightness)
                / MAX_BRIGHTNESS);

        ContentResolver resolver = context.getContentResolver();
        Settings.System
                .putInt(resolver, Settings.System.SCREEN_BRIGHTNESS, bri);
    }

    /**
     * 亮度预览
     * 
     * @param activity
     *            预览activity
     * @param brightness
     *            亮度值(0.47~1)
     */
    public static void brightnessPreview(Activity activity, float brightness)
    {
        Window window = activity.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.screenBrightness = brightness;
        window.setAttributes(lp);
    }

    /**
     * 亮度预览
     * 
     * @param activity
     *            预览activity
     * @param percent
     *            百分比(0.0~1.00)
     */
    public static void brightnessPreviewFromPercent(Activity activity,
            float percent)
    {
        float brightness = percent + (1.0f - percent)
                * (((float) mMinBrighrness) / mMaxBrighrness);
        brightnessPreview(activity, brightness);
    }

}
Sueyexin
Sueyexin   2012.10.11 14:07

在1.5SDK以上的版本中,可以用这个方法实现:

privatevoid brightnessMax() {
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = 1.0f;
getWindow().setAttributes(lp);
}

screenBrightness这个值的范围为0-1。 0最暗,1最亮。这个值也可取小于0的值,表示使用用户首选的亮度值...

suannai0314
suannai0314 (⊙o⊙)…我是运营。。这个真心不会
大约 5 年之前 回复
suannai0314
suannai0314 能将最后的解决方法贴出来然后采纳么?
大约 5 年之前 回复
usniyo
usniyo 感谢您的回答,经过半天跟踪、测试,这个值的范围应该是120.0f/255.0f到1.0f。也不知道对不对,但貌似解决了。
5 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!