android原始碼分析之大字型

hfreeman2008發表於2015-12-21

大字型

在Settings—Accessibility有一個大字型(Large text)的功能,點選大字型的按鈕,系統所有的字型放大。

這個功能是怎麼實現的呢?

開啟原始檔:

./packages/apps/Settings/src/com/android/settings/accessibility/AccessibilitySettings.java

我們可以看到處理點選事件的程式碼:

private void handleToggleLargeTextPreferenceClick() {
    try {
        mCurConfig.fontScale = mToggleLargeTextPreference.isChecked() ? LARGE_FONT_SCALE : 1;
    ActivityManagerNative.getDefault().updatePersistentConfiguration(mCurConfig);
        } catch (RemoteException re) {
            /* ignore */
        }
}

從程式碼可以看出,其實只是呼叫了updatePersistentConfiguration來理新了系統的配置類mCurConfig,就實現了此功能。

我們再來看相關的關鍵程式碼:

字型放大功能是把字型放大到1.3倍

private static final float LARGE_FONT_SCALE = 1.3f;

再來看配置類Configuration:

import android.content.res.Configuration;

private final Configuration mCurConfig = new Configuration();

在更新介面的方法updateServicesPreferences()中:

// Large text.
try {                     mCurConfig.updateFrom(ActivityManagerNative.getDefault().getConfiguration());
} catch (RemoteException re) {
            /* ignore */
}
mToggleLargeTextPreference.setChecked(mCurConfig.fontScale == LARGE_FONT_SCALE);

關鍵類Configuration:

This class describes all device configuration information that can impact the resources the application retrieves. This includes both user-specified configuration options (locale and scaling) as well as device configurations (such as input modes, screen size and screen orientation).

You can acquire this object from Resources, using getConfiguration(). Thus, from an activity, you can get it by chaining the request with getResources():

Configuration config = getResources().getConfiguration();

這裡寫圖片描述

我們開啟原始碼:

./frameworks/base/core/java/android/content/res/Configuration.java

可以看到與字型相關的原始碼:

public float fontScale;
............
public static final int SCREENLAYOUT_SIZE_MASK = 0x0f;
public static final int SCREENLAYOUT_SIZE_UNDEFINED = 0x00;
public static final int SCREENLAYOUT_SIZE_SMALL = 0x01;
public static final int SCREENLAYOUT_SIZE_NORMAL = 0x02;   
public static final int SCREENLAYOUT_SIZE_LARGE = 0x03;    
public static final int SCREENLAYOUT_SIZE_XLARGE = 0x04;

在./frameworks/base/docs/html/guide/topics/resources/providing-resources.jd檔案中,我們也只能看到:

    <tr id="ScreenSizeQualifier">
      <td>Screen size</td>
      <td>
        <code>small</code><br/>
        <code>normal</code><br/>
        <code>large</code><br/>
        <code>xlarge</code>
      </td>
      <td>

字型大小

在Settings—Display—Font size有小,正常,大,超大四個字型。

我們可以看到這幾種字型的縮放比例:

./packages/apps/Settings/res/values/arrays.xml
<string-array name="entryvalues_font_size" translatable="false">
        <item>0.85</item>
        <item>1.0</item>
        <item>1.15</item>
        <item>1.30</item>
</string-array>

開啟程式碼:

packages/apps/Settings/src/com/android/settings/DisplaySettings.java

我們可以檢視與字型相關的程式碼:

import android.content.res.Configuration;

private final Configuration mCurConfig = new Configuration();

public void readFontSizePreference(ListPreference pref) {
        try {        mCurConfig.updateFrom(ActivityManagerNative.getDefault().getConfiguration());
        } catch (RemoteException e) {
            Log.w(TAG, "Unable to retrieve font size");
        }
…….
}


public void writeFontSizePreference(Object objValue) {
try {
    mCurConfig.fontScale = Float.parseFloat(objValue.toString());         ActivityManagerNative.getDefault().updatePersistentConfiguration(mCurConfig);
        } catch (RemoteException e) {
            Log.w(TAG, "Unable to save font size");
        }
}

相關文章