Home > php教程 > PHP源码 > Android gets phone screen size

Android gets phone screen size

大家讲道理
Release: 2016-11-09 09:29:40
Original
1007 people have browsed it

When customizing controls in Android, you often need to get the width and height of the screen. You have to write it every time. You might as well encapsulate it into a tool class and use it directly. Don’t talk nonsense and just write the code

/** 
 *  
 */ 
 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;  
    }  
   
}
Copy after login


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template