• Android中获取并设置屏幕亮度


      最近在做一个Demo的时候用到了调节屏幕亮度的功能,于是上网搜索了一下,并且写了一个小Demo测试了一下,发现代码还是比较简单的。Android中的亮度调节,主要有三个方向,一个是针对于系统的亮度调节一个是针对于App的亮度调节一个是针对当前屏幕的亮度调节

      详细的内容大家可以参考文章尾部博文,写的特别详细清楚,这里我们只改变当前屏幕的亮度的实现:

    下面是布局文件:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     xmlns:tools="http://schemas.android.com/tools"
     4     android:layout_width="match_parent"
     5     android:layout_height="match_parent"
     6     android:paddingBottom="@dimen/activity_vertical_margin"
     7     android:paddingLeft="@dimen/activity_horizontal_margin"
     8     android:paddingRight="@dimen/activity_horizontal_margin"
     9     android:paddingTop="@dimen/activity_vertical_margin"
    10     tools:context="ggcomic.rabbit.lx.adjustlight.MainActivity">
    11 
    12 
    13     <TextView
    14         android:id="@+id/tv"
    15         android:layout_below="@+id/seek"
    16         android:layout_width="wrap_content"
    17         android:layout_height="wrap_content"
    18         android:text="哈哈哈哈"/>
    19     <SeekBar
    20         android:id="@+id/seek"
    21         android:layout_width="match_parent"
    22         android:layout_height="wrap_content" />
    23     <CheckBox
    24         android:id="@+id/cb"
    25         android:layout_below="@+id/tv"
    26         android:layout_width="wrap_content"
    27         android:layout_height="wrap_content"
    28         android:text="跟随系统亮度"/>
    29 </RelativeLayout>

    布局很简单,这里使用seekBar来充当进度条,与使用ProgressBar等是一样的效果。

    MainActivity中的代码:

      1 package ggcomic.rabbit.lx.adjustlight;
      2 
      3 import android.os.Bundle;
      4 import android.provider.Settings;
      5 import android.support.v7.app.AppCompatActivity;
      6 import android.view.Window;
      7 import android.view.WindowManager;
      8 import android.widget.CheckBox;
      9 import android.widget.CompoundButton;
     10 import android.widget.SeekBar;
     11 import android.widget.Toast;
     12 
     13 public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
     14 
     15 
     16     private SeekBar seekBar;
     17     private CheckBox cb;
     18 
     19     @Override
     20     protected void onCreate(Bundle savedInstanceState) {
     21         super.onCreate(savedInstanceState);
     22         setContentView(R.layout.activity_main);
     23         initView();
     24         initEvent();
     25     }
     26 
     27     /**
     28      * 初始化监听
     29      */
     30     private void initEvent() {
     31         //设置seekBar进度被改变的时候的时间监听
     32         seekBar.setOnSeekBarChangeListener(new MyOnSeekBarChangeListener());
     33         //设置CheckBox的点选监听事件
     34         cb.setOnCheckedChangeListener(this);
     35     }
     36 
     37     /**
     38      * 初始化控件的一些操作
     39      */
     40     private void initView() {
     41         seekBar = (SeekBar) findViewById(R.id.seek);
     42         cb = (CheckBox) findViewById(R.id.cb);
     43         //设置最大刻度
     44         seekBar.setMax(255);
     45         //设置初始的Progress
     46         seekBar.setProgress(getSystemBrightness());
     47         //出世设置checkBox为选中状态
     48         cb.setChecked(true);
     49         //设置初始的屏幕亮度与系统一致
     50         changeAppBrightness(getSystemBrightness());
     51     }
     52 
     53     /**
     54      * 获得系统亮度
     55      *
     56      * @return
     57      */
     58     private int getSystemBrightness() {
     59         int systemBrightness = 0;
     60         try {
     61             systemBrightness = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
     62         } catch (Settings.SettingNotFoundException e) {
     63             e.printStackTrace();
     64         }
     65         return systemBrightness;
     66     }
     67 
     68     /**
     69      * 改变App当前Window亮度
     70      *
     71      * @param brightness
     72      */
     73     public void changeAppBrightness(int brightness) {
     74         Window window = this.getWindow();
     75         WindowManager.LayoutParams lp = window.getAttributes();
     76         if (brightness == -1) {
     77             lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
     78         } else {
     79             lp.screenBrightness = (brightness <= 0 ? 1 : brightness) / 255f;
     80         }
     81         window.setAttributes(lp);
     82     }
     83 
     84     @Override
     85     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
     86         if (isChecked) {
     87             Toast.makeText(this, getSystemBrightness() + "", Toast.LENGTH_SHORT).show();
     88             changeAppBrightness(getSystemBrightness());
     89         } else {
     90             int seekBarProgress = seekBar.getProgress();
     91             changeAppBrightness(seekBarProgress);
     92         }
     93     }
     94 
     95     class MyOnSeekBarChangeListener implements SeekBar.OnSeekBarChangeListener {
     96         @Override
     97         public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
     98             //seekBar进度条被改变的时候取消checkBox的点选
     99             cb.setChecked(false);
    100             //改变亮度
    101             changeAppBrightness(progress);
    102         }
    103 
    104         @Override
    105         public void onStartTrackingTouch(SeekBar seekBar) {
    106 
    107         }
    108 
    109         @Override
    110         public void onStopTrackingTouch(SeekBar seekBar) {
    111 
    112         }
    113     }
    114 
    115 }

    代码看起来好像很多的样子,不过关键代码只有不多,获取系统亮度

    1 private int getSystemBrightness() {
    2         int systemBrightness = 0;
    3         try {
    4             systemBrightness = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
    5         } catch (Settings.SettingNotFoundException e) {
    6             e.printStackTrace();
    7         }
    8         return systemBrightness;
    9     }

    这个方法主要是用来获取系统当前的屏幕的亮度,这句话是重点:

    1 Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);

    其中的Settings是provider包下的。

    需要注意的是,返回的亮度是介于0~255之间的int类型值(也是为什么我们将seekBar的MaxValue设置为255的原因)

    然后是设置当前屏幕的亮度的代码

     1 public void changeAppBrightness(int brightness) {
     2         Window window = this.getWindow();
     3         WindowManager.LayoutParams lp = window.getAttributes();
     4         if (brightness == -1) {
     5             lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
     6         } else {
     7             lp.screenBrightness = (brightness <= 0 ? 1 : brightness) / 255f;
     8         }
     9         window.setAttributes(lp);
    10     }

    这里我们先通过Window对象来获取当前窗口,然后通过

    1 WindowManager.LayoutParams lp = window.getAttributes();

    老获取当前窗口的属性对象,screenBrightness为属性对象中一个字段,源码如下:

    1 /**
    2          * This can be used to override the user's preferred brightness of
    3          * the screen.  A value of less than 0, the default, means to use the
    4          * preferred screen brightness.  0 to 1 adjusts the brightness from
    5          * dark to full bright.
    6          */
    7         public float screenBrightness = BRIGHTNESS_OVERRIDE_NONE;

    可见,screenBrightness是一个0.0~1.0之间的float类型的参数,亮度由0.0~1.0递增。如果该值小于0,则默认采取最优的屏幕亮度来适配(经过测试就是系统亮度),这里我们使用了一个三目运算符,判断如果亮度值小于0的话将其置为1.

    代码中关于brightness==-1的判断是将-1作为一个标志位,标志当app设置中包含"跟随系统亮度"或者"恢复系统亮度"的时候,我们传递一个-1参数,这时将screenBrightness参数还原成默认数值(跟随系统)即可。

    思路

    拖动seekBar的进度条来改变屏幕亮度的时候,主要的思路就是:

    设置seekBar的OnSeekBarChangeListener监听,当seekBar的进度改变的时候,我们得到当前的进度,然后将其转换成0.0~1.0之间的值,为当前窗体的属性screenBrightness属性设置值,完成屏幕亮度的改变。

    最后附上一篇特别相信的文章,大家可以去看看~

    Android 亮度调节

  • 相关阅读:
    [LeetCode#114]Flatten Binary Tree to Linked List
    [LeetCode#103]Binary Tree Zigzag Level Order Traversal
    [LeetCode#102]Binary Tree Level Order Traversal
    [LeetCode#145]Binary Tree Postorder Traversal
    [LeetCode#]Binary Tree Preorder Traversal
    [LeetCode#144]Binary Tree Preorder Traversal
    [LeetCode#94]Binary Tree Inorder Traversal
    [LeetCode#101]Symmetric Tree
    [LeetCode#100]Same Tree
    [LeetCode#104, 111]Maximum Depth of Binary Tree, Minimum Depth of Binary Tree
  • 原文地址:https://www.cnblogs.com/RabbitLx/p/5886528.html
Copyright © 2020-2023  润新知