大字体

在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();

android源码分析之大字体_android

我们打开源码:

./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");
        }
}