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

Android屏幕及view的截图实例详解

程序员文章站 2023-12-02 20:06:58
android屏幕及view的截图实例详解 屏幕可见区域的截图 整个屏幕截图的话可以用view view = getwindow().getdecorview()...

android屏幕及view的截图实例详解

屏幕可见区域的截图

整个屏幕截图的话可以用view view = getwindow().getdecorview();

public static bitmap getnormalviewscreenshot(view view) {
    view.setdrawingcacheenabled(true);
    view.builddrawingcache();
    return view.getdrawingcache();
  }

scrollview的整体截屏

public static bitmap getwholescrollviewtobitmap(view view) {
    view.measure(measurespec.makemeasurespec(0, measurespec.unspecified),
        measurespec.makemeasurespec(0, measurespec.unspecified));
    view.layout(0, 0, view.getmeasuredwidth(), view.getmeasuredheight());
    view.builddrawingcache();
    bitmap bitmap = view.getdrawingcache();
    return bitmap;
  }

webview的整体截图

public static bitmap getwholewebviewtobitmap(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;
  }

listview的整体截图

public static bitmap getwholelistviewitemstobitmap(listview listview) {

    listadapter adapter = listview.getadapter();
    int itemscount = adapter.getcount();
    int allitemsheight = 0;
    list<bitmap> bmps = new arraylist<bitmap>();

    for (int i = 0; i < itemscount; i++) {

      view childview = adapter.getview(i, null, listview);
      childview.measure(measurespec.makemeasurespec(listview.getwidth(), measurespec.exactly),
          measurespec.makemeasurespec(0, 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;
  }

需要多次截图的话,需要用到 view.destroydrawingcache();

bitmap normalviewscreenshot = screenshotutils.getnormalviewscreenshot(mframecontent);
        if (normalviewscreenshot != null) {
          bitmap b = bitmap.createbitmap(normalviewscreenshot);
          mimageresult.setimagebitmap(b);
          mframecontent.destroydrawingcache();
        }

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