当前位置:   article > 正文

在安卓下拉状态栏的亮度进度条旁加一个自动亮度调节的开关_下拉状态栏新增自动调节亮度开关

下拉状态栏新增自动调节亮度开关
安卓的原生的SystemUI中下拉状态栏有一个亮度控制条,这个控制条可以控制手机的显示亮度,但是默认这里是没有自动亮度控制的开关的,本篇文章教大家如何在状态栏中添加一个自动亮度调节的开关,废话不多说,进入正题

首先我们来看一下下拉状态栏亮度进度条的位置,进度条的布局位于frameworks/base/packages/SystemUI/res/layout/quick_settings_brightness_dialog.xml
它在frameworks/base/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java文件中被加载,所以我们要在quick_settings_brightness_dialog.xml中加一个控制自动亮度的CheckBox,位置可根据实际情况自己摆放

frameworks/base/packages/SystemUI/res/layout/quick_settings_brightness_dialog.xml

  1. <LinearLayout
  2. android:layout_width="wrap_content"
  3. android:layout_height="wrap_content"
  4. android:orientation="vertical"
  5. android:layout_gravity="center_vertical"
  6. >
  7. <CheckBox
  8. android:id="@+id/brightness_check"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:button="@null"
  12. android:background="@drawable/ic_checkbox_style"
  13. />
  14. <TextView
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:paddingTop="0dp"
  18. android:textSize="11dp"
  19. android:layout_gravity="center_horizontal"
  20. android:textColor="@color/gray_text_color"
  21. android:text="@string/auto"
  22. />
  23. </LinearLayout>

我在这里给这个CheckBox加了一个background,其实就是改变了CheckBox的样式,这个不难,网上有相关介绍,具体的样式定义如下

frameworks/base/packages/SystemUI/res/drawable/ic_check_box_normal.xml
  1. <vector xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:width="24dp"
  3. android:height="24dp"
  4. android:viewportWidth="24.0"
  5. android:viewportHeight="24.0">
  6. <path
  7. android:fillColor="#666666"
  8. android:pathData="M19,5v14H5V5h14m0,-2H5c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h14c1.1,0 2,-0.9 2,-2V5c0,-1.1 -0.9,-2 -2,-2z"/>
  9. </vector>
frameworks/base/packages/SystemUI/res/drawable/ic_check_box_select.xml
  1. <vector xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:width="24dp"
  3. android:height="24dp"
  4. android:viewportWidth="24.0"
  5. android:viewportHeight="24.0">
  6. <path
  7. android:fillColor="#56C0E5"
  8. android:pathData="M19,3L5,3c-1.11,0 -2,0.9 -2,2v14c0,1.1 0.89,2 2,2h14c1.11,0 2,-0.9 2,-2L21,5c0,-1.1 -0.89,-2 -2,-2zM10,17l-5,-5 1.41,-1.41L10,14.17l7.59,-7.59L19,8l-9,9z"/>
  9. </vector>
frameworks/base/packages/SystemUI/res/drawable/ic_checkbox_style.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:drawable="@drawable/ic_check_box_select" android:state_checked="true"/>
  4. <item android:drawable="@drawable/ic_check_box_normal" android:state_checked="false"/>
  5. </selector>

接着我们来看一下引用quick_settings_brightness_dialog的地方,frameworks/base/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java文件我们来看一下QSPanel的构造方法
  1. public QSPanel(Context context, AttributeSet attrs) {
  2. super(context, attrs);
  3. mContext = context;
  4. setOrientation(VERTICAL);
  5. mBrightnessView = LayoutInflater.from(context).inflate(
  6. R.layout.quick_settings_brightness_dialog, this, false);
  7. addView(mBrightnessView);
  8. setupTileLayout();
  9. mFooter = new QSFooter(this, context);
  10. addView(mFooter.getView());
  11. updateResources();
  12. mBrightnessController = new BrightnessController(getContext(),
  13. (ImageView) findViewById(R.id.brightness_icon),
  14. (ToggleSlider) findViewById(R.id.brightness_slider));
  15. }

