Skip to content

Commit f3f60b9

Browse files
author
371718330@qq.com
committed
增加Android屏幕的工具类
1 parent 102c2d8 commit f3f60b9

File tree

1 file changed

+190
-0
lines changed

1 file changed

+190
-0
lines changed

CommonUtil/src/main/java/com/jingewenku/abrahamcaijin/commonutil/AppScreenMgr.java

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,26 @@
44
import android.content.Context;
55
import android.content.res.Resources;
66
import android.graphics.Bitmap;
7+
import android.graphics.Canvas;
8+
import android.graphics.Picture;
79
import android.graphics.Rect;
10+
import android.os.Environment;
811
import android.util.DisplayMetrics;
12+
import android.util.Log;
913
import android.view.Display;
1014
import android.view.View;
15+
import android.view.Window;
1116
import android.view.WindowManager;
17+
import android.webkit.WebView;
1218

19+
import java.io.File;
20+
import java.io.FileNotFoundException;
21+
import java.io.FileOutputStream;
22+
import java.io.IOException;
23+
import java.lang.reflect.Field;
1324
import java.lang.reflect.Method;
25+
import java.text.SimpleDateFormat;
26+
import java.util.Date;
1427

1528
/**
1629
* 主要功能:有关Android屏幕的工具类
@@ -174,4 +187,181 @@ public static Bitmap snapShotWithoutStatusBar(Activity activity)
174187
view.destroyDrawingCache();
175188
return bp;
176189
}
190+
191+
/**
192+
* 获得标题栏高度
193+
*
194+
* @param context 上下文,为Activity对象
195+
* @return 标题栏高度
196+
*/
197+
public static int getTitleBarHeight(Activity context) {
198+
int contentTop = context.getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
199+
return contentTop - getStatusBarHeight(context);
200+
}
201+
202+
/**
203+
* 获取通知栏高度
204+
*
205+
* @param context 上下文
206+
* @return 通知栏高度
207+
*/
208+
public static int getStatusBarHeight(Context context) {
209+
int statusBarHeight = 0;
210+
try {
211+
Class<?> clazz = Class.forName("com.android.internal.R$dimen");
212+
Object obj = clazz.newInstance();
213+
Field field = clazz.getField("status_bar_height");
214+
int temp = Integer.parseInt(field.get(obj).toString());
215+
statusBarHeight = context.getResources().getDimensionPixelSize(temp);
216+
} catch (Exception e) {
217+
e.printStackTrace();
218+
}
219+
return statusBarHeight;
220+
}
221+
222+
/**
223+
* 获取指定Activity的截屏,保存到png文件
224+
*
225+
* @param activity activity
226+
* @return 截屏Bitmap
227+
*/
228+
private static Bitmap takeScreenShot(Activity activity) {
229+
// View是你需要截图的View
230+
View view = activity.getWindow().getDecorView();
231+
view.setDrawingCacheEnabled(true);
232+
view.buildDrawingCache();
233+
Bitmap b1 = view.getDrawingCache();
234+
235+
// 获取状态栏高度
236+
Rect frame = new Rect();
237+
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
238+
int statusBarHeight = frame.top;
239+
Log.i("TAG", "" + statusBarHeight);
240+
241+
// 获取屏幕长和高
242+
int width = activity.getWindowManager().getDefaultDisplay().getWidth();
243+
int height = activity.getWindowManager().getDefaultDisplay()
244+
.getHeight();
245+
Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height
246+
- statusBarHeight);
247+
view.destroyDrawingCache();
248+
return b;
249+
}
250+
251+
/**
252+
* 保存bitmap
253+
*
254+
* @param b bitmap
255+
* @param strFileName 文件名
256+
* @return 是否保存成功
257+
*/
258+
private static boolean savePic(Bitmap b, String strFileName) {
259+
FileOutputStream fos = null;
260+
try {
261+
fos = new FileOutputStream(strFileName);
262+
if (null != fos) {
263+
b.compress(Bitmap.CompressFormat.PNG, 90, fos);
264+
fos.flush();
265+
fos.close();
266+
return true;
267+
}
268+
} catch (FileNotFoundException e) {
269+
e.printStackTrace();
270+
} catch (IOException e) {
271+
e.printStackTrace();
272+
}
273+
274+
return false;
275+
}
276+
277+
/**
278+
* 截取webView快照(webView加载的整个内容的大小)
279+
*
280+
* @param webView webview
281+
* @return 截屏bitmap
282+
*/
283+
private static Bitmap captureWebView(WebView webView) {
284+
Picture snapShot = webView.capturePicture();
285+
286+
Bitmap bmp = Bitmap.createBitmap(snapShot.getWidth(), snapShot.getHeight(), Bitmap.Config.ARGB_8888);
287+
Canvas canvas = new Canvas(bmp);
288+
snapShot.draw(canvas);
289+
return bmp;
290+
}
291+
292+
/**
293+
* 根据毫秒获得格式化日期
294+
*
295+
* @param time 毫秒数
296+
* @param format 格式化字符串
297+
* @return 格式化后的字符串
298+
*/
299+
private static String getDate(long time, String format) {
300+
Date date = new Date(time);
301+
SimpleDateFormat formatter = new SimpleDateFormat(format);
302+
String daystr = formatter.format(date);
303+
return daystr;
304+
}
305+
306+
/**
307+
* 是否存在sd卡
308+
*
309+
* @return 是否存在sd卡
310+
*/
311+
private static Boolean isExistsSD() {
312+
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
313+
return true;
314+
return false;
315+
}
316+
317+
/**
318+
* 获得文件名
319+
*
320+
* @param context 上下文
321+
* @return 文件名
322+
*/
323+
private static String getFileName(Context context) {
324+
String fileName = getDate(System.currentTimeMillis(), "yyyyMMddHHmmss") + ".png";
325+
final String localPath;
326+
if (isExistsSD()) {
327+
localPath = context.getExternalCacheDir() + File.separator + fileName;
328+
} else {
329+
localPath = context.getFilesDir() + fileName;
330+
}
331+
332+
return localPath;
333+
}
334+
335+
/**
336+
* 截屏并保存
337+
*
338+
* @param a activity
339+
* @return 保存的路径
340+
*/
341+
public static String shoot(Activity a) {
342+
String localPath = getFileName(a);
343+
boolean ret = AppScreenMgr.savePic(AppScreenMgr.takeScreenShot(a), localPath);
344+
if (ret) {
345+
return localPath;
346+
} else {
347+
return "";
348+
}
349+
}
350+
351+
/**
352+
* 截屏并保存
353+
*
354+
* @param context 上下文
355+
* @param webView webview
356+
* @return 保存的路径
357+
*/
358+
public static String shootWebView(Context context, WebView webView) {
359+
String localPath = getFileName(context);
360+
boolean ret = AppScreenMgr.savePic(AppScreenMgr.captureWebView(webView), localPath);
361+
if (ret) {
362+
return localPath;
363+
} else {
364+
return "";
365+
}
366+
}
177367
}

0 commit comments

Comments
 (0)