Android 获取View的位置参数:x,y和相对父容器的偏移量以及中心点坐标

你好!

Android 利用代码动态添加Viw

首先,看一下动态添加View的方法:
在这里插入图片描述
然后,我们能看到,addView又五个重载方法,是:

addView(View child)   // 直接添你要添加的View
addView(View child, int index)   //你要添加的View 与 你所想添加到哪个布局层级
addView(View child, int width, int height)   // 你要添加的View与它的宽和高(单位,px)
addView(View view, ViewGroup.LayoutParams params)   // 你要添加的View与它的布局参数
addView(View child, int index, LayoutParams params)  // 你要添加的View 和 它所要添加的布局层级 以及 布局参数

使用方法:
一般动态添加View,最常用的就是LinearLayout和RelativeLayout了,然后,下面贴出使用方法:

		//LinearLayout 和 RelativeLayout都能用
		 ImageView img1 = new ImageView(Context context);
		 //可以给ImageView 设置一个图片, img1.setImageResource(R.mipmap.***);
         viewGroup.addView(img1);

下面是设置了LayoutParams的:

		//LinearLayout
		//new LayoutParams ,宽 180px 高 180px
 	    ImageView img = new ImageView(this);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(180,180);
        //给ImagView设置layoutParams
        img.setLayoutParams(layoutParams);
        //ImageView设置图片资源
        img.setImageResource(R.mipmap.ic_launcher);
        //添加至布局中
        viewGroup.addView(img);
        ImageView img2 = new ImageView(this);
        LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(180,180);
        img2.setLayoutParams(layoutParams2);
        img2.setImageResource(R.mipmap.sing_home_star3);
        viewGroup.addView(img2);
	//RelativeLayout
    ImageView img = new ImageView(this);
    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(180,180);
    img.setLayoutParams(layoutParams);
    img.setImageResource(R.mipmap.ic_launcher);
    viewGroup.addView(img);
    ImageView img2 = new ImageView(this);
    RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(180,180);
    img2.setLayoutParams(layoutParams2);
    img2.setImageResource(R.mipmap.sing_home_star3);
    viewGroup.addView(img2);

下面 是效果图,分别是LinearLayout的两种垂直或水平,然后就是RelatvieLayout,为了可以比对效果,我添加了两个ImageView:
在这里插入图片描述
下面是设置了偏移的动态添加View,(创建了一个宽高都是180px的ImageView,然后 ,让它距离顶部150px,距离左边也是150px):

 	     ImageView img = new ImageView(this);
        //new LayoutParams ,宽 180px 高 180px
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(180, 180);
        //给ImagView设置layoutParams
        img.setLayoutParams(layoutParams);
        //ImageView设置图片资源
        img.setImageResource(R.mipmap.ic_launcher);
        //添加至布局中
        ll.addView(img);
        //创建MarginLayoutParams,参数就填imgView的LayoutParams就行
        ViewGroup.MarginLayoutParams margin = new ViewGroup.MarginLayoutParams(img.getLayoutParams());
        /*
         * 给这个MarginLayoutParams添加偏移参数  四个参数(一般只需要顶部和左边就能确定View的位置,后两个参数添0即可):
         *       left:第一个参数,是View距离屏幕左边的距离
         *       top:第二个参数,是Veiw距离屏幕顶部的距离
         *       right:第三个参数,是View距离屏幕右边的距离
         *       bottom: 第三个参数,是View距离屏幕底部的距离
         * */
        margin.setMargins(150, 150, 0, 0);
        //创建一个LayoutParams,把MarginLayoutParams填入其中
        LinearLayout.LayoutParams layoutParams3 = new LinearLayout.LayoutParams(margin);
        //给ImageView设置进去
        img.setLayoutParams(layoutParams3);


        ImageView img2 = new ImageView(this);
        LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(180, 180);
        img2.setLayoutParams(layoutParams2);
        img2.setImageResource(R.mipmap.sing_home_star3);
        ll.addView(img2);

        ViewGroup.MarginLayoutParams margin2 = new ViewGroup.MarginLayoutParams(img2.getLayoutParams());
        margin2.setMargins(150, 150, 0, 0);
        LinearLayout.LayoutParams layoutParams4 = new LinearLayout.LayoutParams(margin2);
        img2.setLayoutParams(layoutParams4);

