Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用SeekBar实时更新屏幕亮度_Java_Android_Seekbar - Fatal编程技术网

Java 使用SeekBar实时更新屏幕亮度

Java 使用SeekBar实时更新屏幕亮度,java,android,seekbar,Java,Android,Seekbar,因此,我有调整系统亮度的代码,并且我已经正确地实现了搜索栏,但是当我调整seekbar时,屏幕亮度不会改变。我查看了其他一些帖子,解决方案似乎是创建一个虚拟类,它可以快速打开和关闭。如果我这样做的话,我觉得会有一个吨的滞后,因为屏幕会不断刷新,因为搜索杆是移动的 @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { ContentResolver cr = g

因此,我有调整系统亮度的代码,并且我已经正确地实现了搜索栏,但是当我调整seekbar时,屏幕亮度不会改变。我查看了其他一些帖子,解决方案似乎是创建一个虚拟类,它可以快速打开和关闭。如果我这样做的话,我觉得会有一个吨的滞后,因为屏幕会不断刷新,因为搜索杆是移动的

 @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) {

}
刷新的另一个问题是,我在有摄像头预览的屏幕上运行此亮度控制,每次虚拟屏幕加载都会花费一些时间,并进一步增加延迟

我确实看到了这篇文章,但我不知道如何实现它。因为我没有使用偏好


我用下面的代码解决了这个问题,现在它可以正常工作了

@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);
    }

使用我的以下代码,它可以完美地运行:

在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);
        }
    });
  }