android调节屏幕亮度

android调节屏幕亮度_第1张图片

brightness(亮度)是window中的一个属性

修改亮度需要取得window属性

方法为:WindowManager.LayoutParams layoutParams = getWindow().getAttributes();  

  1.         layoutParams.screenBrightness = b;// b是亮度         
  2. getWindow().setAttributes(layoutParams);  
  3. b是一个浮点数 从0~1 ,表示亮度

因此可以通过设置b的值预览屏幕的亮度,然后通过

 Settings.System.putInt(mContext.getContentResolver(),  Settings.System.SCREEN_BRIGHTNESS,brightness);s设置屏幕亮度,brightness值为30-255

实现如图所示效果代码:

1.自定义一个SeekbarPreference继承dialogPreference实现亮度预览

public class SeekBarPreference extends DialogPreference implements OnSeekBarChangeListener{
    private static final String TAG = "SeekBarPreference";
    private static final int MINIMUM_BACKLIGHT = 30;//最低亮度
    private static final int MAXIMUM_BACKLIGHT = 255;
    private SeekBar bar;
    private int oldBrightness;
    private   SharedPreferences sp;
    private static String KEY="brightness";

    public SeekBarPreference(Context context, AttributeSet attrs) {
        super(context, attrs);

        setDialogLayoutResource(R.layout.seekbar_dialog);
        setPositiveButtonText(android.R.string.ok);
        setNegativeButtonText(android.R.string.cancel);

         setDialogIcon(R.drawable.ic_settings_display);    
    }

    @Override
    protected void onBindDialogView(View view) {
        super.onBindDialogView(view); 
        sp=getSharedPreferences();
         bar= (SeekBar) view.findViewById(R.id.seekbar);
         bar.setMax(MAXIMUM_BACKLIGHT-MINIMUM_BACKLIGHT);
         oldBrightness=sp.getInt(KEY, 125);
         bar.setOnSeekBarChangeListener(this);//进度条改变
         bar.setProgress(oldBrightness-MINIMUM_BACKLIGHT);//初始化进度条
    }
    @Override
    protected void onDialogClosed(boolean positiveResult) {
        // TODO Auto-generated method stub
        super.onDialogClosed(positiveResult);
        if(positiveResult){
           /*do nothing*/
        }else{       
           sp.edit().putInt(KEY, oldBrightness).commit();
         
        }
    }
   

    @Override
    public void onProgressChanged(SeekBar seekbar, int progress, boolean arg2) {
        // TODO Auto-generated method stub
       sp.edit().putInt(KEY, progress+MINIMUM_BACKLIGHT).commit();   //进度条改变时改变存储数据,
    }

    @Override
    public void onStartTrackingTouch(SeekBar arg0) {
        // TODO Auto-generated method stub
       
    }

    @Override
    public void onStopTrackingTouch(SeekBar arg0) {
        // TODO Auto-generated method stub
       
    }
 
}

2.调节亮度

public class ControlBrightness  extends PreferenceActivity implements OnSharedPreferenceChangeListener {
   private  SeekBarPreference seekBarPreference;
   private Button enBtn;
   private Button disBtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.buttonbar);
        addPreferencesFromResource(R.xml.custom_setting);
       SharedPreferences sp=  getSharedPreferences(BatteryUtils.FILE_NAME, MODE_WORLD_WRITEABLE);
       sp.registerOnSharedPreferenceChangeListener(this);//监听数据变化,调节屏幕亮度
           }
    @Override
    public void onSharedPreferenceChanged(SharedPreferences sp, String key) {
        // TODO Auto-generated method stub
      
        if(TextUtils.equals(key, "brightness")){
           int brightness=sp.getInt("brightness", 125);
           LayoutParams lp=   getWindow().getAttributes();
          lp.screenBrightness=brightness/255.0f;
          getWindow().setAttributes(lp);
        }
       
    }
 }

 

 

 

 

你可能感兴趣的:(android,String,存储,Class,dialog,button)