Android screen 100% adaptation scheme

Keywords: Android

Android screen 100% adaptation scheme

1, Create a uutils utility class to calculate the scale of the control

Calculate the actual height of the equipment

  //Datum width height
    private static int STANDARD_WIDTH = 1080;
    private  static int STANDARD_HEIGHT = 1920;
    //Actual device resolution
    private float displayMetricsWidth;
    private float displayMetricsHeight;

    private UIUtil(Context context)
    {
        mContext = context;
        WindowManager windowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics displayMetrics = new DisplayMetrics();

        if (displayMetricsWidth == 0.0F || displayMetricsHeight == 0.0F) {
            windowManager.getDefaultDisplay().getMetrics(displayMetrics);
            //Obtain
            int systemBarHight = getSystemBarHeight();
            //Dealing with the problem of real width and height
            if (displayMetrics.widthPixels > displayMetrics.heightPixels) {//Horizontal screen
                this.displayMetricsWidth = (float) displayMetrics.heightPixels;
                this.displayMetricsHeight = (float) displayMetrics.widthPixels - systemBarHight;
            } else {//Vertical screen
                this.displayMetricsWidth = (float) displayMetrics.widthPixels;
                this.displayMetricsHeight = (float) displayMetrics.heightPixels - systemBarHight;
            }
        }

    }

Two ways to get the height of the status box

Method 1. Obtain by reflection

private static String DIME_CLAZZ = "com.android.internal.R$dimen";

 private int getSystemBarHeight()
    {

        return getValue(mContext,DIME_CLAZZ,"system_bar_height",48);
    }

private int getValue(Context context, String attrGroupClass,String attrName,int defValue)
    {
        try{
            Class clz = Class.forName(attrGroupClass);
            Object obj = clz.newInstance();
            Field field = clz.getField(attrName);
            int id = Integer.parseInt(field.get(obj).toString());
            return context.getResources().getDimensionPixelSize(id);
        }catch(Exception e){
            return defValue;
        }
    }

Method 2 provides us with a Resource class through the system

public int getStatusBarHeight() {

  int result = 0;

  int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");

  if (resourceId > 0) {

      result = getResources().getDimensionPixelSize(resourceId);

  }

  return result;

}

Calculate width height scaling

public float getHorizontalScaleValue()
    {
        return this.displayMetricsWidth / (float) STANDARD_WIDTH;
    }

    public float getVerticalScaleValue()
    {
        return this.displayMetricsHeight / (float) STANDARD_HEIGHT;
    }

2, Create a custom Layout extern RelativeLayout

In the custom layout onMeasure method, recalculate the scale of the child control.

@Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if(!isMeasure) {
            isMeasure = true;
            int childCount = this.getChildCount();
            float scaleX = UIUtil.getInstance(getContext()).getHorizontalScaleValue();
            float scaleY = UIUtil.getInstance(getContext()).getVerticalScaleValue();
            for (int i = 0; i < childCount; i++) {
                View child = this.getChildAt(i);
                LayoutParams layoutParams = (LayoutParams) child.getLayoutParams();
                layoutParams.width = (int) (layoutParams.width * scaleX);
                layoutParams.height = (int) (layoutParams.height * scaleY);
                layoutParams.leftMargin = (int) (layoutParams.leftMargin * scaleX);
                layoutParams.rightMargin = (int) (layoutParams.rightMargin * scaleX);
                layoutParams.topMargin = (int) (layoutParams.topMargin * scaleY);
                layoutParams.bottomMargin = (int) (layoutParams.bottomMargin * scaleY);
                child.setPadding((int)(child.getPaddingLeft()* scaleX),(int)(child.getPaddingTop()* scaleY),(int)(child.getRight()* scaleX),(int)(getPaddingBottom()* scaleY));
            }
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

 

 

Posted by BoarderLine on Thu, 09 Jan 2020 11:26:33 -0800