认真看的读者应该已经发现,添加View的时候父布局是LinearLayout或者RelativeLayout的代码的区别就是它的那个LayoutParms是哪个包下面,LinearLayout就用 LinearLayout.LayoutParams,RelativeLayout就用RelativeLayout.LayoutParams,由于代码多,就不贴这种废代码了,下面是效果图:
在这里插入图片描述
如果Ui给的是dp,那么下面有一个dp转px的一个的方法:

	//直接传一个float的单位为dp的值,然后方法会返回单位为px的数,直接设置即可
    public int dp2px(float dpValue) {
        final float scale = DongTaiViewActivity.this.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

关于代码中的屏幕适配

我们都知道,咱们Android碎片化比较严重,如果就生硬的按照这样只传px为单位,那么做完之后可以说跑一个手机布局就能一个样,所以,我们动态添加view也需要进行屏幕适配,这里的思路就是 ,你有View宽高的px,距离顶部的px和距离左边的px,(dp的话上面有个方法可以转为px),然后我们看一下设计图的尺寸标准,然后用标准尺寸除以屏幕分辨率,然后获取的那个倍数再乘以它的标准尺寸,得到的px就很适配了

		//获取屏幕分辨率
 	    DisplayMetrics dm = context.getResources().getDisplayMetrics();
        int screenWidth = dm.widthPixels;
        int screenHeight = dm.heightPixels;

然后接下来就按照基准分辨率来进行计算与此设备屏幕分辨率的倍数:

	 //基准屏幕宽
    private final int standardWith = 750;
    //基准屏幕高
    private final int standradHeight = 1334;
    //屏幕分辨率
    private int screenWidth;
    private int screenHeight;
    //倍数  -- 宽和高与设备分辨率之间的倍数
    private final float multipleWith;
    private final float multipleHeight;
    
    public void calculationB(){
		    screenWidth = dm.widthPixels;
        	screenHeight = dm.heightPixels;
       	    multipleWith = (float) standardWith / (float) screenWidth;
      	    multipleHeight = (float) standradHeight / (float) screenHeight;
	}

然后我们有了设计图基准分辨率与设备之间差的倍数,然后也有它的长宽,和偏移量,那么,接下来我们就该算他们适配后的px数(传进来一个宽和高,然后经过计算,将其返回,思路就是,判断标准宽是否大于屏幕宽,如果大于,直接用传的值除以他们之间的倍数,如果小于,就用2减去他们的倍数,然后用传的宽乘这个倍数,高也一样):

    public int[] getAdaptWH(int width, int height) {
        //适配完的宽度
        float sWidth = 0;
        //适配完的高度
        float sHeight = 0;
        if (standardWith > screenWidth && standradHeight > screenHeight) {
            sWidth = (float) width / multipleWith;
            sHeight = (float) height / multipleHeight;
        } else if (standardWith < screenWidth && standradHeight < screenHeight) {
            sWidth = (float) width * (2.0f - multipleWith);
            sHeight = (float) height * (2.0f - multipleHeight);
        } else if (standardWith < screenWidth && standradHeight > screenHeight) {
            sWidth = (float) width * (2.0f - multipleWith);
            sHeight = (float) height / multipleHeight;
        } else if (standardWith > screenWidth && standradHeight > screenHeight) {
            sWidth = (float) width / multipleWith;
            sHeight = (float) height * (2.0f - multipleHeight);
        }
        //standradWH[0] 是宽  standradWH[1] 是高
        int[] standradWH = {(int) sWidth, (int) sHeight};
        return standradWH;
    }

最后,奉上工具类

对这个屏幕适配我做了一个工具类,名字是PxAdaptionUtils,其中宽度分别可以传单个宽高和集合宽高,以及数组宽高:

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

import java.util.ArrayList;
import java.util.List;

public class PxAdaptionUtils {
    //基准屏幕宽
    private final int standardWith = 750;
    //基准屏幕高
    private final int standradHeight = 1334;
    //屏幕分辨率
    private int screenWidth;
    private int screenHeight;
    private Context context;
    private final float multipleWith;
    private final float multipleHeight;

    public PxAdaptionUtils(Context context) {
        this.context = context;
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        screenWidth = dm.widthPixels;
        screenHeight = dm.heightPixels;
        multipleWith = (float) standardWith / (float) screenWidth;
        multipleHeight = (float) standradHeight / (float) screenHeight;
    }

    public int[] getAdaptWH(int width, int height) {
        //适配完的宽度
        float sWidth = 0;
        //适配完的高度
        float sHeight = 0;
        if (standardWith > screenWidth && standradHeight > screenHeight) {
            sWidth = (float) width / multipleWith;
            sHeight = (float) height / multipleHeight;
        } else if (standardWith < screenWidth && standradHeight < screenHeight) {
            sWidth = (float) width * (2.0f - multipleWith);
            sHeight = (float) height * (2.0f - multipleHeight);
        } else if (standardWith < screenWidth && standradHeight > screenHeight) {
            sWidth = (float) width * (2.0f - multipleWith);
            sHeight = (float) height / multipleHeight;
        } else if (standardWith > screenWidth && standradHeight > screenHeight) {
            sWidth = (float) width / multipleWith;
            sHeight = (float) height * (2.0f - multipleHeight);
        }
        //standradWH[0] 是宽  standradWH[1] 是高
        int[] standradWH = {(int) sWidth, (int) sHeight};
        return standradWH;
    }

    public List<Float> getAdaptWH(List<Float> listWh) {
        //集合从第一个元素开始,每两个元素是一对,索引为偶数的是宽
        List<Float> adaptWHList = new ArrayList<>();
        //看你传来的集合是否成对,如果成对,最后一个值,舍弃
        boolean isPair = true;
        if (listWh.size() % 2 == 0) {
            isPair = true;
        } else {
            isPair = false;
        }

        for (int i = 0; i < listWh.size(); i++) {
            float element = listWh.get(i);
            if (isPair) {
                if (i % 2 == 0) {
                    float adaptWidth = getAdaptWidth(element);
                    adaptWHList.add(adaptWidth);
                } else {
                    float adaptHeight = getAdaptHeight(element);
                    adaptWHList.add(adaptHeight);
                }
            } else {
                if (i == (listWh.size() - 1)) {
                    break;
                }
                if (i % 2 == 0) {
                    float adaptWidth = getAdaptWidth(element);
                    adaptWHList.add(adaptWidth);
                } else {
                    float adaptHeight = getAdaptHeight(element);
                    adaptWHList.add(adaptHeight);
                }
            }
        }
        return adaptWHList;
    }

    public List<Float> getAdaptWH(int[] arrWh) {
        //集合从第一个元素开始,每两个元素是一对,索引为偶数的是宽
        List<Float> adaptWHList = new ArrayList<>();
        //看你传来的集合是否成对,如果成对,最后一个值,舍弃
        boolean isPair = true;
        if (arrWh.length % 2 == 0) {
            isPair = true;
        } else {
            isPair = false;
        }

        for (int i = 0; i < arrWh.length; i++) {
            int element = arrWh[i];
            if (isPair) {
                if (i % 2 == 0) {
                    float adaptWidth = getAdaptWidth(element);
                    adaptWHList.add(adaptWidth);
                } else {
                    float adaptHeight = getAdaptHeight(element);
                    adaptWHList.add(adaptHeight);
                }
            } else {
                if (i == (arrWh.length - 1)) {
                    break;
                }
                if (i % 2 == 0) {
                    float adaptWidth = getAdaptWidth(element);
                    adaptWHList.add(adaptWidth);
                } else {
                    float adaptHeight = getAdaptHeight(element);
                    adaptWHList.add(adaptHeight);
                }
            }
        }
        return adaptWHList;
    }

    private float getAdaptHeight(int height) {
        //适配玩的高度
        float sHeight = 0;
        if (standardWith > screenWidth && standradHeight > screenHeight) {
            sHeight = (float) height / multipleHeight;
        } else if (standardWith < screenWidth && standradHeight < screenHeight) {
            sHeight = (float) height * (2.0f - multipleHeight);
        } else if (standardWith < screenWidth && standradHeight > screenHeight) {
            sHeight = (float) height / multipleHeight;
        } else if (standardWith > screenWidth && standradHeight > screenHeight) {
            sHeight = (float) height * (2.0f - multipleHeight);
        }
        return sHeight;
    }

    private float getAdaptWidth(int width) {
        //适配完的宽度
        float sWidth = 0;
        if (standardWith > screenWidth && standradHeight > screenHeight) {
            sWidth = (float) width / multipleWith;
        } else if (standardWith < screenWidth && standradHeight < screenHeight) {
            sWidth = (float) width * (2.0f - multipleWith);
        } else if (standardWith < screenWidth && standradHeight > screenHeight) {
            sWidth = (float) width * (2.0f - multipleWith);
        } else if (standardWith > screenWidth && standradHeight > screenHeight) {
            sWidth = (float) width / multipleWith;
        }
        return sWidth;
    }

    private float getAdaptHeight(float height) {
        //适配玩的高度
        float sHeight = 0;
        if (standardWith > screenWidth && standradHeight > screenHeight) {
            sHeight = height / multipleHeight;
        } else if (standardWith < screenWidth && standradHeight < screenHeight) {
            sHeight = height * (2.0f - multipleHeight);
        } else if (standardWith < screenWidth && standradHeight > screenHeight) {
            sHeight = height / multipleHeight;
        } else if (standardWith > screenWidth && standradHeight > screenHeight) {
            sHeight = height * (2.0f - multipleHeight);
        }
        return sHeight;
    }

    private float getAdaptWidth(float width) {
        //适配完的宽度
        float sWidth = 0;
        if (standardWith > screenWidth && standradHeight > screenHeight) {
            sWidth = width / multipleWith;
        } else if (standardWith < screenWidth && standradHeight < screenHeight) {
            sWidth = width * (2.0f - multipleWith);
        } else if (standardWith < screenWidth && standradHeight > screenHeight) {
            sWidth = width * (2.0f - multipleWith);
        } else if (standardWith > screenWidth && standradHeight > screenHeight) {
            sWidth = width / multipleWith;
        }
        return sWidth;
    }
}

动态添加View的工具类,里面封装了一些常用的基础控件,如果需要用其他的,也可以自己添加:

/*
 * 参数  ! ViewTag 是你需要添加什么View
 * 参数 ! viewWith 与 viewHeight 是View的宽和高
 *
 *(addView方法上面有参数详解)
 *
 * */
public class DynamicAddViewPlus {
    //imageView
    public static final int TagImag = 0x001;
    //TextView
    public static final int TagTextView = 0x002;
    //Button
    public static final int TagButton = 0x003;
    //EditText
    public static final  int TagEditText = 0x004;
    //ImageButton
    public static final int TagImageButton = 0x005;
    //ToggleButton (状态开关按钮 )
    public static final int TagToggleButton = 0x006;
    //CheckBox
    public static final int TagCheckBox = 0x007;
    //TextClock
    public static  final int TagTextClock = 0x008;

    // 父布局是RelativeLayout
    public static final int TagFatherRelativeLayout = 0x001;
    // 父布局是LinearLayout
    public static final int TagFratherLinearLayout = 0x002;

    //View的宽高的自适应
    public static final int CWRAP_CONTENT = -2;
    //View的宽高铺满全屏
    public static final int CMATCH_PARENT = -1;

    // 把View添加至某个图层,默认是0
    private int layoutHierarchy = -1;

    private Context context;
    private PxAdaptionUtils pxAdaptationUtils;
    //是否指定添加View的图层
    private boolean isAppoint = false;

    public DynamicAddViewPlus(Context context) {
        this.context = context;
        pxAdaptationUtils = new PxAdaptionUtils(context);
    }

    /*
     * ViewTag :添加什么View
     * viewWith : view的宽       px
     * viewHeght : view的高      px
     * distanceTop : view与屏幕顶部的距离 px
     * distanceLeft : view与屏幕左边的距离 px
     * pVg : 父ViewGroup , 就是你要往哪个Layout里面添加,传他的id
     * activity : 你在哪个Activity里面添加,添this就行
     * fatherViewGroupTag : 你需要添加的Layout 是 LinearLayout ,还是 RelativeLayout ,直接填 DynamicAddViewPlus.TagFatherRelativeLayout | DynamicAddViewPlus.TagFratherLinearLayout
     *
     * */
    public View addView(int ViewTag, int viewWith, int viewHeght, int distanceTop, int distanceLeft, ViewGroup pVg, Activity activity, int fatherViewGroupTag, int layoutHierarchy) {
        isAppoint = true;
//        View view = null;
        this.layoutHierarchy = layoutHierarchy;
        switch (ViewTag) {
            case TagImag:
                ImageView img = dtAddImage(viewWith, viewHeght, distanceTop, distanceLeft, pVg, activity, fatherViewGroupTag);
                this.layoutHierarchy = -1;
                return img;
            case TagTextView:
                TextView tv = (TextView) dtAddTextView(viewWith, viewHeght, distanceTop, distanceLeft, pVg, activity, fatherViewGroupTag);
                this.layoutHierarchy = -1;
                return tv;
            case TagButton:
                Button button = dtAddButton(viewWith, viewHeght, distanceTop, distanceLeft, pVg, activity, fatherViewGroupTag);
                this.layoutHierarchy = -1;
                return button;
            case TagEditText:
                EditText editText = dtAddEditText(viewWith, viewHeght, distanceTop, distanceLeft, pVg, activity, fatherViewGroupTag);
                this.layoutHierarchy = -1;
                return editText;
            case TagImageButton:
                ImageButton imageButton = dtAddImageButton(viewWith, viewHeght, distanceTop, distanceLeft, pVg, activity, fatherViewGroupTag);
                return imageButton;
            case TagToggleButton:
                ToggleButton toggleButton = dtAddToggleButton(viewWith, viewHeght, distanceTop, distanceLeft, pVg, activity, fatherViewGroupTag);
                return toggleButton;
            case TagCheckBox:
                CheckBox checkBox = dtAddCheckBox(viewWith, viewHeght, distanceTop, distanceLeft, pVg, activity, fatherViewGroupTag);
                return checkBox;
         /*   case TagTextClock:
                TextClock textClock = dtAddTextClock(viewWith, viewHeght, distanceTop, distanceLeft, pVg, activity, fatherViewGroupTag);
                return textClock;*/
            default:
                return null;
        }
    }

    public View addView(int ViewTag, int viewWith, int viewHeght, int distanceTop, int distanceLeft, ViewGroup pVg, Activity activity, int fatherViewGroupTag) {
        isAppoint = false;
        switch (ViewTag) {
            case TagImag:
                ImageView img = dtAddImage(viewWith, viewHeght, distanceTop, distanceLeft, pVg, activity, fatherViewGroupTag);
                isAppoint = false;
                return img;
            case TagTextView:
                TextView tv = (TextView) dtAddTextView(viewWith, viewHeght, distanceTop, distanceLeft, pVg, activity, fatherViewGroupTag);
                isAppoint = false;
                return tv;
            case TagButton:
                Button button = dtAddButton(viewWith, viewHeght, distanceTop, distanceLeft, pVg, activity, fatherViewGroupTag);
                return button;
            case TagEditText:
                EditText editText = dtAddEditText(viewWith, viewHeght, distanceTop, distanceLeft, pVg, activity, fatherViewGroupTag);
                return editText;
            case TagImageButton:
                ImageButton imageButton = dtAddImageButton(viewWith, viewHeght, distanceTop, distanceLeft, pVg, activity, fatherViewGroupTag);
                return imageButton;
            case TagToggleButton:
                ToggleButton toggleButton = dtAddToggleButton(viewWith, viewHeght, distanceTop, distanceLeft, pVg, activity, fatherViewGroupTag);
                return toggleButton;
            case TagCheckBox:
                CheckBox checkBox = dtAddCheckBox(viewWith, viewHeght, distanceTop, distanceLeft, pVg, activity, fatherViewGroupTag);
                return checkBox;
         /*   case TagTextClock:
                TextClock textClock = dtAddTextClock(viewWith, viewHeght, distanceTop, distanceLeft, pVg, activity, fatherViewGroupTag);
                return textClock;*/
            default:
                return null;
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    private TextClock dtAddTextClock(int viewWith, int viewHeght, int distanceTop, int distanceLeft, ViewGroup pVg, Activity activity, int fatherViewGroupTag) {
        TextClock tClock = new TextClock(context);
        if (viewWith == CWRAP_CONTENT && viewHeght == CWRAP_CONTENT) {
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                tClock.setLayoutParams(layoutParams);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                tClock.setLayoutParams(layoutParams);
            }
        } else if (viewWith == CWRAP_CONTENT && viewHeght == CMATCH_PARENT) {
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                tClock.setLayoutParams(layoutParams);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
                tClock.setLayoutParams(layoutParams);
            }
        } else if (viewWith == CMATCH_PARENT && viewHeght == CMATCH_PARENT) {
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                tClock.setLayoutParams(layoutParams);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                tClock.setLayoutParams(layoutParams);
            }
        } else if (viewWith == CMATCH_PARENT && viewHeght == CWRAP_CONTENT) {
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                tClock.setLayoutParams(layoutParams);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                tClock.setLayoutParams(layoutParams);
            }
        } else {
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                int[] adaptWH = pxAdaptationUtils.getAdaptWH(viewWith, viewHeght);
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(adaptWH[0], adaptWH[1]);
                tClock.setLayoutParams(layoutParams);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                int[] adaptWH = pxAdaptationUtils.getAdaptWH(viewWith, viewHeght);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(adaptWH[0], adaptWH[1]);
                tClock.setLayoutParams(layoutParams);
            }
        }
        if (isAppoint) {
            pVg.addView(tClock, layoutHierarchy);
        } else {
            pVg.addView(tClock);
        }
        ViewGroup.MarginLayoutParams margin = new ViewGroup.MarginLayoutParams(tClock.getLayoutParams());
        int[] adaptWH1 = pxAdaptationUtils.getAdaptWH(distanceLeft, distanceTop);
        margin.setMargins(adaptWH1[0], adaptWH1[1], 0, 0);
        DisplayMetrics metric = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metric);
        if (TagFatherRelativeLayout == fatherViewGroupTag) {
            RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(margin);
            tClock.setLayoutParams(layoutParams2);
        } else if (TagFratherLinearLayout == fatherViewGroupTag) {
            LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(margin);
            tClock.setLayoutParams(layoutParams2);
        }
        return tClock;
    }

    private CheckBox dtAddCheckBox(int viewWith, int viewHeght, int distanceTop, int distanceLeft, ViewGroup pVg, Activity activity, int fatherViewGroupTag) {
        CheckBox cb = new CheckBox(context);
        if (viewWith == CWRAP_CONTENT && viewHeght == CWRAP_CONTENT) {
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                cb.setLayoutParams(layoutParams);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                cb.setLayoutParams(layoutParams);
            }
        } else if (viewWith == CWRAP_CONTENT && viewHeght == CMATCH_PARENT) {
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                cb.setLayoutParams(layoutParams);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
                cb.setLayoutParams(layoutParams);
            }
        } else if (viewWith == CMATCH_PARENT && viewHeght == CMATCH_PARENT) {
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                cb.setLayoutParams(layoutParams);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                cb.setLayoutParams(layoutParams);
            }
        } else if (viewWith == CMATCH_PARENT && viewHeght == CWRAP_CONTENT) {
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                cb.setLayoutParams(layoutParams);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                cb.setLayoutParams(layoutParams);
            }
        } else {
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                int[] adaptWH = pxAdaptationUtils.getAdaptWH(viewWith, viewHeght);
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(adaptWH[0], adaptWH[1]);
                cb.setLayoutParams(layoutParams);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                int[] adaptWH = pxAdaptationUtils.getAdaptWH(viewWith, viewHeght);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(adaptWH[0], adaptWH[1]);
                cb.setLayoutParams(layoutParams);
            }
        }
        if (isAppoint) {
            pVg.addView(cb, layoutHierarchy);
        } else {
            pVg.addView(cb);
        }
        ViewGroup.MarginLayoutParams margin = new ViewGroup.MarginLayoutParams(cb.getLayoutParams());
        int[] adaptWH1 = pxAdaptationUtils.getAdaptWH(distanceLeft, distanceTop);
        margin.setMargins(adaptWH1[0], adaptWH1[1], 0, 0);
        DisplayMetrics metric = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metric);
        if (TagFatherRelativeLayout == fatherViewGroupTag) {
            RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(margin);
            cb.setLayoutParams(layoutParams2);
        } else if (TagFratherLinearLayout == fatherViewGroupTag) {
            LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(margin);
            cb.setLayoutParams(layoutParams2);
        }
        return cb;
    }

    private ImageButton dtAddImageButton(int viewWith, int viewHeght, int distanceTop, int distanceLeft, ViewGroup pVg, Activity activity, int fatherViewGroupTag) {
        ImageButton imgBtn = new ImageButton(context);
        if (viewWith == CWRAP_CONTENT && viewHeght == CWRAP_CONTENT) {
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                imgBtn.setLayoutParams(layoutParams);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                imgBtn.setLayoutParams(layoutParams);
            }
        } else if (viewWith == CWRAP_CONTENT && viewHeght == CMATCH_PARENT) {
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                imgBtn.setLayoutParams(layoutParams);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
                imgBtn.setLayoutParams(layoutParams);
            }
        } else if (viewWith == CMATCH_PARENT && viewHeght == CMATCH_PARENT) {
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                imgBtn.setLayoutParams(layoutParams);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                imgBtn.setLayoutParams(layoutParams);
            }
        } else if (viewWith == CMATCH_PARENT && viewHeght == CWRAP_CONTENT) {
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                imgBtn.setLayoutParams(layoutParams);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                imgBtn.setLayoutParams(layoutParams);
            }
        } else {
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                int[] adaptWH = pxAdaptationUtils.getAdaptWH(viewWith, viewHeght);
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(adaptWH[0], adaptWH[1]);
                imgBtn.setLayoutParams(layoutParams);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                int[] adaptWH = pxAdaptationUtils.getAdaptWH(viewWith, viewHeght);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(adaptWH[0], adaptWH[1]);
                imgBtn.setLayoutParams(layoutParams);
            }
        }
        if (isAppoint) {
            pVg.addView(imgBtn, layoutHierarchy);
        } else {
            pVg.addView(imgBtn);
        }
        ViewGroup.MarginLayoutParams margin = new ViewGroup.MarginLayoutParams(imgBtn.getLayoutParams());
        int[] adaptWH1 = pxAdaptationUtils.getAdaptWH(distanceLeft, distanceTop);
        margin.setMargins(adaptWH1[0], adaptWH1[1], 0, 0);
        DisplayMetrics metric = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metric);
        if (TagFatherRelativeLayout == fatherViewGroupTag) {
            RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(margin);
            imgBtn.setLayoutParams(layoutParams2);
        } else if (TagFratherLinearLayout == fatherViewGroupTag) {
            LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(margin);
            imgBtn.setLayoutParams(layoutParams2);
        }
        return imgBtn;
    }

    private ToggleButton dtAddToggleButton(int viewWith, int viewHeght, int distanceTop, int distanceLeft, ViewGroup pVg, Activity activity, int fatherViewGroupTag) {
        ToggleButton tBtn = new ToggleButton(context);
        if (viewWith == CWRAP_CONTENT && viewHeght == CWRAP_CONTENT) {
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                tBtn.setLayoutParams(layoutParams);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                tBtn.setLayoutParams(layoutParams);
            }
        } else if (viewWith == CWRAP_CONTENT && viewHeght == CMATCH_PARENT) {
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                tBtn.setLayoutParams(layoutParams);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
                tBtn.setLayoutParams(layoutParams);
            }
        } else if (viewWith == CMATCH_PARENT && viewHeght == CMATCH_PARENT) {
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                tBtn.setLayoutParams(layoutParams);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                tBtn.setLayoutParams(layoutParams);
            }
        } else if (viewWith == CMATCH_PARENT && viewHeght == CWRAP_CONTENT) {
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                tBtn.setLayoutParams(layoutParams);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                tBtn.setLayoutParams(layoutParams);
            }
        } else {
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                int[] adaptWH = pxAdaptationUtils.getAdaptWH(viewWith, viewHeght);
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(adaptWH[0], adaptWH[1]);
                tBtn.setLayoutParams(layoutParams);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                int[] adaptWH = pxAdaptationUtils.getAdaptWH(viewWith, viewHeght);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(adaptWH[0], adaptWH[1]);
                tBtn.setLayoutParams(layoutParams);
            }
        }
        if (isAppoint) {
            pVg.addView(tBtn, layoutHierarchy);
        } else {
            pVg.addView(tBtn);
        }
        ViewGroup.MarginLayoutParams margin = new ViewGroup.MarginLayoutParams(tBtn.getLayoutParams());
        int[] adaptWH1 = pxAdaptationUtils.getAdaptWH(distanceLeft, distanceTop);
        margin.setMargins(adaptWH1[0], adaptWH1[1], 0, 0);
        DisplayMetrics metric = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metric);
        if (TagFatherRelativeLayout == fatherViewGroupTag) {
            RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(margin);
            tBtn.setLayoutParams(layoutParams2);
        } else if (TagFratherLinearLayout == fatherViewGroupTag) {
            LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(margin);
            tBtn.setLayoutParams(layoutParams2);
        }
        return tBtn;
    }

    private EditText dtAddEditText(int viewWith, int viewHeght, int distanceTop, int distanceLeft, ViewGroup pVg, Activity activity, int fatherViewGroupTag) {
        EditText editT = new EditText(context);
        if (viewWith == CWRAP_CONTENT && viewHeght == CWRAP_CONTENT) {
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                editT.setLayoutParams(layoutParams);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                editT.setLayoutParams(layoutParams);
            }
        } else if (viewWith == CWRAP_CONTENT && viewHeght == CMATCH_PARENT) {
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                editT.setLayoutParams(layoutParams);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
                editT.setLayoutParams(layoutParams);
            }
        } else if (viewWith == CMATCH_PARENT && viewHeght == CMATCH_PARENT) {
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                editT.setLayoutParams(layoutParams);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                editT.setLayoutParams(layoutParams);
            }
        } else if (viewWith == CMATCH_PARENT && viewHeght == CWRAP_CONTENT) {
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                editT.setLayoutParams(layoutParams);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                editT.setLayoutParams(layoutParams);
            }
        } else {
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                int[] adaptWH = pxAdaptationUtils.getAdaptWH(viewWith, viewHeght);
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(adaptWH[0], adaptWH[1]);
                editT.setLayoutParams(layoutParams);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                int[] adaptWH = pxAdaptationUtils.getAdaptWH(viewWith, viewHeght);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(adaptWH[0], adaptWH[1]);
                editT.setLayoutParams(layoutParams);
            }
        }
        if (isAppoint) {
            pVg.addView(editT, layoutHierarchy);
        } else {
            pVg.addView(editT);
        }
        ViewGroup.MarginLayoutParams margin = new ViewGroup.MarginLayoutParams(editT.getLayoutParams());
        int[] adaptWH1 = pxAdaptationUtils.getAdaptWH(distanceLeft, distanceTop);
        margin.setMargins(adaptWH1[0], adaptWH1[1], 0, 0);
        DisplayMetrics metric = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metric);
        if (TagFatherRelativeLayout == fatherViewGroupTag) {
            RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(margin);
            editT.setLayoutParams(layoutParams2);
        } else if (TagFratherLinearLayout == fatherViewGroupTag) {
            LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(margin);
            editT.setLayoutParams(layoutParams2);
        }
        return editT;
    }

    private Button dtAddButton(int viewWith, int viewHeght, int distanceTop, int distanceLeft, ViewGroup pVg, Activity activity, int fatherViewGroupTag) {
        Button btn = new Button(context);
        if (viewWith == CWRAP_CONTENT && viewHeght == CWRAP_CONTENT) {
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                btn.setLayoutParams(layoutParams);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                btn.setLayoutParams(layoutParams);
            }
        } else if (viewWith == CWRAP_CONTENT && viewHeght == CMATCH_PARENT) {
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                btn.setLayoutParams(layoutParams);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
                btn.setLayoutParams(layoutParams);
            }
        } else if (viewWith == CMATCH_PARENT && viewHeght == CMATCH_PARENT) {
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                btn.setLayoutParams(layoutParams);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                btn.setLayoutParams(layoutParams);
            }
        } else if (viewWith == CMATCH_PARENT && viewHeght == CWRAP_CONTENT) {
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                btn.setLayoutParams(layoutParams);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                btn.setLayoutParams(layoutParams);
            }
        } else {
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                int[] adaptWH = pxAdaptationUtils.getAdaptWH(viewWith, viewHeght);
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(adaptWH[0], adaptWH[1]);
                btn.setLayoutParams(layoutParams);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                int[] adaptWH = pxAdaptationUtils.getAdaptWH(viewWith, viewHeght);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(adaptWH[0], adaptWH[1]);
                btn.setLayoutParams(layoutParams);
            }
        }
        if (isAppoint) {
            pVg.addView(btn, layoutHierarchy);
        } else {
            pVg.addView(btn);
        }
        ViewGroup.MarginLayoutParams margin = new ViewGroup.MarginLayoutParams(btn.getLayoutParams());
        int[] adaptWH1 = pxAdaptationUtils.getAdaptWH(distanceLeft, distanceTop);
        margin.setMargins(adaptWH1[0], adaptWH1[1], 0, 0);
        DisplayMetrics metric = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metric);
        if (TagFatherRelativeLayout == fatherViewGroupTag) {
            RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(margin);
            btn.setLayoutParams(layoutParams2);
        } else if (TagFratherLinearLayout == fatherViewGroupTag) {
            LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(margin);
            btn.setLayoutParams(layoutParams2);
        }
        return btn;
    }

    private View dtAddTextView(int viewWith, int viewHeght, int distanceTop, int distanceLeft, ViewGroup pVg, Activity activity, int fatherViewGroupTag) {
        TextView tv = new TextView(context);
        if (viewWith == CWRAP_CONTENT && viewHeght == CWRAP_CONTENT) {
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                tv.setLayoutParams(layoutParams);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                tv.setLayoutParams(layoutParams);
            }
        } else if (viewWith == CWRAP_CONTENT && viewHeght == CMATCH_PARENT) {
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                tv.setLayoutParams(layoutParams);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
                tv.setLayoutParams(layoutParams);
            }
        } else if (viewWith == CMATCH_PARENT && viewHeght == CMATCH_PARENT) {
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                tv.setLayoutParams(layoutParams);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                tv.setLayoutParams(layoutParams);
            }
        } else if (viewWith == CMATCH_PARENT && viewHeght == CWRAP_CONTENT) {
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                tv.setLayoutParams(layoutParams);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                tv.setLayoutParams(layoutParams);
            }
        } else {
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                int[] adaptWH = pxAdaptationUtils.getAdaptWH(viewWith, viewHeght);
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(adaptWH[0], adaptWH[1]);
                tv.setLayoutParams(layoutParams);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                int[] adaptWH = pxAdaptationUtils.getAdaptWH(viewWith, viewHeght);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(adaptWH[0], adaptWH[1]);
                tv.setLayoutParams(layoutParams);
            }
        }
        if (isAppoint) {
            pVg.addView(tv, layoutHierarchy);
        } else {
            pVg.addView(tv);
        }
        ViewGroup.MarginLayoutParams margin = new ViewGroup.MarginLayoutParams(tv.getLayoutParams());
        int[] adaptWH1 = pxAdaptationUtils.getAdaptWH(distanceLeft, distanceTop);
        margin.setMargins(adaptWH1[0], adaptWH1[1], 0, 0);
        DisplayMetrics metric = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metric);
        if (TagFatherRelativeLayout == fatherViewGroupTag) {
            RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(margin);
            tv.setLayoutParams(layoutParams2);
        } else if (TagFratherLinearLayout == fatherViewGroupTag) {
            LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(margin);
            tv.setLayoutParams(layoutParams2);
        }
        return tv;
    }

    private ImageView dtAddImage(int viewWith, int viewHeght, int distanceTop, int distanceLeft, ViewGroup pVg, Activity activity, int fatherViewGroupTag) {
        ImageView img = new ImageView(context);

        if (viewWith == CWRAP_CONTENT && viewHeght == CWRAP_CONTENT) {
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                img.setLayoutParams(layoutParams);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                img.setLayoutParams(layoutParams);
            }
        } else if (viewWith == CWRAP_CONTENT && viewHeght == CMATCH_PARENT) {
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                img.setLayoutParams(layoutParams);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
                img.setLayoutParams(layoutParams);
            }
        } else if (viewWith == CMATCH_PARENT && viewHeght == CMATCH_PARENT) {
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                img.setLayoutParams(layoutParams);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                img.setLayoutParams(layoutParams);
            }
        } else if (viewWith == CMATCH_PARENT && viewHeght == CWRAP_CONTENT) {
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                img.setLayoutParams(layoutParams);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                img.setLayoutParams(layoutParams);
            }
        } else {
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                int[] adaptWH = pxAdaptationUtils.getAdaptWH(viewWith, viewHeght);
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(adaptWH[0], adaptWH[1]);
                img.setLayoutParams(layoutParams);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                int[] adaptWH = pxAdaptationUtils.getAdaptWH(viewWith, viewHeght);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(adaptWH[0], adaptWH[1]);
                img.setLayoutParams(layoutParams);
            }
        }
        if (isAppoint) {
            pVg.addView(img, layoutHierarchy);
        } else {
            pVg.addView(img);
        }
        ViewGroup.MarginLayoutParams margin = new ViewGroup.MarginLayoutParams(img.getLayoutParams());
        int[] adaptWH1 = pxAdaptationUtils.getAdaptWH(distanceLeft, distanceTop);
        margin.setMargins(adaptWH1[0], adaptWH1[1], 0, 0);
        DisplayMetrics metric = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metric);
        if (TagFatherRelativeLayout == fatherViewGroupTag) {
            RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(margin);
            img.setLayoutParams(layoutParams2);
        } else if (TagFratherLinearLayout == fatherViewGroupTag) {
            LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(margin);
            img.setLayoutParams(layoutParams2);
        }
        return img;
    }
}

class PxAdaptionUtils {
    //基准屏幕宽
    private final int standardWith = 750;
    //基准屏幕高
    private final int standradHeight = 1334;
    //屏幕分辨率
    private int screenWidth;
    private int screenHeight;
    private Context context;
    private final float multipleWith;
    private final float multipleHeight;

    public PxAdaptionUtils(Context context) {
        this.context = context;
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        screenWidth = dm.widthPixels;
        screenHeight = dm.heightPixels;
        multipleWith = (float) standardWith / (float) screenWidth;
        multipleHeight = (float) standradHeight / (float) screenHeight;
    }

    public int[] getAdaptWH(int width, int height) {
        //适配完的宽度
        float sWidth = 0;
        //适配玩的高度
        float sHeight = 0;
        if (standardWith > screenWidth && standradHeight > screenHeight) {
            sWidth = (float) width / multipleWith;
            sHeight = (float) height / multipleHeight;
        } else if (standardWith < screenWidth && standradHeight < screenHeight) {
            sWidth = (float) width * (2.0f - multipleWith);
            sHeight = (float) height * (2.0f - multipleHeight);
        } else if (standardWith < screenWidth && standradHeight > screenHeight) {
            sWidth = (float) width * (2.0f - multipleWith);
            sHeight = (float) height / multipleHeight;
        } else if (standardWith > screenWidth && standradHeight > screenHeight) {
            sWidth = (float) width / multipleWith;
            sHeight = (float) height * (2.0f - multipleHeight);
        }
        //standradWH[0] 是宽  standradWH[1] 是高
        int[] standradWH = {(int) sWidth, (int) sHeight};
        return standradWH;
    }

