Android 调节屏幕亮度

来源:互联网 发布:数据资产管理2017 编辑:程序博客网 时间:2024/05/22 03:14
<span style="font-size:14px;">系统亮度</span>
public class BrightnessManager {

// 判断是否开启了自动亮度调节
public static boolean isAutoBrightness(Activity activity) {
boolean autoBrightness = false;
ContentResolver contentResolver = activity.getContentResolver();
try {
autoBrightness = Settings.System.getInt(contentResolver,
Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
} catch (SettingNotFoundException e) {
e.printStackTrace();
}
return autoBrightness;
}

// 获取当前系统亮度值
public static int getBrightness(Activity activity) {
int brightValue = 0; 
ContentResolver contentResolver = activity.getContentResolver();
try {
brightValue = Settings.System.getInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS);
} catch (SettingNotFoundException e) {
e.printStackTrace();
}  
return brightValue;
}

// 改变屏幕亮度
public static void setBrightness(Activity activity, int brightValue) {
WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
lp.screenBrightness = (brightValue <= 0 ? -1.0f : brightValue / 255f);
activity.getWindow().setAttributes(lp);
}

// 开启亮度自动亮度模式
public static void startAutoBrightness(Activity activity) {
Settings.System.putInt(activity.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
Uri uri = android.provider.Settings.System.getUriFor("screen_brightness");  
activity.getContentResolver().notifyChange(uri, null);  
}

// 停止自动亮度模式
public static void stopAutoBrightness(Activity activity) {
Settings.System.putInt(activity.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
Uri uri = android.provider.Settings.System.getUriFor("screen_brightness");  
activity.getContentResolver().notifyChange(uri, null);  
}

/** 
     * 设置当前屏幕亮度的模式  
    * SCREEN_BRIGHTNESS_MODE_AUTOMATIC=1 为自动调节屏幕亮度 
    * SCREEN_BRIGHTNESS_MODE_MANUAL=0 为手动调节屏幕亮度 
    */  
public static void setBrightnessMode(Activity activity, int brightMode) {  
        Settings.System.putInt(activity.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, brightMode);  

    }  


/**
 * 1.滑动SeekBar 改变当前屏幕的亮度值 
 * 2.点击button  调用系统的亮度(手动和自动两种模式)
 * 根据系统判断是手动还是自动
 * 
 */

public class MainActivity extends Activity {


private Button button;
private SeekBar lightSeek;
private boolean flag = false;
private int defalutValue = 75;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

button = (Button) findViewById(R.id.button);
lightSeek = (SeekBar) findViewById(R.id.light_seekBar);
lightSeek.setOnSeekBarChangeListener(seekBarChange);
}

public void System (View v) {
if (flag) {
flag = false;
button.setTextColor(getResources().getColor(R.color.black));
button.setText(getResources().getString(R.string.star));
setScreenLight(defalutValue);
} else {
flag = true; 
button.setTextColor(getResources().getColor(R.color.red));
button.setText(getResources().getString(R.string.close));

boolean isAutoBrightness = BrightnessManager.isAutoBrightness(this);
if (isAutoBrightness) {  // 自动调整亮度
BrightnessManager.setBrightness(this, -1);
} else {
//获取当前屏幕的亮度
int brightValue = BrightnessManager.getBrightness(this);
lightSeek.setProgress(brightValue);
BrightnessManager.setBrightness(this, brightValue);
}
}
}

private OnSeekBarChangeListener seekBarChange = new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
setScreenLight(progress);
}
};


public void setScreenLight(int progress) {
if (progress < 1) {
progress = 1;
} else if (progress > 255) {
progress = 255;
}
final WindowManager.LayoutParams attrs = getWindow().getAttributes();
attrs.screenBrightness = progress / 255f;
getWindow().setAttributes(attrs);
defalutValue = progress;
}


}



0 0