欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android截屏截图的几种方法总结

程序员文章站 2023-10-21 20:00:18
 android截屏   android截屏的原理:获取具体需要截屏的区域的bitmap,然后绘制在画布上,保存为图片后进行分享或者其它用途 一、activit...

 android截屏

  android截屏的原理:获取具体需要截屏的区域的bitmap,然后绘制在画布上,保存为图片后进行分享或者其它用途

一、activity截屏

1、截activity界面(包含空白的状态栏)

/** 
   * 根据指定的activity截图(带空白的状态栏) 
   * 
   * @param context 要截图的activity 
   * @return bitmap 
   */ 
  public static bitmap shotactivity(activity context) { 
    view view = context.getwindow().getdecorview(); 
    view.setdrawingcacheenabled(true); 
    view.builddrawingcache(); 
 
    bitmap bitmap = bitmap.createbitmap(view.getdrawingcache(), 0, 0, view.getmeasuredwidth(), view.getmeasuredheight()); 
    view.setdrawingcacheenabled(false); 
    view.destroydrawingcache(); 
    return bitmap; 
  } 

2、截activity界面(去除状态栏)

/** 
   * 根据指定的activity截图(去除状态栏) 
   * 
   * @param activity 要截图的activity 
   * @return bitmap 
   */ 
  public bitmap shotactivitynostatusbar(activity activity) { 
    // 获取windows中最顶层的view 
    view view = activity.getwindow().getdecorview(); 
    view.builddrawingcache(); 
 
    // 获取状态栏高度 
    rect rect = new rect(); 
    view.getwindowvisibledisplayframe(rect); 
    int statusbarheights = rect.top; 
    display display = activity.getwindowmanager().getdefaultdisplay(); 
 
    // 获取屏幕宽和高 
    int widths = display.getwidth(); 
    int heights = display.getheight(); 
 
    // 允许当前窗口保存缓存信息 
    view.setdrawingcacheenabled(true); 
 
    // 去掉状态栏 
    bitmap bmp = bitmap.createbitmap(view.getdrawingcache(), 0, 
        statusbarheights, widths, heights - statusbarheights); 
 
    // 销毁缓存信息 
    view.destroydrawingcache(); 
 
    return bmp; 
  } 

二、view截屏

/** 
 * 根据指定的view截图 
 * 
 * @param v 要截图的view 
 * @return bitmap 
 */ 
public static bitmap getviewbitmap(view v) { 
  if (null == v) { 
    return null; 
  } 
  v.setdrawingcacheenabled(true); 
  v.builddrawingcache(); 
  if (build.version.sdk_int >= 11) { 
    v.measure(view.measurespec.makemeasurespec(v.getwidth(), view.measurespec.exactly), 
        view.measurespec.makemeasurespec(v.getheight(), view.measurespec.exactly)); 
    v.layout((int) v.getx(), (int) v.gety(), (int) v.getx() + v.getmeasuredwidth(), (int) v.gety() + v.getmeasuredheight()); 
  } else { 
    v.measure(view.measurespec.makemeasurespec(0, view.measurespec.unspecified), 
        view.measurespec.makemeasurespec(0, view.measurespec.unspecified)); 
    v.layout(0, 0, v.getmeasuredwidth(), v.getmeasuredheight()); 
  } 
 
  bitmap bitmap = bitmap.createbitmap(v.getdrawingcache(), 0, 0, v.getmeasuredwidth(), v.getmeasuredheight()); 
  v.setdrawingcacheenabled(false); 
  v.destroydrawingcache(); 
  return bitmap; 
} 

三、scrollview截屏:scrollview只有一个childview,虽然没有全部显示在界面上,但是已经全部渲染绘制,因此可以直接 调用scrollview.draw(canvas)来完成截图

/** 
 * scrollview截屏 
 * 
 * @param scrollview 要截图的scrollview 
 * @return bitmap 
 */ 
public static bitmap shotscrollview(scrollview scrollview) { 
  int h = 0; 
  bitmap bitmap = null; 
  for (int i = 0; i < scrollview.getchildcount(); i++) { 
    h += scrollview.getchildat(i).getheight(); 
    scrollview.getchildat(i).setbackgroundcolor(color.parsecolor("#ffffff")); 
  } 
  bitmap = bitmap.createbitmap(scrollview.getwidth(), h, bitmap.config.rgb_565); 
  final canvas canvas = new canvas(bitmap); 
  scrollview.draw(canvas); 
  return bitmap; 
} 

四、listview截屏:listview是会回收与重用item,并且只会绘制在屏幕上显示的itemview,下面的方法采用一个list来存储item的视图,这种方案依然不够好,当item足够多的时候,可能会发生oom。

/** 
 * listview截图 
 * 
 * @param listview 要截图的listview 
 * @return bitmap 
 */ 
