iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >android 手机截取长屏实例代码
  • 841
分享到

android 手机截取长屏实例代码

android手机截取 2023-05-31 00:05:03 841人浏览 薄情痞子
摘要

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

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

 public class ScreenUtils {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;}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;}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;}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;   }}

--结束END--

本文标题: android 手机截取长屏实例代码

本文链接: https://www.lsjlt.com/news/222655.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作