使用SeekBar实时更新屏幕亮度

时间:2013-06-10 20:22:38

标签: java android seekbar

所以我有调整系统亮度的代码,我已经正确实现了搜索条,但是当我调整搜索条时,屏幕亮度不会改变。我查看了其他一些帖子,解决方案似乎是创建一个可以快速打开和关闭的Dummy类。如果我这样做,虽然那时我觉得会有大量的滞后,因为随着搜索栏的移动,屏幕会不断刷新。

 @Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    ContentResolver cr = getContentResolver();
    try{
    int brightness = Settings.System.getInt(cr,Settings.System.SCREEN_BRIGHTNESS);
    Settings.System.putInt(cr, Settings.System.SCREEN_BRIGHTNESS, brightness);
    WindowManager.LayoutParams lp = getWindow().getAttributes();
    lp.screenBrightness = brightness / 255.0f;
    getWindow().setAttributes(lp);

    }
    catch (Settings.SettingNotFoundException a)
    {

    }
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
    //Toast.makeText(MainActivity.this, "Started Tracking Seekbar", Toast.LENGTH_SHORT).show();
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {

}

刷新的另一个问题是我在屏幕上运行此亮度控制,其中有相机预览并且每次虚拟屏幕加载需要一些时间并且进一步增加延迟时杀死相机

我确实看过这篇文章,但我无法弄清楚如何实现它。由于我没有使用偏好

I have a seekbar in my preferences to update screen brightness. Is there a way to refresh the UI to see the live change?

2 个答案:

答案 0 :(得分:3)

我用以下代码解决了问题,它现在按预期工作了!

@Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
    // TODO Auto-generated method stub
    BackLightValue = (float)arg1/100;

    WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
    layoutParams.screenBrightness = BackLightValue;
    getWindow().setAttributes(layoutParams);
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
}
@Override
public void onStopTrackingTouch(SeekBar arg0) {

    int SysBackLightValue = (int)(BackLightValue * 255);
    android.provider.Settings.System.putInt(getContentResolver(),
            android.provider.Settings.System.SCREEN_BRIGHTNESS,
            SysBackLightValue);
    }

答案 1 :(得分:3)

使用以下代码完美运行:

在你的xml文件中:

  <SeekBar
            android:id="@+id/seekBar1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="?android:attr/selectableItemBackground"
            android:max="10"
            android:progressDrawable="@drawable/progress"
            android:scaleType="fitCenter"
            android:thumb="@drawable/thumb" />

注意:在搜索栏中获取所需的属性。

在您的任何活动中使用以下方法并调用:

   private void setSeekbar()
   {
       seekBar = (SeekBar) findViewById(R.id.seekBar1);
        seekBar.setMax(255);
       float curBrightnessValue = 0;

       try {
                curBrightnessValue = android.provider.Settings.System.getInt(
                getContentResolver(),
                android.provider.Settings.System.SCREEN_BRIGHTNESS);
           } catch (SettingNotFoundException e) {
           e.printStackTrace();
           }

    int screen_brightness = (int) curBrightnessValue;
    seekBar.setProgress(screen_brightness);
    seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
    int progress = 0;

        @Override
        public void onProgressChanged(SeekBar seekBar, int progresValue,
                boolean fromUser) {
            progress = progresValue;

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            android.provider.Settings.System.putInt(getContentResolver(),
                    android.provider.Settings.System.SCREEN_BRIGHTNESS,
                    progress);
        }
    });
  }
相关问题