    public List<Float> getAdaptWH(List<Float> listWh) {
        //集合从第一个元素开始,每两个元素是一对,索引为偶数的是宽
        List<Float> adaptWHList = new ArrayList<>();
        //看你传来的集合是否成对,如果成对,最后一个值,舍弃
        boolean isPair = true;
        if (listWh.size() % 2 == 0) {
            isPair = true;
        } else {
            isPair = false;
        }

        for (int i = 0; i < listWh.size(); i++) {
            float element = listWh.get(i);
            if (isPair) {
                if (i % 2 == 0) {
                    float adaptWidth = getAdaptWidth(element);
                    adaptWHList.add(adaptWidth);
                } else {
                    float adaptHeight = getAdaptHeight(element);
                    adaptWHList.add(adaptHeight);
                }
            } else {
                if (i == (listWh.size() - 1)) {
                    break;
                }
                if (i % 2 == 0) {
                    float adaptWidth = getAdaptWidth(element);
                    adaptWHList.add(adaptWidth);
                } else {
                    float adaptHeight = getAdaptHeight(element);
                    adaptWHList.add(adaptHeight);
                }
            }
        }
        return adaptWHList;
    }

    public List<Float> getAdaptWH(int[] arrWh) {
        //集合从第一个元素开始,每两个元素是一对,索引为偶数的是宽
        List<Float> adaptWHList = new ArrayList<>();
        //看你传来的集合是否成对,如果成对,最后一个值,舍弃
        boolean isPair = true;
        if (arrWh.length % 2 == 0) {
            isPair = true;
        } else {
            isPair = false;
        }

        for (int i = 0; i < arrWh.length; i++) {
            int element = arrWh[i];
            if (isPair) {
                if (i % 2 == 0) {
                    float adaptWidth = getAdaptWidth(element);
                    adaptWHList.add(adaptWidth);
                } else {
                    float adaptHeight = getAdaptHeight(element);
                    adaptWHList.add(adaptHeight);
                }
            } else {
                if (i == (arrWh.length - 1)) {
                    break;
                }
                if (i % 2 == 0) {
                    float adaptWidth = getAdaptWidth(element);
                    adaptWHList.add(adaptWidth);
                } else {
                    float adaptHeight = getAdaptHeight(element);
                    adaptWHList.add(adaptHeight);
                }
            }
        }
        return adaptWHList;
    }

