Android获取常用辅助方法(获取屏幕高度、宽度、密度、通知栏高度、截图)
作者:mrr 时间:2023-10-30 19:42:57
我们需要获取Android手机或Pad的屏幕的物理尺寸,以便于界面的设计或是其他功能的实现。下面就分享一下Android中常用的一些辅助方法:
获取屏幕高度:
/**
* 获得屏幕高度
* @param context
* @return
* by Hankkin at:2015-10-07 21:15:59
*/
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
* by Hankkin at:2015-10-07 21:16:13
*/
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
* by Hankkin at:2015-10-07 21:16:29
*/
public static float getScreenDensity(Context context) {
return context.getResources().getDisplayMetrics().density;
}
dip转px:
/**
* dip转px像素
* @param context
* @param px
* @return
* by Hankkin at:2015-10-07 21:16:43
*/
public static int dip2px(Context context, float px) {
final float scale = getScreenDensity(context);
return (int) (px * scale + 0.5);
}
获取状态栏高度:
/**
* 获得状态栏的高度
* @param context
* @return
* by Hankkin at:2015-10-07 21:16:43
*/
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
* by Hankkin at:2015-10-07 21:16:43
*/
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
* by Hankkin at:2015-10-07 21:16:43
*/
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;
}
以上所述是本文给大家介绍的Android获取常用辅助方法(获取屏幕高度、宽度、密度、通知栏高度、截图),希望对大家也是帮助,更多信息登录脚本之家网站了解更多信息。
标签:android,获取,方法
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java中Collections.sort的使用
2022-08-20 04:31:49
![](https://img.aspxhome.com/file/2023/0/90210_0s.png)
AndroidManifest.xml中含盖的安全问题详解
2023-05-19 09:22:30
java实现简单猜拳小游戏
2021-09-01 07:49:46
![](https://img.aspxhome.com/file/2023/3/79223_0s.jpg)
在编程语言中怎样定义队列及其使用(C++)
2021-05-28 14:00:43
![](https://img.aspxhome.com/file/2023/4/124034_0s.jpg)
Java新手学习之IO流的简单使用
2023-08-09 15:57:33
![](https://img.aspxhome.com/file/2023/6/58196_0s.jpg)
C#中图片、二进制与字符串的相互转换方法
2023-05-16 13:07:50
Spring Boot集成MyBatis实现通用Mapper的配置及使用
2023-07-03 18:17:10
60条Android开发注意事项与经验总结
2021-12-07 11:55:16
Android实现关机与重启的几种方式(推荐)
2022-08-31 14:29:54
浅谈解决Hibernate懒加载的4种方式
2022-07-04 00:46:39
![](https://img.aspxhome.com/file/2023/0/85950_0s.png)
Android 接收微信、QQ其他应用打开第三方分享功能
2022-06-12 18:11:58
![](https://img.aspxhome.com/file/2023/7/113287_0s.jpg)
Java结构型模式之门面模式详解
2023-07-29 10:26:55
![](https://img.aspxhome.com/file/2023/8/91058_0s.png)
C#中is,as,using关键字的使用说明
2023-07-01 22:54:42
![](https://img.aspxhome.com/file/2023/4/76394_0s.jpg)
Java数据类型的全面剖析
2022-07-09 06:51:38
![](https://img.aspxhome.com/file/2023/1/63061_0s.png)
Springboot中加入druid连接池
2023-08-08 05:47:22
解决Android studio 2.3升级到Android studio 3.0 后apt报错问题
2023-05-17 13:56:50
SpringBoot 整合jdbc和mybatis的方法
2023-08-10 12:51:09
![](https://img.aspxhome.com/file/2023/8/58158_0s.png)
Spring Boot应用监控的实战教程
2022-03-02 18:17:09
![](https://img.aspxhome.com/file/2023/5/83595_0s.png)
java8中的lambda表达式简介
2022-09-12 04:14:10
spring Boot查询数据分页显示的方法实例
2023-04-28 13:30:08