|
|
package net.micode.notes.tool;
|
|
|
|
|
|
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;
|
|
|
|
|
|
/**
|
|
|
* ScreenUtils是一个工具类,用于获取屏幕信息和截取屏幕截图。
|
|
|
*/
|
|
|
public class ScreenUtils {
|
|
|
|
|
|
/**
|
|
|
* 获得屏幕宽度
|
|
|
* 通过Context对象获取屏幕宽度,使用WindowManager服务获取显示度量,并返回屏幕的宽度像素值。
|
|
|
*
|
|
|
* @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;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获得屏幕高度
|
|
|
* 通过Context对象获取屏幕高度,使用WindowManager服务获取显示度量,并返回屏幕的高度像素值。
|
|
|
*
|
|
|
* @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;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取当前屏幕截图,包含状态栏
|
|
|
* 截取整个屏幕的截图,包括状态栏,并返回一个Bitmap对象。
|
|
|
*
|
|
|
* @param activity 当前Activity对象
|
|
|
* @return 包含状态栏的屏幕截图的Bitmap对象
|
|
|
*/
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取当前屏幕截图,不包含状态栏
|
|
|
* 截取整个屏幕的截图,不包括状态栏,并返回一个Bitmap对象。
|
|
|
*
|
|
|
* @param activity 当前Activity对象
|
|
|
* @return 不包含状态栏的屏幕截图的Bitmap对象
|
|
|
*/
|
|
|
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;
|
|
|
}
|
|
|
}
|