android調節屏幕亮度

brightness(亮度)是window中的一個屬性
修改亮度需要取得window屬性
方法為:WindowManager.LayoutParams layoutParams = getWindow().getAttributes(); 
        layoutParams.screenBrightness = b;// b是亮度        
getWindow().setAttributes(layoutParams); 
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);//進度條改變 www.aiwalls.com
         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 開發專欄

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *