Android- disable the system font scaling

Designers worked hard to make a draft design, UI developers worked hard to put a good tune, but the default font size Android users to modify the system's original design is likely to fail, become very ugly, consider your user population, because users do not want to change the default font size style aliasing lead to app, we can do this:

1. Font Use dp / dip instead of sp

dp / dip (device independent pixels) : an abstract unit of density-based screen. On the display of 160 dots per inch, 1dp = 1px. Different devices have different display, the hardware and related equipment.
sp (Scaled Pixels): mainly used fonts display a scale-independent pixels, and dp similar, but can be scaled according to the user's font size preference.

View TextView source code can be found:

/**
* Set the default text size to the given value, interpreted as "scaled
* pixel" units.  This size is adjusted based on the current density and
* user font size preference.
*
* @param size The scaled pixel size.
*
* @attr ref android.R.styleable#TextView_textSize
*/
@android.view.RemotableViewMethod
public void setTextSize(float size) {
    setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
}
/**
* Set the default text size to a given unit and value.  See {@link
* TypedValue} for the possible dimension units.
*
* @param unit The desired dimension unit.
* @param size The desired size in the given units.
*
* @attr ref android.R.styleable#TextView_textSize
*/
public void setTextSize(int unit, float size) {
    Context c = getContext();
    Resources r;
    if (c == null)
        r = Resources.getSystem();
    else
        r = c.getResources();
    setRawTextSize(TypedValue.applyDimension(
    unit, size, r.getDisplayMetrics()));
}
private void setRawTextSize(float size) {
    if (size != mTextPaint.getTextSize()) {
        mTextPaint.setTextSize(size);
        if (mLayout != null) {
            nullLayouts();
            requestLayout();
            invalidate();
        }
    }
}

TypedValue.applyDimension () source as follows:

public static float applyDimension(int unit, float value,
    DisplayMetrics metrics)
{
    switch (unit) {
        case COMPLEX_UNIT_PX:
            return value;
        case COMPLEX_UNIT_DIP:
            return value * metrics.density;
        case COMPLEX_UNIT_SP:
            return value * metrics.scaledDensity;
        case COMPLEX_UNIT_PT:
            return value * metrics.xdpi * (1.0f/72);
        case COMPLEX_UNIT_IN:
            return value * metrics.xdpi;
        case COMPLEX_UNIT_MM:
            return value * metrics.xdpi * (1.0f/25.4f);
        }
    return 0;
}

Sp conversion difference dp and in that only metrics.density and metrics.scaledDensity,

/**
* A scaling factor for fonts displayed on the display.  This is the same
* as {@link #density}, except that it may be adjusted in smaller
* increments at runtime based on a user preference for the font size.
*/
public float scaledDensity;

Thus, the difference dp and sp is multiplied by a scale.

2. Disable the system by rewriting method

Application can be rewritten in the following ways


@Override
public void onConfigurationChanged(Configuration newConfig) {
    if (newConfig.fontScale != 1)//非默认值
        getResources();
    super.onConfigurationChanged(newConfig);
}
@Override
public Resources getResources() {
    Resources res = super.getResources();
    if (res.getConfiguration().fontScale != 1) {//非默认值
        Configuration newConfig = new Configuration();
        newConfig.setToDefaults();//设置默认
        res.updateConfiguration(newConfig, res.getDisplayMetrics());
    }
    return res;
}

By rewriting method, the fontScale reset to default values. This non-invasive way, valid only for the current App.
It may incorporate a very low cost Android adaptation mode screen view

Welcome to love learning progress together a small partner additive group: 230,274,309. Share together, progress together! Less the water, dry goods and more! ! welcome everybody! ! ! (Convicted of diving into the group added)
Published 134 original articles · won praise 197 · views 410 000 +

Guess you like

Origin blog.csdn.net/u011733020/article/details/102515518