Android 系统设置中显示设置之亮度调节篇
在上一篇博客中,我们学习了显示设置中休眠设置和屏保设置的基本内容。在这一篇中我们来学习设备亮度的调节,首先还是学习在DisplaySettings.xml增加了一个叫做BrightnessPreference的preference的选项用来控制亮度调节。
步骤1:当用户点击BrightnessPreference的时候,由BrightnessPreference做对应的处理(注:其实是发送了一个广播)
1 public class BrightnessPreference extends Preference { 2 3 public BrightnessPreference(Context context, AttributeSet attrs) { 4 super(context, attrs); 5 } 6 7 @Override 8 protected void onClick() { 9 //发送了一个显示亮度调节对话框的广播 10 Intent intent = new Intent(Intent.ACTION_SHOW_BRIGHTNESS_DIALOG); 11 getContext().sendBroadcastAsUser(intent, UserHandle.CURRENT_OR_SELF); 12 } 13 }
步骤2:对广播的注册并处理,有一个SettingsUI的类继承自SystemUI,该类负责监听上面的广播并进行处理,详见:
1 public class SettingsUI extends SystemUI { 2 private static final String TAG = "SettingsUI"; 3 private static final boolean DEBUG = false; 4 private final Handler mHandler = new Handler(); 5 private BrightnessDialog mBrightnessDialog; 6 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { 7 @Override 8 public void onReceive(Context context, Intent intent) { 9 String action = intent.getAction(); 10 if (action.equals(Intent.ACTION_SHOW_BRIGHTNESS_DIALOG)) { 11 if (DEBUG) Log.d(TAG, "showing brightness dialog"); 12 if (mBrightnessDialog == null) { 13 mBrightnessDialog = new BrightnessDialog(mContext); 14 mBrightnessDialog.setOnDismissListener(new DialogInterface.OnDismissListener() { 15 @Override 16 public void onDismiss(DialogInterface dialog) { 17 mBrightnessDialog = null;}}); 18 } 19 if (!mBrightnessDialog.isShowing()) { 20 //显示亮度调节的dialog 21 mBrightnessDialog.show(); } 22 } else { 23 Log.w(TAG, "unknown intent: " + intent);}}}; 24 25 public void start() { 26 //监听ACTION_SHOW_BRIGHTNESS_DIALOG广播 27 IntentFilter filter = new IntentFilter(); 28 filter.addAction(Intent.ACTION_SHOW_BRIGHTNESS_DIALOG); 29 mContext.registerReceiverAsUser(mIntentReceiver, UserHandle.ALL, filter, null, mHandler); 30 }
步骤3:上面是负责显示亮度调节的对话框,具体的设置需要查看BrightnessDialog,详见:
1 /** 2 * Create the brightness dialog and any resources that are used for the 3 * entire lifetime of the dialog. 4 */ 5 @Override 6 public void onCreate(Bundle savedInstanceState) { 7 super.onCreate(savedInstanceState); 8 //这里使用了Window,使我们的亮度调节的对话框能在全局界面显示 9 Window window = getWindow(); 10 window.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY); 11 window.getAttributes().privateFlags |= 12 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS; 13 window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); 14 window.requestFeature(Window.FEATURE_NO_TITLE); 15 //加载我们的对话框布局文件,其实就是一个ImageView(开关)和com.android.systemui.settings.ToggleSlider(拖动条) 16 setContentView(R.layout.quick_settings_brightness_dialog); 17 setCanceledOnTouchOutside(true); //触摸其他部位就隐藏该dialog 18 } 19 @Override 20 protected void onStart() { 21 super.onStart(); 22 //当我们点击或者拖动ToggleSlider都是有BrightnessController进行处理的 23 mBrightnessController = new BrightnessController(getContext(), 24 (ImageView) findViewById(R.id.brightness_icon), 25 (ToggleSlider) findViewById(R.id.brightness_slider)); 26 dismissBrightnessDialog(mBrightnessDialogLongTimeout); 27 mBrightnessController.addStateChangedCallback(this); 28 }
步骤4: BrightnessController详解
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。