最近在作一個Demo的時候用到了調節屏幕亮度的功能,因而上網搜索了一下,而且寫了一個小Demo測試了一下,發現代碼仍是比較簡單的。Android中的亮度調節,主要有三個方向,一個是針對於系統的亮度調節,一個是針對於App的亮度調節,一個是針對當前屏幕的亮度調節。android
詳細的內容你們能夠參考文章尾部博文,寫的特別詳細清楚,這裏咱們只改變當前屏幕的亮度的實現:app
下面是佈局文件:less
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等是同樣的效果。ide
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 }
這個方法主要是用來獲取系統當前的屏幕的亮度,這句話是重點:this
1 Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
其中的Settings是provider包下的。xml
須要注意的是,返回的亮度是介於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屬性設置值,完成屏幕亮度的改變。