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

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

程序员文章站 2023-12-10 13:31:04
本文实例讲述了android编程之截屏实现方法。分享给大家供大家参考,具体如下: public class screenshot { // 获取指定activ...

本文实例讲述了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),
    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;
  bitmap bitmap = null;
  // 获取listview实际高度
  for (int i = 0; i < scrollview.getchildcount(); i++) {
   h += scrollview.getchildat(i).getheight();
   scrollview.getchildat(i).setbackgroundresource(r.drawable.bg3);
  }
  log.d(tag, "实际高度:" + h);
  log.d(tag, " 高度:" + scrollview.getheight());
  // 创建对应大小的bitmap
  bitmap = bitmap.createbitmap(scrollview.getwidth(), h,
    bitmap.config.argb_8888);
  final canvas canvas = new canvas(bitmap);
  scrollview.draw(canvas);
  // 测试输出
  fileoutputstream out = null;
  try {
   out = new fileoutputstream("/sdcard/screen_test.png");
  } catch (filenotfoundexception e) {
   e.printstacktrace();
  }
  try {
   if (null != out) {
    bitmap.compress(bitmap.compressformat.png, 100, out);
    out.flush();
    out.close();
   }
  } catch (ioexception e) {
   // todo: handle exception
  }
  return bitmap;
 }
 private static string tag = "listview and scrollview item 截图:";
 /**
  * 截图listview
  * **/
 public static bitmap getbbitmap(listview listview) {
  int h = 0;
  bitmap bitmap = null;
  // 获取listview实际高度
  for (int i = 0; i < listview.getchildcount(); i++) {
   h += listview.getchildat(i).getheight();
  }
  log.d(tag, "实际高度:" + h);
  log.d(tag, "list 高度:" + listview.getheight());
  // 创建对应大小的bitmap
  bitmap = bitmap.createbitmap(listview.getwidth(), h,
    bitmap.config.argb_8888);
  final canvas canvas = new canvas(bitmap);
  listview.draw(canvas);
  // 测试输出
  fileoutputstream out = null;
  try {
   out = new fileoutputstream("/sdcard/screen_test.png");
  } catch (filenotfoundexception e) {
   e.printstacktrace();
  }
  try {
   if (null != out) {
    bitmap.compress(bitmap.compressformat.png, 100, out);
    out.flush();
    out.close();
   }
  } catch (ioexception e) {
   // todo: handle exception
  }
  return bitmap;
 }
}

希望本文所述对大家android程序设计有所帮助。