研究调节屏幕亮度时遇到的问解决方案如下:
1、可以通过获取当前Activity 的WindowManager.LayoutParams 属性,然后设置其screenBrightness的值即可;
screenBrightness 的取值范围在[0,1]之间,具体可以如下操作:
WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.screenBrightness = 0.5; getWindow().setAttributes(lp);
首先获取系统屏幕的最大值,然后设置seekbar的setMax。
/** * 获取屏幕亮度 */ private int getScreenBrightness() { int nowBrightnessValue = 0; ContentResolver resolver = getContentResolver(); try { nowBrightnessValue = android.provider.Settings.System.getInt(resolver, Settings.System.SCREEN_BRIGHTNESS); } catch (Exception e) { e.printStackTrace(); } return nowBrightnessValue; }
然后在调节屏幕亮度函数中与seekbar进行绑定
private void setScreenLight(View popupView) { mBrightnessProgress = getScreenBrightness(); final SeekBar mSeekBarLight = (SeekBar) popupView.findViewById(R.id.seekBarLight); mSeekBarLight.setProgress(mBrightnessProgress); mSeekBarLight.setMax(MAX_BRIGHTNESS); mSeekBarLight.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // TODO Auto-generated method stub if (fromUser) { WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.screenBrightness = (float) (progress / (MAX_BRIGHTNESS * 1.0)); getWindow().setAttributes(lp); } } @Override public void onStartTrackingTouch(SeekBar arg0) { // TODO Auto-generated method stub } @Override public void onStopTrackingTouch(SeekBar arg0) { // TODO Auto-generated method stub } });
}
活动滚动条的过程中,屏幕亮度不发生改变。
因为调节亮度作用域发生了改变,之前是在Activity里面对亮度修改, 而现在是作为子activity对TabActivity或ViewGroup 容器进行修改,因此不能成功,需要
通过getParent()方法获取器Parent,然后设置。
private void setScreenLight(View popupView) { mBrightnessProgress = getScreenBrightness(); final SeekBar mSeekBarLight = (SeekBar) popupView.findViewById(R.id.seekBarLight); mSeekBarLight.setProgress(mBrightnessProgress); mSeekBarLight.setMax(MAX_BRIGHTNESS); mSeekBarLight.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // TODO Auto-generated method stub if (fromUser) { WindowManager.LayoutParams lp = getParent().getWindow().getAttributes(); lp.screenBrightness = (float) (progress / (MAX_BRIGHTNESS * 1.0)); getParent().getWindow().setAttributes(lp); } } @Override public void onStartTrackingTouch(SeekBar arg0) { // TODO Auto-generated method stub } @Override public void onStopTrackingTouch(SeekBar arg0) { // TODO Auto-generated method stub } }); }