温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

android 手机截取长屏实例代码

发布时间:2020-09-13 11:29:55 来源:脚本之家 阅读:159 作者:喜欢萌妹子的少年 栏目:移动开发

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

 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)));

好了,截取成功了!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI