/** * 判断是否为平板 * * @return */ private boolean isPad() { WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE); Display display = wm.getDefaultDisplay(); // 屏幕宽度 float screenWidth = display.getWidth(); // 屏幕高度 float screenHeight = display.getHeight(); DisplayMetrics dm = new DisplayMetrics(); display.getMetrics(dm); double x = Math.pow(dm.widthPixels / dm.xdpi, 2); double y = Math.pow(dm.heightPixels / dm.ydpi, 2); // 屏幕尺寸 double screenInches = Math.sqrt(x + y); // 大于6尺寸则为Pad if (screenInches >= 6.0) { return true; } return false; }
import android.app.Activity; import android.content.Context; import android.util.DisplayMetrics; public class DisplayUtils { /** * 将px值转换为dp值 */ public static int px2dp(Context context, float pxValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); } /** * 将dp值转换为px值 */ public static int dp2px(Context context, float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } /** * 将px值转换为sp值 */ public static int px2sp(Context context, float pxValue) { final float fontScale = context.getResources().getDisplayMetrics().scaledDensity; return (int) (pxValue / fontScale + 0.5f); } /** * 将sp值转换为px值 */ public static int sp2px(Context context, float spValue) { final float fontScale = context.getResources().getDisplayMetrics().scaledDensity; return (int) (spValue * fontScale + 0.5f); } /** * 获取屏幕宽度 */ public static int getScreenWidthPixels(Activity context) { DisplayMetrics metric = new DisplayMetrics(); context.getWindowManager().getDefaultDisplay().getMetrics(metric); return metric.widthPixels; } /** * 获取屏幕高度 */ public static int getScreenHeightPixels(Activity context) { DisplayMetrics metric = new DisplayMetrics(); context.getWindowManager().getDefaultDisplay().getMetrics(metric); return metric.heightPixels; } }