a
554325746@qq.com
2019-12-24 570a73851c26d810c2597596a8acc8a8d4cde211
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package com.basic.security.utils;
 
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
 
import com.basic.security.activity.WelcomeActivity;
import com.basic.security.base.BaseApplication;
import com.basic.security.model.Style;
 
import java.lang.reflect.Method;
import java.util.Map;
 
public class LayoutUtil {
    public static void heightSetMatchParent(View view) {
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
        view.setLayoutParams(layoutParams);
    }
 
    public static void setHeight(View view, int height) {
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        layoutParams.height = getHeightPixel(height);
        view.setLayoutParams(layoutParams);
    }
 
    public static void setHeight2(View view, int height) {
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        layoutParams.height = height;
        view.setLayoutParams(layoutParams);
    }
 
    public static int getRealHeight() {
        Activity activity = BaseApplication.getApplication().activity;
        if (activity == null) {
            activity = WelcomeActivity.activity;
        }
        Display display = activity.getWindowManager().getDefaultDisplay();
        int realWidth;
        int realHeight;
        if (Build.VERSION.SDK_INT >= 17) {
            //new pleasant way to get real metrics
            DisplayMetrics realMetrics = new DisplayMetrics();
            display.getRealMetrics(realMetrics);
            realWidth = realMetrics.widthPixels;
            realHeight = realMetrics.heightPixels;
        } else if (Build.VERSION.SDK_INT >= 14) {
            //reflection for this weird in-between time
            try {
                Method mGetRawH = Display.class.getMethod("getRawHeight");
                Method mGetRawW = Display.class.getMethod("getRawWidth");
                realWidth = (Integer) mGetRawW.invoke(display);
                realHeight = (Integer) mGetRawH.invoke(display);
            } catch (Exception e) {
                //this may not be 100% accurate, but it's all we've got
                realWidth = display.getWidth();
                realHeight = display.getHeight();
                System.out.println("LayoutUtil.getRealHeight" + e.getMessage());
            }
        } else {
            //This should be close, as lower API devices should not have window navigation bars
            realWidth = display.getWidth();
            realHeight = display.getHeight();
        }
        return realHeight;
    }
 
    public static int getRealWidth() {
        Activity activity = BaseApplication.getApplication().activity;
        if (activity == null) {
            activity = WelcomeActivity.activity;
        }
        Display display = activity.getWindowManager().getDefaultDisplay();
        int realWidth;
        int realHeight;
        if (Build.VERSION.SDK_INT >= 17) {
            //new pleasant way to get real metrics
            DisplayMetrics realMetrics = new DisplayMetrics();
            display.getRealMetrics(realMetrics);
            realWidth = realMetrics.widthPixels;
            realHeight = realMetrics.heightPixels;
        } else if (Build.VERSION.SDK_INT >= 14) {
            //reflection for this weird in-between time
            try {
                Method mGetRawH = Display.class.getMethod("getRawHeight");
                Method mGetRawW = Display.class.getMethod("getRawWidth");
                realWidth = (Integer) mGetRawW.invoke(display);
                realHeight = (Integer) mGetRawH.invoke(display);
            } catch (Exception e) {
                //this may not be 100% accurate, but it's all we've got
                realWidth = display.getWidth();
                realHeight = display.getHeight();
                System.out.println("LayoutUtil.getRealHeight" + e.getMessage());
            }
        } else {
            //This should be close, as lower API devices should not have window navigation bars
            realWidth = display.getWidth();
            realHeight = display.getHeight();
        }
        return realWidth;
    }
 
    public static int getRealWidth2() {
        Display display = BaseApplication.getApplication().activity.getWindowManager().getDefaultDisplay();
        int realWidth;
        int realHeight;
        if (Build.VERSION.SDK_INT >= 17) {
            //new pleasant way to get real metrics
            DisplayMetrics realMetrics = new DisplayMetrics();
            display.getRealMetrics(realMetrics);
            realWidth = realMetrics.widthPixels;
            realHeight = realMetrics.heightPixels;
        } else if (Build.VERSION.SDK_INT >= 14) {
            //reflection for this weird in-between time
            try {
                Method mGetRawH = Display.class.getMethod("getRawHeight");
                Method mGetRawW = Display.class.getMethod("getRawWidth");
                realWidth = (Integer) mGetRawW.invoke(display);
                realHeight = (Integer) mGetRawH.invoke(display);
            } catch (Exception e) {
                //this may not be 100% accurate, but it's all we've got
                realWidth = display.getWidth();
                realHeight = display.getHeight();
                System.out.println("LayoutUtil.getRealHeight" + e.getMessage());
            }
        } else {
            //This should be close, as lower API devices should not have window navigation bars
            realWidth = display.getWidth();
            realHeight = display.getHeight();
        }
        return realWidth;
    }
 