    private float getAdaptHeight(int height) {
        //适配玩的高度
        float sHeight = 0;
        if (standardWith > screenWidth && standradHeight > screenHeight) {
            sHeight = (float) height / multipleHeight;
        } else if (standardWith < screenWidth && standradHeight < screenHeight) {
            sHeight = (float) height * (2.0f - multipleHeight);
        } else if (standardWith < screenWidth && standradHeight > screenHeight) {
            sHeight = (float) height / multipleHeight;
        } else if (standardWith > screenWidth && standradHeight > screenHeight) {
            sHeight = (float) height * (2.0f - multipleHeight);
        }
        return sHeight;
    }

    private float getAdaptWidth(int width) {
        //适配完的宽度
        float sWidth = 0;
        if (standardWith > screenWidth && standradHeight > screenHeight) {
            sWidth = (float) width / multipleWith;
        } else if (standardWith < screenWidth && standradHeight < screenHeight) {
            sWidth = (float) width * (2.0f - multipleWith);
        } else if (standardWith < screenWidth && standradHeight > screenHeight) {
            sWidth = (float) width * (2.0f - multipleWith);
        } else if (standardWith > screenWidth && standradHeight > screenHeight) {
            sWidth = (float) width / multipleWith;
        }
        return sWidth;
    }

