溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Window及UI縮放工具WindowUtil

發(fā)布時間:2020-06-29 12:56:02 來源:網(wǎng)絡(luò) 閱讀:551 作者:樂無莜 欄目:移動開發(fā)
package com.example.util;

import java.lang.reflect.Field;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.WindowManager;
import android.widget.TextView;

import com.example.base.BaseApplication;

/**
 * Window及UI縮放工具
 *
 */
@SuppressWarnings("unused")
public class WindowUtil {
	/** UI設(shè)計的豎向高度,單位:px */
	private static final int UI_DESIGN_PORTRAIT_SIZE = 1800;
	/** UI設(shè)計的橫向高度,單位:px */
	private static final int UI_DESIGN_LANDSCAPE_SIZE = 1080;

	/** 自動縮放嚴格模式標(biāo)志 */
	private static final String AUTO_RESIZE_STRICT_TAG = "strict_mode";
	private static final String TAG = WindowUtil.class.getSimpleName();
	/** 狀態(tài)欄高度 */
	public static int STATUS_BAR_HEIGHT;
	/** 縮放比例:水平 */
	public static float SCALE_RATIO_HORIZONTAL;
	/** 縮放比例:垂直 */
	public static float SCALE_RATIO_VERTICAL;
	/** 縮放比例 */
	public static float SCALE_RATIO;
	/** 屏幕旋轉(zhuǎn)度 */
	public static int WINDOW_ROTATION;

	private static float SCREEN_DENSITY = 1.0F;
	static {
		computeScaleRatio();
		computeScreenDensity();
		computeWindowRotation();
	}

	/**
	 * View按UI設(shè)計大小等比縮放,重新計算view的寬高、邊距、文字大小
	 *
	 * @param view
	 * @return
	 */
	public static boolean resize(View view) {
		return resize(view, SCALE_RATIO_HORIZONTAL, SCALE_RATIO);
	}

	/**
	 * 遞歸重新計算View及其子View的寬高
	 *
	 * @param view
	 * @return
	 */
	public static boolean resizeRecursively(View view) {
		return resizeRecursively(view, SCALE_RATIO, SCALE_RATIO);
	}

	/**
	 * 重新計算view的寬高、邊距、文本大小<br>
	 * (其中寬高、文本大小按照相同縮放系數(shù);內(nèi)外邊距水平方向按照水平比例系數(shù),垂直方向按照垂直比例系數(shù))
	 *
	 * @param view
	 * @param horizontalRatio
	 * @param verticalRatio
	 * @return
	 */

	public static boolean resize(View view, float horizontalRatio, float verticalRatio) {
		if (view == null)
			return false;
		/* 重新計算寬高 */
		resizeWidthAndHeight(view, horizontalRatio, verticalRatio);
		/* 重新計算內(nèi)邊距 */
		repadding(view);
		/* 重新計算外邊距 */
		remargin(view);
		/* 重新計算文本大小 */
		if (view instanceof TextView)
			resizeText((TextView) view);
		return true;
	}

	/**
	 * 重新計算view的寬高、邊距、文本大小<br>
	 * (嚴格模式)
	 *
	 * @param view
	 * @param horizontalRatio
	 * @param verticalRatio
	 * @return
	 */

	public static boolean resizeStrictly(View view, float horizontalRatio, float verticalRatio) {
		if (view == null)
			return false;
		/* 重新計算寬高 */
		resizeWidthAndHeight(view, horizontalRatio, verticalRatio);
		/* 重新計算內(nèi)邊距 */
		repadding(view, horizontalRatio, verticalRatio);
		/* 重新計算外邊距 */
		remargin(view, horizontalRatio, verticalRatio);
		/* 重新計算文本大小 */
		if (view instanceof TextView)
			resizeText((TextView) view);
		return true;
	}

	/**
	 * 重新計算view的寬高
	 *
	 * @param view
	 * @param horizontalRatio
	 * @param verticalRatio
	 * @return
	 */

