3
    public void SetBright(float value) 
{
    Window mywindow = getWindow();

    WindowManager.LayoutParams lp = mywindow.getAttributes();

            lp.screenBrightness = value;

            mywindow.setAttributes(lp);
}

我想调整屏幕亮度。但是当我尝试使用这种方法时没有任何反应。可能是因为我使用了 KEEP_SCREEN_ON 标志吗?

4

3 回答 3

7

确保在设置屏幕亮度之前未启用“自动亮度”。如果您使用的是 Android 2.2 或更高版本的 SDK,您可以在“设置”>“显示”中手动执行此操作或使用代码。

就像是:

int brightnessMode = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE);
if (brightnessMode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
    Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
}

WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.screenBrightness = 0.5F; // set 50% brightness
getWindow().setAttributes(layoutParams);

确保该值介于 0.0F 和 1.0F 之间。-1.0F 的值使用存储在首选项中的默认亮度。根据文档“默认值小于 0,意味着使用首选的屏幕亮度。0 到 1 将亮度从暗调整为全亮。”

于 2011-02-23T11:58:46.943 回答
0

screenBrightness 的值介于0.0f和之间1.0f,可能你的值太大了。我用WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ONflag也可以调节屏幕亮度

于 2011-01-07T06:42:29.007 回答
0

使用搜索栏调整亮度:

公共类 AndroidBrightnessActivity 扩展 Activity {

private SeekBar brightbar;
private int brightness;
private ContentResolver cResolver;
private Window window;
TextView txtPerc;
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    brightbar = (SeekBar) findViewById(R.id.brightbar);
    txtPerc = (TextView) findViewById(R.id.txtPercentage);
    cResolver = getContentResolver();
    window = getWindow();
    brightbar.setMax(255);
    brightbar.setKeyProgressIncrement(1);

    try
    {
        brightness = System.getInt(cResolver, System.SCREEN_BRIGHTNESS);
    }
    catch (SettingNotFoundException e)
    {
        Log.e("Error", "Cannot access system brightness");
        e.printStackTrace();
    }
    brightbar.setProgress(brightness);
    brightbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
    {
        public void onStopTrackingTouch(SeekBar seekBar)
        {
            System.putInt(cResolver,System.SCREEN_BRIGHTNESS,brightness);
            LayoutParams layoutpars = window.getAttributes();
            layoutpars.screenBrightness = brightness / (float)255;
            window.setAttributes(layoutpars);
        }

        public void onStartTrackingTouch(SeekBar seekBar)
        {
            //Nothing handled here
        }

        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
        {
            if(progress<=20)
            {
              brightness=20;
            }
            else
            {
                brightness = progress;
            }
            float perc = (brightness /(float)255)*100;
            txtPerc.setText((int)perc +" %");
        }
    });   
}

}

快乐的编码...

于 2014-05-07T04:14:55.617 回答