Android在自訂控制時,經常需要獲得螢幕的寬高,每次都要寫,不妨直接把他封裝成工具類,直接拿來用,廢話不說,直接上代碼
/** * */ package com.example.customview; import android.content.Context; import android.util.DisplayMetrics; import android.view.WindowManager; /** * 获取手机屏幕大小 * @author * */ public class MeasureUtil { /** * 宽 * @return */ public static int getWidth(Context context){ WindowManager wm=(WindowManager) context.getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); return outMetrics.widthPixels; } /** * 高 * @return */ public static int getHeight(Context context){ WindowManager wm=(WindowManager) context.getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); return outMetrics.heightPixels; } }