这里我们可以看到通过LayoutInflater.from方法获取到了亮度进度条的View,然后添加到了QSPanel,QSPanel就是我们下拉状态栏看到的快捷设置面板,接下来我们可以看到创建了一个BrightnessController对象,对象的构造传入了(ImageView)findViewById(R.id.brightness_icon)和(ToggleSlider) findViewById(R.id.brightness_slider)这两个
控件对象,从这个对象的命名其实就可以看出来,亮度控制者,状态栏上亮度调节具体逻辑的实现全部是在这个类里面实现的,OK,我们可以在这里将CheckBox对象也传到这个类里面,代码如下
  1. public QSPanel(Context context, AttributeSet attrs) {
  2. super(context, attrs);
  3. ...........
  4. ...........
  5. mBrightnessController = new BrightnessController(getContext(),
  6. (ImageView) findViewById(R.id.brightness_icon),
  7. (ToggleSlider) findViewById(R.id.brightness_slider));
  8. mBrightnessController.setCheckBox((CheckBox)findViewById(R.id.brightness_check));
  9. }
上述步骤完成后,我们就可以在BrightnessController这个类里面来具体实现自动亮度控制的逻辑

frameworks/base/packages/SystemUI/src/com/android/systemui/settings/BrightnessController.java
  1. import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE;
  2. import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
  3. import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL;
  4. public void setCheckBox(CheckBox checkBox){
  5. mCheckBox = checkBox;
  6. mCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  7. @Override
  8. public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
  9. // TODO Auto-generated method stub
  10. if (arg1) {
  11. Settings.System.putInt(mContext.getContentResolver(), SCREEN_BRIGHTNESS_MODE,
  12. SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
  13. } else {
  14. Settings.System.putInt(mContext.getContentResolver(), SCREEN_BRIGHTNESS_MODE,
  15. SCREEN_BRIGHTNESS_MODE_MANUAL);
  16. }
  17. Intent intent = new Intent();
  18. intent.setAction("com.android.autobrightness");
  19. mContext.sendBroadcast(intent);
  20. }
  21. });
  22. }

通过改变SCREEN_BRIGHTNESS_MODE的值,便可以控制打开关闭自动亮度调节,此方法是我从Settings代码中找到的,亲试有效,这里发送了个广播是为了刷新设置的自动调节亮度界面显示问题,不用深究,这里还有一个问题,就是自动亮度调节的默认,或者在设置中改变了自动亮度调节按钮,怎么来刷新我们的CheckBox的显示,很好解决,BrightnessController这个类里面其实已经有了这个方法,叫做updateIcon方法,我们在这个类里可以直接设置CheckBox的显示

  1. private void updateIcon(boolean automatic) {
  2. if (mIcon != null) {
  3. mIcon.setImageResource(automatic && SHOW_AUTOMATIC_ICON ?
  4. com.android.systemui.R.drawable.ic_qs_brightness_auto_on :
  5. com.android.systemui.R.drawable.ic_qs_brightness_auto_off);
  6. }
  7. if (mCheckBox != null) {
  8. mCheckBox.setChecked(automatic);
  9. }
  10. }



这里还有最后一个要解决的问题,点击设置-显示-亮度,这里会弹出一个亮度控制条dialog,这个dialog其实也是quick_settings_brightness_dialog,所以我们需要把这个dialog中的CheckBox也传给BrightnessController,具体实现如下

frameworks/base/packages/SystemUI/src/com/android/systemui/settings/BrightnessDialog.java
  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. ...........
  5. final ImageView icon = (ImageView) findViewById(R.id.brightness_icon);
  6. final ToggleSlider slider = (ToggleSlider) findViewById(R.id.brightness_slider);
  7. mBrightnessController = new BrightnessController(this, icon, slider);
  8. CheckBox checkBox = (CheckBox) findViewById(R.id.brightness_check);
  9. mBrightnessController.setCheckBox(checkBox);
  10. }

OK,以上就是在状态栏添加自动亮度调节开关的全部过程,按照如上方法,就可以实现全部功能,可能需要注意的就是layout布局的调节,这方面需要大家自己来调整,大家看了文章,觉得不错就点个赞吧



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/642974
推荐阅读
相关标签
  

闽ICP备14008679号