    private float getAdaptHeight(float height) {
        //适配玩的高度
        float sHeight = 0;
        if (standardWith > screenWidth && standradHeight > screenHeight) {
            sHeight = height / multipleHeight;
        } else if (standardWith < screenWidth && standradHeight < screenHeight) {
            sHeight = height * (2.0f - multipleHeight);
        } else if (standardWith < screenWidth && standradHeight > screenHeight) {
            sHeight = height / multipleHeight;
        } else if (standardWith > screenWidth && standradHeight > screenHeight) {
            sHeight = height * (2.0f - multipleHeight);
        }
        return sHeight;
    }

    private float getAdaptWidth(float width) {
        //适配完的宽度
        float sWidth = 0;
        if (standardWith > screenWidth && standradHeight > screenHeight) {
            sWidth = width / multipleWith;
        } else if (standardWith < screenWidth && standradHeight < screenHeight) {
            sWidth = width * (2.0f - multipleWith);
        } else if (standardWith < screenWidth && standradHeight > screenHeight) {
            sWidth = width * (2.0f - multipleWith);
        } else if (standardWith > screenWidth && standradHeight > screenHeight) {
            sWidth = width / multipleWith;
        }
        return sWidth;
    }

}

感谢观看

我是入梦,谢谢你的观看我的博客,如果有什么错误,请随时联系我,QQ:897589417

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