Android 屏幕适配

·  阅读 56

屏幕适配
1 基本概念
屏幕尺寸是对角线的长度,单位是英寸,一英寸等于2.54厘米

private static float sNoncompatDensity;// 系统的Density
private static float sNoncompatScaleDensity;// 系统的ScaledDensity

private static void setCustomDensity(Activity activity, final Application application){
    final DisplayMetrics appDisplayMetrics = application.getResources().getDisplayMetrics();

    if(sNoncompatDensity == 0){
        // 系统的Density
        sNoncompatDensity = appDisplayMetrics.density;
        // 系统的ScaledDensity
        sNoncompatScaleDensity = appDisplayMetrics.scaledDensity;
        // 监听在系统设置中切换字体
        application.registerComponentCallbacks(new ComponentCallbacks() {
            @Override
            public void onConfigurationChanged(@NonNull Configuration newConfig) {
                if(newConfig != null && newConfig.fontScale > 0){
                    sNoncompatScaleDensity = application.getResources().getDisplayMetrics().scaledDensity;
                }
            }

            @Override
            public void onLowMemory() {

            }
        });
    }
    // 此处以360dp的设计图作为例子
    final float targetDensity = appDisplayMetrics.widthPixels / 360;
    final float targetScaledDensity = targetDensity * (sNoncompatScaleDensity/sNoncompatDensity);
    final int targetDensityDpi = (int)(160 * targetDensity);

    appDisplayMetrics.density = targetDensity;
    appDisplayMetrics.scaledDensity = targetScaledDensity;
    appDisplayMetrics.densityDpi = targetDensityDpi;

    final DisplayMetrics activityDisplayMetrics = activity.getResources().getDisplayMetrics();
    activityDisplayMetrics.density = targetDensity;
    activityDisplayMetrics.scaledDensity = targetScaledDensity;
    activityDisplayMetrics.densityDpi = targetDensityDpi;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setCustomDensity(this,this.getApplication());
    setContentView(R.layout.activity_main);

}
复制代码

约束布局
Guideline 辅助线1
Guide_percent

Barrier 辅助线2
barrierDirection= end
Referenced_ids=“id1,id2”

Bias = 0.3 偏移 同一个方向有两个拉力

Group
Referenced_ids=“id1,id2”

Chains
CircleAngle
CircleRadius

Width_default
Width_perced

屏幕适配
1 Guideline
2 width = 0dp
Width_default = “spread”
Width_perced = “0.3”

比例 dimensionRatio

分类:
Android
标签:
分类:
Android
标签:
收藏成功!
已添加到「」, 点击更改