如何获得屏幕的宽高?ScreenUtils工具类的学习与应用!!!
来源:互联网 发布:php开源博客系统 编辑:程序博客网 时间:2024/05/20 13:18
在屏幕布局的时候,总会需要获得屏幕的一些参数,而Android本身又没有提供这些API,这时候就需要程序猿自己去写工具类获取自己想要的数据,好在不复杂,可以写下来保存留着用。
import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Rect; import android.util.DisplayMetrics; import android.view.View; import android.view.WindowManager; /** * 获得屏幕相关的辅助类 * * * */ public class ScreenUtils { private ScreenUtils() { /* cannot be instantiated */ throw new UnsupportedOperationException("cannot be instantiated"); } /** * 获得屏幕高度 * * @param context * @return */ public static int getScreenWidth(Context context) { WindowManager wm = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); return outMetrics.widthPixels; } /** * 获得屏幕宽度 * * @param context * @return */ public static int getScreenHeight(Context context) { WindowManager wm = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); return outMetrics.heightPixels; } /** * 获得状态栏的高度 * * @param context * @return */ public static int getStatusHeight(Context context) { int statusHeight = -1; try { Class<?> clazz = Class.forName("com.android.internal.R$dimen"); Object object = clazz.newInstance(); int height = Integer.parseInt(clazz.getField("status_bar_height") .get(object).toString()); statusHeight = context.getResources().getDimensionPixelSize(height); } catch (Exception e) { e.printStackTrace(); } return statusHeight; } /** * 获取当前屏幕截图,包含状态栏 * * @param activity * @return */ public static Bitmap snapShotWithStatusBar(Activity activity) { View view = activity.getWindow().getDecorView(); view.setDrawingCacheEnabled(true); view.buildDrawingCache(); Bitmap bmp = view.getDrawingCache(); int width = getScreenWidth(activity); int height = getScreenHeight(activity); Bitmap bp = null; bp = Bitmap.createBitmap(bmp, 0, 0, width, height); view.destroyDrawingCache(); return bp; } /** * 获取当前屏幕截图,不包含状态栏 * * @param activity * @return */ public static Bitmap snapShotWithoutStatusBar(Activity activity) { View view = activity.getWindow().getDecorView(); view.setDrawingCacheEnabled(true); view.buildDrawingCache(); Bitmap bmp = view.getDrawingCache(); Rect frame = new Rect(); activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); int statusBarHeight = frame.top; int width = getScreenWidth(activity); int height = getScreenHeight(activity); Bitmap bp = null; bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height - statusBarHeight); view.destroyDrawingCache(); return bp; } }
1 0
- 如何获得屏幕的宽高?ScreenUtils工具类的学习与应用!!!
- 和屏幕相关的工具类ScreenUtils
- Android ScreenUtils-获得屏幕相关的辅助类
- ScreenUtils,一个跟android屏幕相关的工具类
- Android 开发的常用工具类(一)——ScreenUtils 可用于获取屏幕宽高,截屏的工具类
- 封装获得屏幕宽高的工具类
- 获得屏幕的高宽
- Android获得屏幕宽高的方法
- 获得屏幕高宽的方法汇总
- 在任意类中获得屏幕的宽高
- 屏幕相关辅助类 ScreenUtils
- 工具类—如何快速获得Android应用的版本号
- 获得Android手机的屏幕宽、高以及dip或sp与pix的转换
- 分享一个获取屏幕的宽高的工具类
- 如何获得屏幕的分辨率
- Android中获得屏幕宽高的实用方法
- 获得屏幕宽高
- 获得屏幕宽高
- opencv 获取Mat像素值
- 第十一篇:Harr特征博文讲解集
- 2013年 蓝桥杯 (1--10题)
- TCP传输控制协议 读书笔记
- 战胜棋王后,人工智能是否可以颠覆安全?
- 如何获得屏幕的宽高?ScreenUtils工具类的学习与应用!!!
- 项目失败的常见原因有哪些?
- #Tab方案(一)#
- Educational Codeforces Round 7 Codeforces 622D Codeforces 622E
- 南京JingDong笔试题
- Objective-C中的Block
- HDU1042 N!
- 自己用Python写的iOS项目自动打包脚本
- 《iOS Human Interface Guidelines》——Collection View