	public static boolean resizeWidthAndHeight(View view, float horizontalRatio, float verticalRatio) {
		if (view == null)
			return false;
		Object tag = view.getTag();
		if (tag instanceof String) {
			String tagString = (String) tag;
			if ("ignoreSize".equals(tagString)) {
				return true;
			}
		}
		LayoutParams params = view.getLayoutParams();
		if (params != null) {
			int width = params.width;
			int height = params.height;
			if (params.width != LayoutParams.MATCH_PARENT && params.width != LayoutParams.WRAP_CONTENT) {
				width = (int) (width * horizontalRatio);
				if (width > 1)
					params.width = width;
			}
			if (params.height != LayoutParams.MATCH_PARENT && params.height != LayoutParams.WRAP_CONTENT) {
				height = (int) (height * verticalRatio);
				if (height > 1)
					params.height = height;
			}
			view.setLayoutParams(params);
		}

		return true;
	}

	/**
	 * 重新計算view的Padding(非嚴格模式)
	 *
	 * @param view
	 * @return
	 */
	public static boolean repadding(View view) {
		return repadding(view, SCALE_RATIO_HORIZONTAL, SCALE_RATIO_VERTICAL);
	}

	/**
	 * 重新計算view的Padding(嚴格模式)
	 *
	 * @param view
	 * @return
	 */
	public static boolean repadding(View view, float horizontalRatio, float verticalRatio) {
		if (view == null)
			return false;
		view.setPadding(
		    (int) (view.getPaddingLeft() * horizontalRatio), (int) (view.getPaddingTop() * verticalRatio),
		    (int) (view.getPaddingRight() * horizontalRatio), (int) (view.getPaddingBottom() * verticalRatio));
		return true;
	}

	/**
	 * 重新計算view的Margin
	 *
	 * @param view
	 * @return
	 */
	public static void remargin(View view) {
		remargin(view, SCALE_RATIO_HORIZONTAL, SCALE_RATIO_VERTICAL);
	}

	/**
	 * 重新計算view的Margin
	 *
	 * @param view
	 * @return
	 */
	public static void remargin(View view, float horizontalRatio, float verticalRatio) {
		MarginLayoutParams marginParams = null;
		try {
			marginParams = (MarginLayoutParams) view.getLayoutParams();
		} catch (ClassCastException e) {
			return;
		}
		if (marginParams == null)
			return;
		int left = (int) (marginParams.leftMargin * horizontalRatio);
		int top = (int) (marginParams.topMargin * verticalRatio);
		int right = (int) (marginParams.rightMargin * horizontalRatio);
		int bottom = (int) (marginParams.bottomMargin * verticalRatio);
		marginParams.setMargins(left, top, right, bottom);
		view.setLayoutParams(marginParams);
	}

