Android编程之截屏实现方法(包括scrollview与listview)

2019-12-10 19:52:57王冬梅
  • MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));  view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); 
  • view.buildDrawingCache();  Bitmap bitmap = view.getDrawingCache(); 
  • if (bitmap != null) {  System.out.println("这不是nullde1"); 
  • Log.d("nullde1", "nullde1");  } else { 
  • System.out.println("这nullnulllnulnlul");  } 
  • return bitmap;  } 
  • // 程序入口1  public static void shoot(Activity a) { 
  • ScreenShot.savePic(ScreenShot.takeScreenShot(a), "/sdcard/screen_test.png");  } 
  • // 程序入口2  public static void shootView(View view) { 
  • ScreenShot.savePic(ScreenShot.convertViewToBitmap(view),  "sdcard/xx.png"); 
  • }  public static Bitmap getViewBitmap(View v) { 
  • v.clearFocus();  v.setPressed(false); 
  • boolean willNotCache = v.willNotCacheDrawing();  v.setWillNotCacheDrawing(false); 
  • // Reset the drawing cache background color to fully transparent  // for the duration of this operation 
  • int color = v.getDrawingCacheBackgroundColor();  v.setDrawingCacheBackgroundColor(0); 
  • if (color != 0) {  v.destroyDrawingCache(); 
  • }  v.buildDrawingCache(); 
  • Bitmap cacheBitmap = v.getDrawingCache();  if (cacheBitmap == null) { 
  • Log.e("TTTTTTTTActivity", "failed getViewBitmap(" + v + ")",  new RuntimeException()); 
  • return null;  } 
  • Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);  // Restore the view 
  • v.destroyDrawingCache();  v.setWillNotCacheDrawing(willNotCache); 
  • v.setDrawingCacheBackgroundColor(color);  return bitmap; 
  • }  /** 
  • * 截取scrollview的屏幕  * **/ 
  • public static Bitmap getBitmapByView(ScrollView scrollView) {  int h = 0;