Android开发

Android编程获取并设置Activity亮度的方法

本文主要是介绍Android编程获取并设置Activity亮度的方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

本文实例讲述了Android编程获取并设置Activity亮度的方法。分享给大家供大家参考,具体如下:

此API只适合2.1以上版本:

/**
 * 判断是否开启了自动亮度调节
 *
 * @param aContext
 * @return
 */
public static boolean isAutoBrightness(ContentResolver aContentResolver) {
  boolean automicBrightness = false;
  try {
    automicBrightness = Settings.System.getInt(aContentResolver,
        Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
  } catch (SettingNotFoundException e) {
    e.printStackTrace();
  }
  return automicBrightness;
}
/**
 * 获取屏幕的亮度
 *
 * @param activity
 * @return
 */
public static int getScreenBrightness(Activity activity) {
  int nowBrightnessValue = 0;
  ContentResolver resolver = activity.getContentResolver();
  try {
    nowBrightnessValue = android.provider.Settings.System.getInt(
        resolver, Settings.System.SCREEN_BRIGHTNESS);
  } catch (Exception e) {
    e.printStackTrace();
  }
  return nowBrightnessValue;
}
/**
 * 设置亮度
 *
 * @param activity
 * @param brightness
 */
public static void setBrightness(Activity activity, int brightness) {
  // Settings.System.putInt(activity.getContentResolver(),
  // Settings.System.SCREEN_BRIGHTNESS_MODE,
  // Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
  WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
  lp.screenBrightness = Float.valueOf(brightness) * (1f / 255f);
  activity.getWindow().setAttributes(lp);
}
/**
 * 停止自动亮度调节
 *
 * @param activity
 */
public static void stopAutoBrightness(Activity activity) {
  Settings.System.putInt(activity.getContentResolver(),
      Settings.System.SCREEN_BRIGHTNESS,
      Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
}
/**
 * 开启亮度自动调节
 *
 * @param activity
 */
public static void startAutoBrightness(Activity activity) {
  Settings.System.putInt(activity.getContentResolver(),
      Settings.System.SCREEN_BRIGHTNESS_MODE,
      Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
}
/**
 * 保存亮度设置状态
 *
 * @param resolver
 * @param brightness
 */
public static void saveBrightness(ContentResolver resolver, int brightness) {
  Uri uri = android.provider.Settings.System
      .getUriFor("screen_brightness");
  android.provider.Settings.System.putInt(resolver, "screen_brightness",
      brightness);
  resolver.notifyChange(uri, null);
}

使用SeekBar进行亮度控制:

sSeekBar = (SeekBar) findViewById(R.id.seek_bar);
sSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
  @Override
  public void onStopTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub
  }
  @Override
  public void onStartTrackingTouch(SeekBar seekBar) {
  }
  @Override
  public void onProgressChanged(SeekBar seekBar, int progress,
      boolean fromUser) {
    if (progress < 10) {
    } else {
      setBrightness(Test2Activity.this, progress);
    }
  }
});
//获取当前亮度的位置
int a =getScreenBrightness(this);
sSeekBar.setProgress(a);

<SeekBar
  android:id="@+id/seek_bar"
  android:layout_width="200dp"
  android:layout_height="wrap_content"
  android:layout_alignParentBottom="true"
  android:max="255"
  android:maxHeight="8dp"
  android:minHeight="8dp"
  android:paddingBottom="5dp"
  android:paddingLeft="13dp"
  android:paddingRight="13dp"
  android:progress="0"
   />

希望本文所述对大家Android程序设计有所帮助。

这篇关于Android编程获取并设置Activity亮度的方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!