屏幕适配-自定义像素适配

屏幕适配-修改系统的Density,DensityDpi

1.自定义像素适配思路

直接使用设计稿的宽高,比如给定了ios尺寸比列720*1250px,直接使用px写xml

2.步骤
  • 获取屏幕宽高和设计图宽高的缩放比列,如10801920对应7201250
  • 自定义View继承四中常用布局,重写onMeasure方法,为所有子View的LayoutParams重新赋值
  • 布局中使用我们自定义的View容器直接按照设计图写XML
3.代码实现
  • 首先定义一个工具类,返回屏幕宽高和设计图宽高的缩放比列
package com.cyq.uidemo;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.WindowManager;

public class ScreenUtils {
    private static ScreenUtils utils;
    //设计图宽高
    private static final float STANDARD_WIDTH = 500;
    private static final float STANDARD_HEIGHT = 1280;
    //屏幕显示宽高
    private int mDisplayWidth;
    private int mDisplayHeight;

    private ScreenUtils(Context context) {
        if (mDisplayWidth == 0 || mDisplayHeight == 0) {
            WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            if (manager != null) {
                DisplayMetrics displayMetrics = new DisplayMetrics();
                manager.getDefaultDisplay().getMetrics(displayMetrics);
                if (displayMetrics.widthPixels > displayMetrics.heightPixels) {
                    //横屏
                    mDisplayWidth = displayMetrics.heightPixels;
                    mDisplayHeight = displayMetrics.widthPixels - getStatusBarHeight(context);
                } else {
                    mDisplayWidth = displayMetrics.widthPixels;
                    mDisplayHeight = displayMetrics.heightPixels;
                }

            }
        }
    }

    /**
     * 获取系统ActionBar高度
     * @param context
     * @return
     */
    public int getStatusBarHeight(Context context) {
        int resID = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resID > 0) {
            return context.getResources().getDimensionPixelOffset(resID);
        }
        return 0;
    }

    public static ScreenUtils getInstance(Context context) {
        if (utils == null) {
            utils = new ScreenUtils(context.getApplicationContext());
        }
        return utils;
    }

    /**
     * 获取水平方向的缩放比例
     *
     * @return
     */
    public float getHorizontalScale() {
        return mDisplayWidth / STANDARD_WIDTH;
    }

    /**
     * 获取竖直方向的缩放比列
     * @return
     */
    public float getVerticalScale() {
        return mDisplayHeight / STANDARD_HEIGHT;
    }
}
  • 自定义View继承集中常用的布局容器,例如继承RelativeLayout,重新赋值它的子View的LayoutParams中的宽高,margin,其他几种布局的事项基本差不多
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;

public class ScreenAdapterLayout extends RelativeLayout {
    private boolean flag;

    public ScreenAdapterLayout(Context context) {
        super(context);
    }

    public ScreenAdapterLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ScreenAdapterLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (!flag) {
            float scaleX = ScreenUtils.getInstance(getContext()).getHorizontalScale();
            float scaleY = ScreenUtils.getInstance(getContext()).getVerticalScale();

            int count = getChildCount();

            for (int i = 0; i < count; i++) {
                View child = getChildAt(i);
                LayoutParams params = (LayoutParams) child.getLayoutParams();
                params.width = (int) (params.width * scaleX);
                params.height = (int) (params.height * scaleY);
                params.leftMargin = (int) (params.leftMargin * scaleX);
                params.rightMargin = (int) (params.rightMargin * scaleX);
                params.topMargin = (int) (params.topMargin * scaleY);
                params.bottomMargin = (int) (params.bottomMargin * scaleY);

            }
            flag = true;
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

  • XML中直接使用设计稿的px值



    


你可能感兴趣的:(屏幕适配-自定义像素适配)