public static bitmap shotlistview(listview listview) { 
  listadapter adapter = listview.getadapter(); 
  int itemscount = adapter.getcount(); 
  int allitemsheight = 0; 
 
  arraylist<bitmap> bmps = new arraylist<>(); 
  for (int i = 0; i < itemscount; i++) { 
    view childview = adapter.getview(i, null, listview); 
    childview.measure(view.measurespec.makemeasurespec(listview.getwidth(),view.measurespec.exactly), 
        view.measurespec.makemeasurespec(0,view.measurespec.unspecified)); 
 
    childview.layout(0, 0, childview.getmeasuredwidth(), childview.getmeasuredheight()); 
    childview.setdrawingcacheenabled(true); 
    childview.builddrawingcache(); 
    bmps.add(childview.getdrawingcache()); 
    allitemsheight += childview.getmeasuredheight(); 
  } 
 
  bitmap bigbitmap = bitmap.createbitmap(listview.getmeasuredwidth(), allitemsheight, bitmap.config.argb_8888); 
  canvas bigcanvas = new canvas(bigbitmap); 
 
  paint paint = new paint(); 
  int iheight = 0; 
 
  for (int i = 0; i < bmps.size(); i++) { 
    bitmap bmp = bmps.get(i); 
    bigcanvas.drawbitmap(bmp, 0, iheight, paint); 
    iheight += bmp.getheight(); 
 
    bmp.recycle(); 
    bmp = null; 
  } 
  return bigbitmap; 
} 

五、recycleview截屏

/** 
   * recyclerview截屏 
   * 
   * @param view 要截图的recyclerview 
   * @return bitmap 
   */ 
  public static bitmap shotrecyclerview(recyclerview view) { 
    recyclerview.adapter adapter = view.getadapter(); 
    bitmap bigbitmap = null; 
    if (adapter != null) { 
      int size = adapter.getitemcount(); 
      int height = 0; 
      paint paint = new paint(); 
      int iheight = 0; 
      final int maxmemory = (int) (runtime.getruntime().maxmemory() / 1024); 
 
      // use 1/8th of the available memory for this memory cache. 
      final int cachesize = maxmemory / 8; 
      lrucache<string, bitmap> bitmacache = new lrucache<>(cachesize); 
      for (int i = 0; i < size; i++) { 
        recyclerview.viewholder holder = adapter.createviewholder(view, adapter.getitemviewtype(i)); 
        adapter.onbindviewholder(holder, i); 
        holder.itemview.measure( 
            view.measurespec.makemeasurespec(view.getwidth(), view.measurespec.exactly), 
            view.measurespec.makemeasurespec(0, view.measurespec.unspecified)); 
        holder.itemview.layout(0, 0, holder.itemview.getmeasuredwidth(), 
            holder.itemview.getmeasuredheight()); 
        holder.itemview.setdrawingcacheenabled(true); 
        holder.itemview.builddrawingcache(); 
        bitmap drawingcache = holder.itemview.getdrawingcache(); 
        if (drawingcache != null) { 
 
          bitmacache.put(string.valueof(i), drawingcache); 
        } 
        height += holder.itemview.getmeasuredheight(); 
      } 
 
      bigbitmap = bitmap.createbitmap(view.getmeasuredwidth(), height, bitmap.config.argb_8888); 
      canvas bigcanvas = new canvas(bigbitmap); 
      drawable lbackground = view.getbackground(); 
      if (lbackground instanceof colordrawable) { 
        colordrawable lcolordrawable = (colordrawable) lbackground; 
        int lcolor = lcolordrawable.getcolor(); 
        bigcanvas.drawcolor(lcolor); 
      } 
 
      for (int i = 0; i < size; i++) { 
        bitmap bitmap = bitmacache.get(string.valueof(i)); 
        bigcanvas.drawbitmap(bitmap, 0f, iheight, paint); 
        iheight += bitmap.getheight(); 
        bitmap.recycle(); 
      } 
    } 
    return bigbitmap; 
  } 

六、webview截屏

1、截取webview可视区域的截图

/**  
  * 截取webview可视区域的截图  
  * @param webview 前提:webview要设置webview.setdrawingcacheenabled(true);  
  * @return  
  */  
private bitmap capturewebviewvisiblesize(webview webview){  
  bitmap bmp = webview.getdrawingcache();  
  return bmp;  
}  

2、截取webview的整个页面

/**  
 * 截取webview快照(webview加载的整个内容的大小)  
 * @param webview  
 * @return  
 */  
private bitmap capturewebview(webview webview){  
  picture snapshot = webview.capturepicture();  
    
  bitmap bmp = bitmap.createbitmap(snapshot.getwidth(),snapshot.getheight(), bitmap.config.argb_8888);  
  canvas canvas = new canvas(bmp);  
  snapshot.draw(canvas);  
  return bmp;  
}  

3、截取手机屏幕,获取界面展示的webview

/** 
 * 截屏 
 * 
 * @param context 
 * @return 
 */ 
private bitmap capturescreen(activity context) { 
  view cv = context.getwindow().getdecorview(); 
  bitmap bmp = bitmap.createbitmap(cv.getwidth(), cv.getheight(), bitmap.config.argb_8888); 
  canvas canvas = new canvas(bmp); 
  cv.draw(canvas); 
  return bmp; 
} 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!