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(); System1.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(); System1.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(); System1.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)); // System1.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 style) { try { if (view == null) { return; } if (style.containsKey(Style.fontSize)) { try { if (view instanceof TextView) { int fontSize = (Integer) style.get(Style.fontSize); System1.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(); } } }