Android中获得手机屏幕大小实现代码
Android中获得手机屏幕大小实现代码
发布时间:2016-12-28 来源:查字典编辑
摘要:Android在自定义控件时,经常需要获得屏幕的宽高,每次都要写,不妨直接把他封装成工具类,直接拿来用,废话不说,直接上代码/****/pa...

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; } }

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新安卓软件开发学习
热门安卓软件开发学习
编程开发子分类