Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> android 屏幕亮度調節方法詳解

android 屏幕亮度調節方法詳解

編輯:Android開發實例

屏幕亮度自動調節
主要是從Sensor分析之中分離出來分析LIGHT 光線感應器,因此就分析一下自動調節屏幕亮度(手機隨著光線的強度自我調節,也就是在亮的光線下屏幕自動調亮一些,暗的時候就自動調暗一些,省得光線對眼睛有刺激).....

    (本人從歷經挫折才大概了解流程),現在就開始講一下流程,如果有不對地方希望牛人指出來。。。

    先從Sttings入手吧,在diaplay中有屏幕亮度調節,有一個進度調,上面有一個checkbox(自動調節屏幕亮度的),

那麼我們當然去找settings下的文件開始看了,先找到
DiaplaySettings.java文件以及display_setting.xml文件,display_setting.xml裡面能找到相應的組件,但是 在DiaplaySettings.java文件中卻不能找到調用相應組件的代碼,

那麼我們再到settings中去找,能找到BrightnessPreference.java文件,沒錯這個文件就是自動調節亮度的文件,我們先看看代碼:
構造函數中有mAutomaticAvailable = context.getResources().getBoolean(com.android.internal.R.bool.config_automatic_brightness_available);
開始以為mAutomaticAvailabl的值(為true,在config.xml中)是判斷是否自動調節亮度,因為在我跟進到PowerManagerService.java中,在initInThread() 中
  mUseSoftwareAutoBrightness = resources.getBoolean(
                com.android.internal.R.bool.config_automatic_brightness_available);

        if (mUseSoftwareAutoBrightness) {
            mAutoBrightnessLevels = resources.getIntArray(
                    com.android.internal.R.array.config_autoBrightnessLevels);//得到自動化調節的值調用 getAutoBrightnessValue(int sensorValue, int[] values) 方法
            mLcdBacklightValues = resources.getIntArray(
                    com.android.internal.R.array.config_autoBrightnessLcdBacklightValues);
            mButtonBacklightValues = resources.getIntArray(
                    com.android.internal.R.array.config_autoBrightnessButtonBacklightValues);
            mKeyboardBacklightValues = resources.getIntArray(。。。。。
。。。。。。這裡看起來好像是當mUseSoftwareAutoBrightness為true時就取得config.xml文件config_autoBrightnessLevels的值,再去根據光線自動調節,但是mAutomaticAvailabl的值只是說明有沒有自動調節屏幕的功能(比如說你將config.xml文件的config_automatic_brightness_available的值改為false,編譯替換framework-res.apk那麼在屏幕亮度調節中將沒有自動調節的checkbox),這也能解釋構造函數一開始就初始化mAutomaticAvailabl的值。

接下來在BrightnessPreference.java文件中有onProgressChanged,onStartTrackingTouch(SeekBar seekBar),onStopTrackingTouch(SeekBar seekBar)方法,這些一看也能懂就不介紹了,在onBindDialogView(View view)方法中有
mCheckBox = (CheckBox)view.findViewById(R.id.automatic_mode);
        if (mAutomaticAvailable) {//當有自動調節功能
            mCheckBox.setOnCheckedChangeListener(this);//為checkbox添加監聽事件
            try {
                mOldAutomatic = Settings.System.getInt(getContext().getContentResolver(),
                        Settings.System.SCREEN_BRIGHTNESS_MODE);
。。。。。。
主要看public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        setMode(isChecked ? Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC
                : Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
        if (!isChecked) {
            setBrightness(mSeekBar.getProgress() + MINIMUM_BACKLIGHT);
        }
    }
這裡才是你選擇自動調節亮度的checkbox,如果選擇則setMode(Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);並將數據存放到數據庫中。

這裡是重點:這裡有個數據庫,當監測到數據庫中的值發生改變時也就是變為Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC時,就會跳到
powerManagerService.java的initInThread()中ContentResolver resolver = mContext.getContentResolver();
        Cursor settingsCursor = resolver.query(Settings.System.CONTENT_URI, null,
                "(" + Settings.System.NAME + "=?) or ("
                        + Settings.System.NAME + "=?) or ("
                        + Settings.System.NAME + "=?) or ("
                        + Settings.System.NAME + "=?) or ("
                        + Settings.System.NAME + "=?) or ("
                        + Settings.System.NAME + "=?) or ("
                        + Settings.System.NAME + "=?)",
                new String[]{STAY_ON_WHILE_PLUGGED_IN, SCREEN_OFF_TIMEOUT, DIM_SCREEN, XEC_DLS_CONTROL,
                        SCREEN_BRIGHTNESS_MODE, WINDOW_ANIMATION_SCALE, TRANSITION_ANIMATION_SCALE},
                null);
        mSettings = new ContentQueryMap(settingsCursor, Settings.System.NAME, true, mHandler);
        SettingsObserver settingsObserver = new SettingsObserver();
        mSettings.addObserver(settingsObserver);
數據庫中數據發生改變時觸發事件的發生。

在 powerManagerService.java的systemReady()(只在開機調用一次就不再調用)中
 if (mUseSoftwareAutoBrightness) {
            Log.i("frist","frist="+"aaaa");
            mLightSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
            enableLightSensor(true);
        }
而mUseSoftwareAutoBrightness是在initInThread()線程中取得了true值,那麼就到enableLightSensor(true);方法去看
 if (enable) {
                    mSensorManager.registerListener(mLightListener, mLightSensor,
                            SensorManager.SENSOR_DELAY_NORMAL);
                }
如果傳遞來的值為true也就是enable=true就監聽而且用匹配屏幕方向變化的感應SENSOR_DELAY_NORMAL。
再看mLightListener事件:SensorEventListener mLightListener = new SensorEventListener() {
        public void onSensorChanged(SensorEvent event) {
            synchronized (mLocks) {
                int value = (int)event.values[0];//從event裡面獲取當前數據
                Log.i("value","value="+value);       
                long milliseconds = SystemClock.elapsedRealtime();
                if (mDebugLightSensor) {
                    Slog.d(TAG, "onSensorChanged: light value: " + value);
                }
                mHandler.removeCallbacks(mAutoBrightnessTask);
                if (mLightSensorValue != value) {//mLightSensorValue是當前的屏幕亮度
                    if (mLightSensorValue == -1 ||
                            milliseconds < mLastScreenOnTime + mLightSensorWarmupTime) {
                        // process the value immediately if screen has just turned on
                        lightSensorChangedLocked(value);
                    } else {
                        // delay processing to debounce the sensor
                        mLightSensorPendingValue = value;//mLightSensorPendingValue是准備要設置的亮度
                        mHandler.postDelayed(mAutoBrightnessTask, LIGHT_SENSOR_DELAY);//到mAutoBrightnessTask線程去完成自動調節
                    }
                } else {
                    mLightSensorPendingValue = -1;
                }
            }
        }
當前屏幕亮度肯定不等與-1,所以做else中的mHandler.postDelayed(mAutoBrightnessTask, LIGHT_SENSOR_DELAY),那麼我們再到mAutoBrightnessTask
線程看:private Runnable mAutoBrightnessTask = new Runnable() {
        public void run() {
            synchronized (mLocks) {
                int value = (int)mLightSensorPendingValue;
                Log.i("mLightSensorPendingValue","mLightSensorPendingValue"+mLightSensorPendingValue);
                if (value >= 0) {
                    mLightSensorPendingValue = -1;
                    lightSensorChangedLocked(value);
                }
            }
        }
    };就是不斷的去調用lightSensorChangedLocked(value)
private void lightSensorChangedLocked(int value) {
。。。。。。。
if (mLightSensorValue != value) {
            mLightSensorValue = value;
            if ((mPowerState & BATTERY_LOW_BIT) == 0) {
                // Use light sensor value no matter it is in a dock or not.
                int lcdValue = getAutoBrightnessValue(
                        value,
                        mLcdBacklightValues);// 取得mLcdBacklightValues裡面的值
                int buttonValue = getAutoBrightnessValue(value, mButtonBacklightValues);
                int keyboardValue;
                if (mKeyboardVisible) {
                    keyboardValue = getAutoBrightnessValue(value, mKeyboardBacklightValues);
                } else {
                    keyboardValue = 0;
                }
。。。
if (mAutoBrightessEnabled && mScreenBrightnessOverride < 0) {//設置屏幕亮度
                    mScreenBrightness.setTargetLocked(lcdValue, AUTOBRIGHTNESS_ANIM_STEPS,
                            INITIAL_SCREEN_BRIGHTNESS, (int)mScreenBrightness.curValue);
                }
。。。。。
   }

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved