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

时间:2022-09-20 12:14:42

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

此api只适合2.1以上版本:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
 * 判断是否开启了自动亮度调节
 *
 * @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进行亮度控制:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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);
?
1
2
3
4
5
6
7
8
9
10
11
12
13
<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程序设计有所帮助。