	/**
	 * 重新計算TextView中文本的大小
	 *
	 * @param view
	 * @return
	 */
	public static boolean resizeText(TextView view) {
		if (view == null)
			return false;
		Object tag = view.getTag();
		if (tag instanceof String) {
			String tagString = (String) tag;
			if ("ignoreSize".equals(tagString)) {
				return true;
			}
		}
		float textSize = view.getTextSize();
		float ratio = SCALE_RATIO;
		view.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize * ratio);
		return true;
	}

	/**
	 * 重新計算view的寬高(高度及寬度均按照水平縮放比例)
	 *
	 * @param view
	 * @return
	 */
	public static boolean resizeWithHorizontalRatio(View view) {
		return resize(view, SCALE_RATIO_HORIZONTAL, SCALE_RATIO_HORIZONTAL);
	}

	/**
	 * 重新計算view的寬高(高度按照垂直縮放比例,寬度按照水平縮放比例)
	 *
	 * @param view
	 * @return
	 */
	public static boolean resizeWithRespectiveRatio(View view) {
		return resize(view, SCALE_RATIO_HORIZONTAL, SCALE_RATIO_VERTICAL);
	}

	public static boolean resizeChildrenRecursively(View view) {
		if (view == null)
			return false;

		if (!(view instanceof ViewGroup))
			return true;
		ViewGroup group = ((ViewGroup) view);
		int childCount = group.getChildCount();
		View child = null;
		for (int i = 0; i < childCount; i++) {
			child = group.getChildAt(i);
			resizeRecursively(child);
		}
		return true;
	}

	/**
	 * 遞歸重新計算view的寬高(高度按照垂直縮放比例,寬度按照水平縮放比例)
	 *
	 * @param view
	 * @return
	 */
	public static boolean resizeRecursivelyWithRespectiveRatio(View view) {
		return resizeRecursively(view, SCALE_RATIO_HORIZONTAL, SCALE_RATIO_VERTICAL);
	}

	/**
	 * 遞歸重新計算view的寬高(高度和寬度均按照垂直縮放比例)
	 *
	 * @param view
	 * @return
	 */
	public static boolean resizeRecursivelyWithVerticalRatio(View view) {
		return resizeRecursively(view, SCALE_RATIO_VERTICAL, SCALE_RATIO_VERTICAL);
	}

	/**
	 * 遞歸重新計算view的寬高
	 *
	 * @param view
	 * @param horizontalRatio
	 *            水平縮放比例
	 * @param verticalRatio
	 *            垂直縮放比例
	 * @return
	 */
	private static boolean resizeRecursively(View view, float horizontalRatio, float verticalRatio) {
		if (view == null)
			return false;
		/* 是否為嚴格模式 */
		boolean strictMode = isStrictMode(view);
		/* 如果當(dāng)前View需要以嚴格模式縮放,自動將所有子孫View按照嚴格模式縮放 */
		if (strictMode)
			return resizeStrictRecursively(view, SCALE_RATIO, SCALE_RATIO);
		resize(view, horizontalRatio, verticalRatio);
		if (!(view instanceof ViewGroup))
			return true;
		ViewGroup group = ((ViewGroup) view);
		int childCount = group.getChildCount();
		View child = null;
		for (int i = 0; i < childCount; i++) {
			child = group.getChildAt(i);
			resizeRecursively(child, horizontalRatio, verticalRatio);
		}
		return true;
	}

	/**
	 * 遞歸重新計算view的寬高(嚴格模式)
	 *
	 * @param view
	 * @param horizontalRatio
	 *            水平縮放比例
	 * @param verticalRatio
	 *            垂直縮放比例
	 * @return
	 */
	private static boolean resizeStrictRecursively(View view, float horizontalRatio, float verticalRatio) {
		if (view == null)
			return false;
		resizeStrictly(view, horizontalRatio, verticalRatio);
		if (!(view instanceof ViewGroup))
			return true;
		ViewGroup group = ((ViewGroup) view);
		int childCount = group.getChildCount();
		View child = null;
		for (int i = 0; i < childCount; i++) {
			child = group.getChildAt(i);
			resizeStrictRecursively(child, horizontalRatio, verticalRatio);
		}
		return true;
	}

	/**
	 * 是否為嚴格縮放模式
	 *
	 * @param view
	 * @return
	 */
	private static boolean isStrictMode(View view) {
		boolean strictMode = false;
		Object tag = view.getTag();
		if (tag == null)
			return false;
		String tagString = String.valueOf(tag);
		if (AUTO_RESIZE_STRICT_TAG.equals(tagString))
			strictMode = true;
		return strictMode;
	}

	/**
	 * 根據(jù)屏幕寬度設(shè)置傳入View的寬度
	 *
	 * @param view
	 * @param designedWidthResId
	 *            UI設(shè)計的高度資源ID
	 * @return 是否成功設(shè)置
	 */
	public static boolean setWidth(View view, int designedWidthResId) {
		float designedWidth = BaseApplication.getAppContext().getResources().getDimension(designedWidthResId);
		LayoutParams params = null;
		if (view instanceof ViewGroup)
			params = view.getLayoutParams();
		if (params == null)
			params = ((View) (view.getParent())).getLayoutParams();
		if (params == null)
			return false;
		params.width = (int) (designedWidth * SCALE_RATIO_HORIZONTAL);
		view.setLayoutParams(params);
		return true;
	}

	/**
	 * 根據(jù)屏幕寬度設(shè)置傳入View的高度
	 *
	 * @param view
	 * @param designedHeightResId
	 *            UI設(shè)計的高度資源ID(以像素為單位)
	 * @return 是否成功設(shè)置
	 */
	public static boolean setHeight(View view, int designedHeightResId) {
		if (view == null)
			return false;
		float designedHeight = BaseApplication.getAppContext().getResources().getDimension(designedHeightResId);
		LayoutParams params = null;
		if (view instanceof ViewGroup)
			params = view.getLayoutParams();
		if (params == null)
			params = ((View) (view.getParent())).getLayoutParams();
		if (params == null)
			return false;
		params.height = (int) (designedHeight * SCALE_RATIO_VERTICAL);
		view.setLayoutParams(params);
		return true;
	}

	/**
	 * 根據(jù)屏幕寬高設(shè)置傳入View的寬高(按照寬度比例等比縮放)
	 *
	 * @param view
	 * @param designedWidthResId
	 *            UI設(shè)計的寬度資源ID(以像素為單位)
	 * @param designedHeightResId
	 *            UI設(shè)計的高度資源ID(以像素為單位)
	 * @return 是否成功設(shè)置
	 */
	public static boolean setSize(View view, int designedWidthResId, int designedHeightResId) {
		float designedWidth = BaseApplication.getAppContext().getResources().getDimension(designedWidthResId);
		float designedHeight = BaseApplication.getAppContext().getResources().getDimension(designedHeightResId);
		LayoutParams params = null;
		if (view instanceof ViewGroup)
			params = view.getLayoutParams();
		if (params == null)
			params = ((View) (view.getParent())).getLayoutParams();
		if (params == null)
			return false;
		params.width = (int) (designedWidth * SCALE_RATIO_HORIZONTAL);
		params.height = (int) (designedHeight * SCALE_RATIO_HORIZONTAL);
		view.setLayoutParams(params);
		return true;
	}

	/**
	 * 根據(jù)屏幕寬度設(shè)置傳入View的高度
	 *
	 * @param view
	 * @param height
	 *            像素值
	 *
	 * @return 是否成功設(shè)置
	 */
	public static boolean setViewHeight(View view, int height) {
		LayoutParams params = null;
		if (view instanceof ViewGroup)
			params = view.getLayoutParams();
		if (params == null)
			params = ((View) (view.getParent())).getLayoutParams();
		if (params == null)
			return false;
		params.height = height;
		view.setLayoutParams(params);
		return true;
	}

	/**
	 * 設(shè)置視圖寬高
	 *
	 * @param view
	 * @param width
	 * @param height
	 * @return
	 */
	public static boolean setViewSize(View view, int width, int height) {
		LayoutParams params = null;
		if (view instanceof ViewGroup)
			params = view.getLayoutParams();
		if (params == null)
			params = ((View) (view.getParent())).getLayoutParams();
		if (params == null)
			return false;
		params.width = width;
		params.height = height;
		view.setLayoutParams(params);
		return true;
	}

	/**
	 * 根據(jù)屏幕寬度設(shè)置傳入TextView的文本大小
	 *
	 * @param view
	 * @param designedTextSizeResId
	 *            UI設(shè)計的文本大小資源ID(以像素為單位)
	 * @return 是否成功設(shè)置
	 */
	public static boolean setTextSize(TextView view, int designedTextSizeResId) {
		if (view == null)
			return false;
		float designedSize = BaseApplication.getAppContext().getResources().getDimension(designedTextSizeResId);
		float size = designedSize * SCALE_RATIO_HORIZONTAL;
		view.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
		return true;
	}

	/**
	 * 設(shè)置View的外邊距(像素值)
	 *
	 * @param view
	 * @param left
	 * @param top
	 * @param right
	 * @param bottom
	 * @return
	 */
	public static boolean setMargin(View view, int left, int top, int right, int bottom) {
		MarginLayoutParams marginParams = null;
		try {
			marginParams = (MarginLayoutParams) view.getLayoutParams();
		} catch (ClassCastException e) {
			return false;
		}
		if (marginParams == null)
			return false;
		marginParams.leftMargin = left;
		marginParams.topMargin = top;
		marginParams.rightMargin = right;
		marginParams.bottomMargin = bottom;
		view.setLayoutParams(marginParams);
		return true;
	}

	/**
	 * 設(shè)置View的頂部外邊距(像素值)
	 *
	 * @param view
	 * @return
	 */
	public static boolean setMarginTop(View view, int marginTop) {
		MarginLayoutParams marginParams = null;
		try {
			marginParams = (MarginLayoutParams) view.getLayoutParams();
		} catch (ClassCastException e) {
			return false;
		}
		if (marginParams == null)
			return false;
		marginParams.topMargin = marginTop;
		view.setLayoutParams(marginParams);
		return true;
	}

	/**
	 * 設(shè)置View的左側(cè)外邊距(像素值)
	 *
	 * @param view
	 * @return
	 */
	public static boolean setMarginLeft(View view, int marginLeft) {
		MarginLayoutParams marginParams = null;
		try {
			marginParams = (MarginLayoutParams) view.getLayoutParams();
		} catch (ClassCastException e) {
			return false;
		}
		if (marginParams == null)
			return false;
		marginParams.leftMargin = marginLeft;
		view.setLayoutParams(marginParams);
		return true;
	}

	/**
	 * 設(shè)置View的右側(cè)外邊距(像素值)
	 *
	 * @param view
	 * @return
	 */
	public static boolean setMarginRight(View view, int marginRight) {
		MarginLayoutParams marginParams = null;
		try {
			marginParams = (MarginLayoutParams) view.getLayoutParams();
		} catch (ClassCastException e) {
			return false;
		}
		if (marginParams == null)
			return false;
		marginParams.rightMargin = marginRight;
		view.setLayoutParams(marginParams);
		return true;
	}

	/**
	 * 設(shè)置View的底部外邊距(像素值)
	 *
	 * @param view
	 * @return
	 */
	public static boolean setMarginBottom(View view, int marginBottom) {
		MarginLayoutParams marginParams = null;
		try {
			marginParams = (MarginLayoutParams) view.getLayoutParams();
		} catch (ClassCastException e) {
			return false;
		}
		if (marginParams == null)
			return false;
		marginParams.bottomMargin = marginBottom;
		view.setLayoutParams(marginParams);
		return true;
	}

	/**
	 * 設(shè)置View的內(nèi)邊距(像素值)
	 *
	 * @param view
	 * @return
	 */
	public static boolean setPadding(View view, int left, int top, int right, int bottom) {
		if (view == null)
			return false;
		view.setPadding(left, top, right, bottom);
		return true;
	}

	/**
	 * 設(shè)置View的頂部內(nèi)邊距(像素值)
	 *
	 * @param view
	 * @return
	 */
	public static boolean setPaddingTop(View view, int top) {
		if (view == null)
			return false;
		view.setPadding(view.getPaddingLeft(), top, view.getPaddingRight(), view.getPaddingBottom());
		return true;
	}

	/**
	 * 設(shè)置View的左側(cè)內(nèi)邊距(像素值)
	 *
	 * @param view
	 * @return
	 */
	public static boolean setPaddingLeft(View view, int paddingLeft) {
		if (view == null)
			return false;
		view.setPadding(paddingLeft, view.getPaddingTop(), view.getPaddingRight(), view.getPaddingBottom());
		return true;
	}

	/**
	 * 設(shè)置View的右側(cè)內(nèi)邊距(像素值)
	 *
	 * @param view
	 * @return
	 */
	public static boolean setPaddingRight(View view, int paddingRight) {
		if (view == null)
			return false;
		view.setPadding(view.getPaddingLeft(), view.getPaddingTop(), paddingRight, view.getPaddingBottom());
		return true;
	}

	/**
	 * 設(shè)置View的底部內(nèi)邊距(像素值)
	 *
	 * @param view
	 * @return
	 */
	public static boolean setPaddingBottom(View view, int paddingBottom) {
		if (view == null)
			return false;
		view.setPadding(view.getPaddingLeft(), view.getPaddingTop(), view.getPaddingRight(), paddingBottom);
		return true;
	}

	/**
	 * 獲取View的寬度(像素值)
	 *
	 * @param view
	 * @return
	 */
	public static int getWidth(View view) {
		LayoutParams params = view.getLayoutParams();
		if (params == null)
			return 0;
		return params.width;
	}

	/**
	 * 獲取View的高度(像素值)
	 *
	 * @param view
	 * @return
	 */
	public static int getHeight(View view) {
		LayoutParams params = view.getLayoutParams();
		if (params == null)
			return 0;
		return params.height;
	}

	/**
	 * 獲取高度值(像素)(按照寬度比例縮放)
	 *
	 * @return
	 */
	public static float getHorizontalScaledDimen(int heightResId) {
		return BaseApplication.getAppContext().getResources().getDimension(heightResId) * SCALE_RATIO_HORIZONTAL;
	}

	/**
	 * 獲取高度值(像素)(按照高度比例縮放)
	 *
	 * @return
	 */
	public static float getVerticalScaledDimen(int heightResId) {
		return BaseApplication.getAppContext().getResources().getDimension(heightResId) * SCALE_RATIO_VERTICAL;
	}

	/**
	 * 獲取屏幕寬度(像素)
	 *
	 * @return
	 */
	public static int getWindowWidth() {
		DisplayMetrics dm = new DisplayMetrics();
		dm = BaseApplication.getAppContext().getResources().getDisplayMetrics();
		if (dm == null)
			return 0;
		return dm.widthPixels;
	}

	/**
	 * 獲取屏幕高度(像素)
	 *
	 * @return
	 */
	public static int getWindowHeight() {
		DisplayMetrics dm = new DisplayMetrics();
		dm = BaseApplication.getAppContext().getResources().getDisplayMetrics();
		if (dm == null)
			return 0;
		return dm.heightPixels;
	}

	/**
	 * 計算資源文件中定義的尺寸像素值
	 *
	 * @param resId
	 *            dimen.xml中定義的資源ID
	 * @return
	 */
	public static float computeDimen(int resId) {
		return BaseApplication.getAppContext().getResources().getDimension(resId);
	}

	/**
	 * 計算資源文件中定義的尺寸像素值,并乘以縮放系數(shù)ratio
	 *
	 * @return
	 */
	public static float computeScaledDimen(int resId, float ratio) {
		return computeDimen(resId) * ratio;
	}

	/**
	 * 重新計算尺寸像素值,并乘以縮放系數(shù)ratio
	 *
	 * @return
	 */
	public static int computeScaledSize(int size) {
		return (int) (size * SCALE_RATIO);
	}

	/**
	 * 重新計算尺寸像素值,并乘以縮放系數(shù)ratio
	 *
	 * @return
	 */
	public static int computeScaledSize(float size) {
		return (int) (size * SCALE_RATIO);
	}

	/**
	 * 計算資源文件中定義的尺寸像素值,并乘以垂直縮放系數(shù)
	 *
	 * @param resId
	 *            UI設(shè)計的大小資源ID(以像素為單位)
	 * @return
	 */
	public static float computeScaledDimenByVerticalRatio(int resId) {
		return computeScaledDimen(resId, SCALE_RATIO_VERTICAL);
	}

	/**
	 * 計算資源文件中定義的尺寸像素值,并乘以水平縮放系數(shù)
	 *
	 * @param resId
	 *            UI設(shè)計的大小資源ID(以像素為單位)
	 * @return
	 */
	public static float computeScaledDimenByHorizontalRatio(int resId) {
		return computeScaledDimen(resId, SCALE_RATIO_HORIZONTAL);
	}

	/**
	 * 計算屏幕密度
	 */
	public static void computeScreenDensity() {
		DisplayMetrics dm = new DisplayMetrics();
		dm = BaseApplication.getAppContext().getResources().getDisplayMetrics();
		if (dm == null)
			return;
		SCREEN_DENSITY = dm.density;
	}

	/**
	 * 計算UI/字體縮放比例
	 */
	public static void computeScaleRatio() {
		int windowWidth = getWindowWidth();
		int windowHeight = getWindowHeight();
		if (windowWidth == 0 || windowHeight == 0)
			return;
		int designedWidth = (windowWidth > windowHeight) ? UI_DESIGN_PORTRAIT_SIZE : UI_DESIGN_LANDSCAPE_SIZE;
		int designedHeight = (windowWidth > windowHeight) ? UI_DESIGN_LANDSCAPE_SIZE : UI_DESIGN_PORTRAIT_SIZE;
		SCALE_RATIO_HORIZONTAL = (float) windowWidth / (float) designedWidth;
		SCALE_RATIO_VERTICAL = (float) windowHeight / (float) designedHeight;

		float ratioDesigned = (float) UI_DESIGN_PORTRAIT_SIZE / (float) UI_DESIGN_LANDSCAPE_SIZE;
		float ratioDevice = (float) windowHeight / (float) windowWidth;
		/* 當(dāng)設(shè)備寬高比例與UI設(shè)計的比例相同,或者設(shè)備寬高比例比UI設(shè)計的比例瘦長時按照寬度等比縮放(主流),相反,如果比UI設(shè)計的比例胖扁時按照高度縮放 */
		SCALE_RATIO = ratioDevice >= ratioDesigned ? SCALE_RATIO_HORIZONTAL : SCALE_RATIO_VERTICAL;
	}

	/**
	 * 檢查當(dāng)前屏幕方向是否為橫向
	 *
	 * @return
	 */
	public static boolean isLandscape() {
		return BaseApplication.getAppContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
	}

	/**
	 * dip轉(zhuǎn)換px
	 *
	 * @param dip
	 * @return
	 */
	public static int dip2px(float dip) {
		float f = BaseApplication.getAppContext().getResources().getDisplayMetrics().density;
		return (int) (dip * f + 0.5F);
	}

	/**
	 * dip轉(zhuǎn)換px
	 *
	 * @param dip
	 * @return
	 */
	public static int px2dip(float px) {
		float f = BaseApplication.getAppContext().getResources().getDisplayMetrics().density;
		return (int) (px / f);
	}

	/**
	 * 檢查UI事件是否發(fā)生在視圖view的區(qū)域內(nèi)
	 *
	 * @param v
	 * @param ev
	 * @return
	 */
	public static boolean intersects(View v, MotionEvent ev) {
		if (v == null)
			return false;
		Rect rect = new Rect();
		v.getHitRect(rect);
		Rect r = new Rect();
		r.left = r.right = (int) ev.getX();
		r.bottom = r.top = (int) ev.getY();
		return rect.intersects(r.left, r.top, r.right, r.bottom);
	}

	/**
	 * 通過反射計算狀態(tài)欄高度
	 *
	 * @return
	 */
	public static int getStatusBarHeight() {
		if (STATUS_BAR_HEIGHT != 0)
			return STATUS_BAR_HEIGHT;
		Class<?> c = null;
		Object obj = null;
		Field field = null;
		int x = 0, statusBarHeight = 0;
		try {
			c = Class.forName("com.android.internal.R$dimen");
			obj = c.newInstance();
			field = c.getField("status_bar_height");
			x = Integer.parseInt(field.get(obj).toString());
			statusBarHeight = BaseApplication.getAppContext().getResources().getDimensionPixelSize(x);
			STATUS_BAR_HEIGHT = statusBarHeight;
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		return STATUS_BAR_HEIGHT;
	}

	/**
	 * 根據(jù)Activity獲取狀態(tài)欄高度
	 *
	 * @param activityo
	 * @return
	 */
	public static int getStatusBarHeight(Activity activity) {
		Rect frame = new Rect();
		activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
		return frame.top;
	}

	public static void computeWindowRotation() {
		WindowManager windowManager = (WindowManager) BaseApplication.getAppContext().getSystemService(Context.WINDOW_SERVICE);
		WINDOW_ROTATION = windowManager.getDefaultDisplay().getRotation();
		LogUtil.d(TAG, "computeWindowRotation rotation : " + WINDOW_ROTATION);
	}

	public static int getWindowRotation() {
		return WINDOW_ROTATION;
	}
}


向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI