服务器之家

服务器之家 > 正文

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

时间:2021-04-27 15:21     来源/作者:傅荣康

本文实例讲述了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程序设计有所帮助。

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
返回顶部