易采站长站为您分析Android编程之截屏实现方法,包括截取scrollview与listview屏幕的相关技巧,以及截屏图片的生成与保存技巧,需要的朋友可以参考下
本文实例讲述了Android编程之截屏实现方法。,具体如下:
- public class ScreenShot { // 获取指定Activity的截屏,保存到png文件
- public static Bitmap takeScreenShot(Activity activity) { // View是你需要截图的View
- View view = activity.getWindow().getDecorView(); view.setDrawingCacheEnabled(true);
- view.buildDrawingCache(); Bitmap b1 = view.getDrawingCache();
- // 获取状态栏高度 Rect frame = new Rect();
- activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); int statusBarHeight = frame.top;
- System.out.println(statusBarHeight); // 获取屏幕长和高
- int width = activity.getWindowManager().getDefaultDisplay().getWidth(); int height = activity.getWindowManager().getDefaultDisplay()
- .getHeight(); // 去掉标题栏
- // Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455); Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height
- - statusBarHeight); view.destroyDrawingCache();
- savePic(b, "/sdcard/screen_test.png"); return b;
- } // 保存到sdcard
- public static void savePic(Bitmap b, String strFileName) { FileOutputStream fos = null;
- try { fos = new FileOutputStream(strFileName);
- if (null != fos) { b.compress(Bitmap.CompressFormat.PNG, 90, fos);
- fos.flush(); fos.close();
- } } catch (FileNotFoundException e) {
- e.printStackTrace(); } catch (IOException e) {
- e.printStackTrace(); }
- } /**
- * 把View对象转换成bitmap * */
- public static Bitmap convertViewToBitmap(View view) { view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),










