美文网首页
系统默认的字体缩放

系统默认的字体缩放

作者: Guuuj | 来源:发表于2019-10-16 09:45 被阅读0次

    系统默认的字体缩放大小

    // Configuration.java
    public float fontScale;
    
    public Configuration() {
        setToDefaults();
    }
    
    public void setToDefaults() {
        fontScale = 1; // 默认为1
        ...
    }
    

    系统提供字体缩放的设置项Settings.System.FONT_SCALE,可以通过修改这个设置项,使系统字体改变,默认不配置此值。

    // Settings.java
    public static final class System extends NameValueTable {
        private static final float DEFAULT_FONT_SCALE = 1.0f;
        ...    
        public static final String FONT_SCALE = "font_scale";
    }
    
    

    设置应用的无障碍功能中,可以通过"放大或缩小文字"修改此设置项,并定义了一组数值,作为参考选项

    <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>
    

    ActivityManagerService中监测这个设置项的改变,这个值的改变会更新系统配置

    private final class FontScaleSettingObserver extends ContentObserver {
        private final Uri mFontScaleUri = Settings.System.getUriFor(FONT_SCALE);
    
        public FontScaleSettingObserver() {
            super(mHandler);
            ContentResolver resolver = mContext.getContentResolver();
            resolver.registerContentObserver(mFontScaleUri, false, this, UserHandle.USER_ALL);
        }
    
        @Override
        public void onChange(boolean selfChange, Uri uri, @UserIdInt int userId) {
            if (mFontScaleUri.equals(uri)) {
                updateFontScaleIfNeeded(userId);
            }
        }
    }
    
    private void updateFontScaleIfNeeded(@UserIdInt int userId) {
        final float scaleFactor = Settings.System.getFloatForUser(mContext.getContentResolver(),
                FONT_SCALE, 1.0f, userId);
        if (mConfiguration.fontScale != scaleFactor) {
            final Configuration configuration = mWindowManager.computeNewConfiguration();
            configuration.fontScale = scaleFactor;
            synchronized (this) {
                updatePersistentConfigurationLocked(configuration, userId);
            }
        }
    }
    
    private void updatePersistentConfigurationLocked(Configuration values, @UserIdInt int userId) {
        final long origId = Binder.clearCallingIdentity();
        try {
            // 更新到当前配置mConfiguration中
            updateConfigurationLocked(values, null, false, true, userId, false /* deferResume */);
        } finally {
            Binder.restoreCallingIdentity(origId);
        }
    }
    
    
    

    使用adb测试系统屏幕字体大小的变化

    adb shell settings put system font_scale 1.30
    

    adb获取font_scale的值

    adb shell settings get system font_scale
    

    相关文章

      网友评论

          本文标题:系统默认的字体缩放

          本文链接:https://www.haomeiwen.com/subject/fsdymctx.html