Android to get the phone screen size implementation code
- 2020-06-23 01:51:42
- OfStack
Android in custom control, often need to get the width and height of the screen, every time to write, might as well directly package it into a tool class, directly used, nonsense don't say, directly on the code
/**
*
*/
package com.example.customview;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.WindowManager;
/**
* Gets the screen size of the phone
* @author
*
*/
public class MeasureUtil {
/**
* wide
* @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;
}
/**
* high
* @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;
}
}