当前位置: 移动技术网 > 移动技术>移动开发>Android > android 手机截取长屏实例代码

android 手机截取长屏实例代码

2019年07月24日  | 移动技术网移动技术  | 我要评论

最近项目遇到一个需求:把当前页面保存到手机相册。想了想 我还不会呢,就百度了下大神的足迹,踏着大神的足迹,一路向前。废话不说,记录下,后期学习。

 public class screenutils {

/**
 * 截取scrollview的屏幕
 * @param scrollview
 * @return
 */
public static bitmap getbitmapbyview(scrollview scrollview) {
  int h = 0;
  bitmap bitmap = null;
  // 获取scrollview实际高度
  for (int i = 0; i < scrollview.getchildcount(); i++) {
    h += scrollview.getchildat(i).getheight();
    scrollview.getchildat(i).setbackgroundcolor(
        color.parsecolor("#ffffff"));
  }
  // 创建对应大小的bitmap
  bitmap = bitmap.createbitmap(scrollview.getwidth(), h,
      bitmap.config.rgb_565);
  final canvas canvas = new canvas(bitmap);
  scrollview.draw(canvas);
  return bitmap;
}

/**
 * 截图listview
 * **/
public static bitmap getlistviewbitmap(listview listview,string picpath) {
  int h = 0;
  bitmap bitmap;
  // 获取listview实际高度
  for (int i = 0; i < listview.getchildcount(); i++) {
    h += listview.getchildat(i).getheight();
  }
  // 创建对应大小的bitmap
  bitmap = bitmap.createbitmap(listview.getwidth(), h,
      bitmap.config.argb_8888);
  final canvas canvas = new canvas(bitmap);
  listview.draw(canvas);
  return bitmap;
}


/**
 * 压缩图片
 * @param image
 * @return
 */
public static bitmap compressimage(bitmap image) {
  bytearrayoutputstream baos = new bytearrayoutputstream();
  // 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
  image.compress(bitmap.compressformat.jpeg, 100, baos);
  int options = 100;
  // 循环判断如果压缩后图片是否大于250k,大于继续压缩
  while (baos.tobytearray().length / 1024 > 1024 && options >10) {
    // 重置baos
    baos.reset();
    // 这里压缩options%,把压缩后的数据存放到baos中
    image.compress(bitmap.compressformat.jpeg, options, baos);
    // 每次都减少10
    options -= 10;
  }
  // 把压缩后的数据baos存放到bytearrayinputstream中
  bytearrayinputstream isbm = new bytearrayinputstream(baos.tobytearray());
  // 把bytearrayinputstream数据生成图片
  bitmap bitmap = bitmapfactory.decodestream(isbm, null, null);
  return bitmap;
}

/**
 * 保存到sdcard
 * @param b
 * @return
 */
public static string savepic(context context, bitmap b) {

  file outfile = new file("/sdcard/image");
  // 如果文件不存在,则创建一个新文件
  if (!outfile.isdirectory()) {
    try {
      outfile.mkdir();
    } catch (exception e) {
      e.printstacktrace();
    }
  }
  string fname = outfile + "/" + system.currenttimemillis() + ".jpg";
  fileoutputstream fos = null;
  try {
    fos = new fileoutputstream(fname);
    if (null != fos) {
      b.compress(bitmap.compressformat.jpeg, 90, fos);
      fos.flush();
      fos.close();
    }
  } catch (filenotfoundexception e) {
    e.printstacktrace();
  } catch (ioexception e) {
    e.printstacktrace();
  }

  // 其次把文件插入到系统图库
  try {
    mediastore.images.media.insertimage(context.getcontentresolver(),
        outfile.getabsolutepath(), fname, null);
  } catch (filenotfoundexception e) {
    e.printstacktrace();
  }
  // 最后通知图库更新
  context.sendbroadcast(new intent(intent.action_media_scanner_scan_file, uri.parse("file://" + fname)));


  return fname;
   }
}

以上为百度的工具类。

使用方法:

 screenutils
   .savepic(xxxactivity.this,screenutils.compressimage(screenutils
      .getbitmapbyview(xxxscrollview)));

好了,截取成功了!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网