    public static int getHeightPixel(int height) {
        int toHeight = (int) (height * getRealHeight() * 1.0 / BaseApplication.getApplication().getResources().getDimension(RUtils.R_dimen_w800));
//        System.out.println("height=" + height + ", to height=" + toHeight);
        return toHeight;
    }
 
    //
//    public static int getHeight20() {
//        if (BaseApplication.getApplication().activity.dimenNotMatch()) {
//            return getHeightPixel(20);
//        } else {
//            return (int) BaseApplication.getApplication().getResources().getDimension(R.dimen.h20);
//        }
//    }
//
//    public static int getWidth20() {
//        if (BaseApplication.getApplication().activity.dimenNotMatch()) {
//            return getWidthPixel(20);
//        } else {
//            return (int) BaseApplication.getApplication().getResources().getDimension(R.dimen.h20);
//        }
//    }
    public static int getWidthPixel(int height) {
        return (int) (height * getRealWidth() * 1.0 / BaseApplication.getApplication().getResources().getDimension(RUtils.R_dimen_h1280));
    }
 
    public static int getHeight1(int height) {
//        if (BaseApplication.getApplication().activity.isPortrait()) {
//            return (int) (height * 1.0 / 1280 * getRealHeight());
//        } else {
        return (int) (height * 1.0 / 1280 * getRealWidth());
//        }
    }
 
    public static int getHeight2(int height) {
//        if (BaseApplication.getApplication().activity.isPortrait()) {
//            return (int) (height * 1.0 / 1280 * getRealHeight());
//        } else {
        return (int) (height * 1.0 / 1280 * 1600);
//        }
    }
 
    public static int getWidth1(int width) {
//        if (BaseApplication.getApplication().activity.isPortrait()) {
//            return (int) (width * 1.0 / 800 * getRealWidth());
//        } else {
        return (int) (width * 1.0 / 800 * getRealHeight());
//        }
    }
 
    public static int getWidth2(int width) {
//        if (BaseApplication.getApplication().activity.isPortrait()) {
//            return (int) (width * 1.0 / 800 * getRealWidth());
//        } else {
        return (int) (width * 1.0 / 800 * 900);
//        }
    }
 
