赞
踩
首先我们来看一下下拉状态栏亮度进度条的位置,进度条的布局位于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,位置可根据实际情况自己摆放
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="vertical"
- android:layout_gravity="center_vertical"
- >
- <CheckBox
- android:id="@+id/brightness_check"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:button="@null"
- android:background="@drawable/ic_checkbox_style"
- />
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:paddingTop="0dp"
- android:textSize="11dp"
- android:layout_gravity="center_horizontal"
- android:textColor="@color/gray_text_color"
- android:text="@string/auto"
- />
- </LinearLayout>
- <vector xmlns:android="http://schemas.android.com/apk/res/android"
- android:width="24dp"
- android:height="24dp"
- android:viewportWidth="24.0"
- android:viewportHeight="24.0">
- <path
- android:fillColor="#666666"
- 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"/>
- </vector>
- <vector xmlns:android="http://schemas.android.com/apk/res/android"
- android:width="24dp"
- android:height="24dp"
- android:viewportWidth="24.0"
- android:viewportHeight="24.0">
- <path
- android:fillColor="#56C0E5"
- 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"/>
- </vector>
- <?xml version="1.0" encoding="utf-8"?>
-
- <selector xmlns:android="http://schemas.android.com/apk/res/android">
-
- <item android:drawable="@drawable/ic_check_box_select" android:state_checked="true"/>
- <item android:drawable="@drawable/ic_check_box_normal" android:state_checked="false"/>
-
- </selector>
- public QSPanel(Context context, AttributeSet attrs) {
- super(context, attrs);
- mContext = context;
-
- setOrientation(VERTICAL);
-
- mBrightnessView = LayoutInflater.from(context).inflate(
- R.layout.quick_settings_brightness_dialog, this, false);
- addView(mBrightnessView);
-
- setupTileLayout();
-
- mFooter = new QSFooter(this, context);
- addView(mFooter.getView());
-
- updateResources();
-
- mBrightnessController = new BrightnessController(getContext(),
- (ImageView) findViewById(R.id.brightness_icon),
- (ToggleSlider) findViewById(R.id.brightness_slider));
-
- }
- public QSPanel(Context context, AttributeSet attrs) {
- super(context, attrs);
- ...........
- ...........
-
- mBrightnessController = new BrightnessController(getContext(),
- (ImageView) findViewById(R.id.brightness_icon),
- (ToggleSlider) findViewById(R.id.brightness_slider));
-
- mBrightnessController.setCheckBox((CheckBox)findViewById(R.id.brightness_check));
-
- }
上述步骤完成后,我们就可以在BrightnessController这个类里面来具体实现自动亮度控制的逻辑
- import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE;
- import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
- import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL;
-
- public void setCheckBox(CheckBox checkBox){
- mCheckBox = checkBox;
-
- mCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
-
- @Override
- public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
- // TODO Auto-generated method stub
- if (arg1) {
- Settings.System.putInt(mContext.getContentResolver(), SCREEN_BRIGHTNESS_MODE,
- SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
- } else {
- Settings.System.putInt(mContext.getContentResolver(), SCREEN_BRIGHTNESS_MODE,
- SCREEN_BRIGHTNESS_MODE_MANUAL);
- }
- Intent intent = new Intent();
- intent.setAction("com.android.autobrightness");
- mContext.sendBroadcast(intent);
- }
- });
- }
- private void updateIcon(boolean automatic) {
- if (mIcon != null) {
- mIcon.setImageResource(automatic && SHOW_AUTOMATIC_ICON ?
- com.android.systemui.R.drawable.ic_qs_brightness_auto_on :
- com.android.systemui.R.drawable.ic_qs_brightness_auto_off);
- }
-
- if (mCheckBox != null) {
- mCheckBox.setChecked(automatic);
- }
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
- ...........
-
- final ImageView icon = (ImageView) findViewById(R.id.brightness_icon);
- final ToggleSlider slider = (ToggleSlider) findViewById(R.id.brightness_slider);
- mBrightnessController = new BrightnessController(this, icon, slider);
- CheckBox checkBox = (CheckBox) findViewById(R.id.brightness_check);
- mBrightnessController.setCheckBox(checkBox);
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。