系统默认的字体缩放

系统默认的字体缩放大小

// 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
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 选择qi:是表达式 标签选择器 类选择器 属性选择器 继承属性: color,font,text-align,li...
    love2013阅读 2,456评论 0 11
  • CSS参考手册 一、初识CSS3 1.1 CSS是什么 CSS3在CSS2.1的基础上增加了很多强大的新功能。目前...
    没汁帅阅读 4,291评论 1 13
  • CSS要点记录 CSS 指层叠样式表 (Cascading Style Sheets) 多种样式时的优先级问题 数...
    Tony__Hu阅读 1,340评论 0 0
  • 一、CSS入门 1、css选择器 选择器的作用是“用于确定(选定)要进行样式设定的标签(元素)”。 有若干种形式的...
    宠辱不惊丶岁月静好阅读 1,733评论 0 6
  • 转载自:https://blog.csdn.net/a2241076850/article/details/529...
    小小程序员jh阅读 2,175评论 0 1

友情链接更多精彩内容