    public static int getPixles(String pixelStr) {
        try {
            int pixels = Integer.parseInt(pixelStr.replaceAll("\\D+", ""));
            if (pixelStr.startsWith("h")) {
                return getHeight1(pixels);
            } else if (pixelStr.startsWith("w")) {
                return getWidth1(pixels);
            } else if (pixelStr.startsWith("sh")) {
                return getHeight2(pixels);
            } else if (pixelStr.startsWith("sw")) {
                return getWidth2(pixels);
            } else {
                return getHeight1(pixels);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
 
    public static void applyStyle(View view, Map<String, Object> style) {
        try {
            if (view == null) {
                return;
            }
            if (style.containsKey(Style.fontSize)) {
                try {
                    if (view instanceof TextView) {
                        int fontSize = (Integer) style.get(Style.fontSize);
                        System.out.println("LayoutUtil.applyStyle fontSize=" + fontSize);
//                        if (fontSize == 21) {
////                            float fontSize1 = BaseApplication.getApplication().activity.fragment_face_detail.sign_up_rule_field_label.getTextSize();
////                            ((TextView) view).setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize1);
//                        } else {
                        ((TextView) view).setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize);
//                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
            if (layoutParams == null) {
                return;
            }
            if (style.containsKey(Style.height)) {
                layoutParams.height = getHeight1((Integer) style.get(Style.height));
            }
            if (style.containsKey(Style.width)) {
                layoutParams.width = getWidth1((Integer) style.get(Style.width));
            }
            int paddingLeft = view.getPaddingLeft();
            int paddingRight = view.getPaddingRight();
            int paddingTop = view.getPaddingTop();
            int paddingBottom = view.getPaddingBottom();
            if (style.containsKey(Style.paddingLeft)) {
                paddingLeft = getWidth1((Integer) style.get(Style.paddingLeft));
            }
            if (style.containsKey(Style.paddingRight)) {
                paddingRight = getWidth1((Integer) style.get(Style.paddingRight));
            }
            if (style.containsKey(Style.paddingTop)) {
                paddingTop = getHeight1((Integer) style.get(Style.paddingTop));
            }
            if (style.containsKey(Style.paddingBottom)) {
                paddingBottom = getHeight1((Integer) style.get(Style.paddingBottom));
            }
            view.setPadding(getWidthPixel(paddingLeft), getHeightPixel(paddingTop), getWidthPixel(paddingRight), getHeightPixel(paddingBottom));
            if (layoutParams instanceof LinearLayout.LayoutParams) {
                LinearLayout.LayoutParams linearLayoutLayoutParams = (LinearLayout.LayoutParams) layoutParams;
                int marginLeft = linearLayoutLayoutParams.leftMargin;
                int marginRight = linearLayoutLayoutParams.rightMargin;
                int marginTop = linearLayoutLayoutParams.topMargin;
                int marginBottom = linearLayoutLayoutParams.bottomMargin;
                if (style.containsKey(Style.marginLeft)) {
                    marginLeft = getWidth1((Integer) style.get(Style.marginLeft));
                }
                if (style.containsKey(Style.marginRight)) {
                    marginRight = getWidth1((Integer) style.get(Style.marginRight));
                }
                if (style.containsKey(Style.marginTop)) {
                    marginTop = getHeight1((Integer) style.get(Style.marginTop));
                }
                if (style.containsKey(Style.marginBottom)) {
                    marginBottom = getHeight1((Integer) style.get(Style.marginBottom));
                }
                linearLayoutLayoutParams.leftMargin = marginLeft;
                linearLayoutLayoutParams.rightMargin = marginRight;
                linearLayoutLayoutParams.topMargin = marginTop;
                linearLayoutLayoutParams.bottomMargin = marginBottom;
            }
            if (layoutParams instanceof RelativeLayout.LayoutParams) {
                RelativeLayout.LayoutParams relativeLayoutLayoutParams = (RelativeLayout.LayoutParams) layoutParams;
                int marginLeft = relativeLayoutLayoutParams.leftMargin;
                int marginRight = relativeLayoutLayoutParams.rightMargin;
                int marginTop = relativeLayoutLayoutParams.topMargin;
                int marginBottom = relativeLayoutLayoutParams.bottomMargin;
                if (style.containsKey(Style.marginLeft)) {
                    marginLeft = getWidth1((Integer) style.get(Style.paddingBottom));
                }
                if (style.containsKey(Style.marginRight)) {
                    marginRight = getWidth1((Integer) style.get(Style.marginRight));
                }
                if (style.containsKey(Style.marginTop)) {
                    marginTop = getHeight1((Integer) style.get(Style.marginTop));
                }
                if (style.containsKey(Style.marginBottom)) {
                    marginBottom = getHeight1((Integer) style.get(Style.marginBottom));
                }
                relativeLayoutLayoutParams.leftMargin = marginLeft;
                relativeLayoutLayoutParams.rightMargin = marginRight;
                relativeLayoutLayoutParams.topMargin = marginTop;
                relativeLayoutLayoutParams.bottomMargin = marginBottom;
            }
            view.setLayoutParams(layoutParams);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public static void radioButton12(RadioButton radioButton) {
        try {
            Drawable d = radioButton.getCompoundDrawables()[0];
            int width = (int) BaseApplication.getApplication().activity.getResources().getDimension(ResolutionAdaptation.h12());
            d.setBounds(0, 0, width, width);
            radioButton.setCompoundDrawables(d, null, null, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public static void checkBox13(CheckBox radioButton) {
        try {
            Drawable d = radioButton.getCompoundDrawables()[0];
            int width = (int) BaseApplication.getApplication().activity.getResources().getDimension(ResolutionAdaptation.h13());
            d.setBounds(0, 0, width, width);
            radioButton.setCompoundDrawables(d, null, null, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public static void checkBox21(CheckBox radioButton) {
        try {
            Drawable d = radioButton.getCompoundDrawables()[0];
            int width = (int) BaseApplication.getApplication().activity.getResources().getDimension(ResolutionAdaptation.h21());
            d.setBounds(0, 0, width, width);
            radioButton.setCompoundDrawables(d, null, null, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public static void checkBox(CheckBox checkBox) {
        try {
            Drawable d = checkBox.getCompoundDrawables()[0];
            if (d != null) {
                d.setBounds(0, 0, (int) checkBox.getTextSize(), (int) checkBox.getTextSize());
                checkBox.setCompoundDrawables(d, null, null, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public static void radioButton(RadioButton radioButton) {
        try {
            Drawable d = radioButton.getCompoundDrawables()[0];
            if (d != null) {
                d.setBounds(0, 0, (int) radioButton.getTextSize(), (int) radioButton.getTextSize());
                radioButton.setCompoundDrawables(d, null, null, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}