码迷,mamicode.com
首页 > 移动开发 > 详细

Android 系统设置中显示设置之亮度调节篇

时间:2014-07-29 20:57:04      阅读:589      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   color   使用   os   文件   

 

  在上一篇博客中,我们学习了显示设置中休眠设置和屏保设置的基本内容。在这一篇中我们来学习设备亮度的调节,首先还是学习在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,详见:

bubuko.com,布布扣
 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     }
View Code

 

   步骤4: BrightnessController详解

 

Android 系统设置中显示设置之亮度调节篇,布布扣,bubuko.com

Android 系统设置中显示设置之亮度调节篇

标签:android   style   blog   http   color   使用   os   文件   

原文地址:http://www.cnblogs.com/yinhaojun/p/3876132.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!