萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 安卓開發 >> Android屏幕亮度調節

Android屏幕亮度調節

SeekBar seekBar = (SeekBar) findViewById(R.id.seekBar);

// 進度條綁定最大亮度,255是最大亮度
seekBar.setMax(255);
// 取得當前亮度
int normal = Settings.System.getInt(getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS, 255);
// 進度條綁定當前亮度
seekBar.setProgress(normal);

seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// 取得當前進度
int tmpInt = seekBar.getProgress();

// 當進度小於80時,設置成80,防止太黑看不見的後果。
if (tmpInt < 80) {
tmpInt = 80;
}

// 根據當前進度改變亮度
Settings.System.putInt(getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS, tmpInt);
tmpInt = Settings.System.getInt(getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS, -1);
WindowManager.LayoutParams wl = getWindow().getAttributes();

float tmpFloat = (float) tmpInt / 255;
if (tmpFloat > 0 && tmpFloat <= 1) {
wl.screenBrightness = tmpFloat;
}
getWindow().setAttributes(wl);

}

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

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
}
});

copyright © 萬盛學電腦網 all rights reserved