阿里云服务器免费领卷啦。

捡代码论坛-最全的游戏源码下载技术网站!

 找回密码
 立 即 注 册

QQ登录

只需一步,快速开始

搜索
关于源码区的附件失效或欺骗帖, 处理办法
查看: 4209|回复: 0

Android 实用方法 — 截屏与截长图功能的实现

[复制链接]

4208

主题

210

回帖

12万

积分

管理员

管理员

Rank: 9Rank: 9Rank: 9

积分
126156
QQ
发表于 2017-2-20 11:26:04 | 显示全部楼层 |阅读模式

Demo在GitHub的地址:

https://github.com/Hebin320/ScreenShoot

Demo在CSDN上的下载地址:

http://download.csdn.net/detail/hebin320320/9721970

Android开发中,有时候会遇到需要截屏分享到朋友圈或者QQ,截屏有截取当前屏幕,也有需要截取不仅一个屏幕,可能会很长。

截取当前屏幕并保存到内存卡的方法:

  1. // 获取指定Activity的截屏,保存到png文件
  2.     public static Bitmap takeScreenShot(Activity activity) {
  3.         // View是你需要截图的View
  4.         View view = activity.getWindow().getDecorView();
  5.         view.setDrawingCacheEnabled(true);
  6.         view.buildDrawingCache();
  7.         Bitmap b1 = view.getDrawingCache();

  8.         // 获取状态栏高度
  9.         Rect frame = new Rect();
  10.         activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
  11.         int statusBarHeight = frame.top;
  12.         System.out.println(statusBarHeight);

  13.         // 获取屏幕长和高
  14.         int width = activity.getWindowManager().getDefaultDisplay().getWidth();
  15.         int height = activity.getWindowManager().getDefaultDisplay()
  16.                 .getHeight();
  17.         // 去掉标题栏
  18.         // Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);
  19.         Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height
  20.                 - statusBarHeight);
  21.         view.destroyDrawingCache();
  22.         return b;
  23.     }

  24.     // 保存到sdcard
  25.     public static void savePic(Bitmap b, String strFileName) {
  26.         FileOutputStream fos = null;
  27.         try {
  28.             fos = new FileOutputStream(strFileName);
  29.             if (null != fos) {
  30.                 b.compress(Bitmap.CompressFormat.PNG, 90, fos);
  31.                 fos.flush();
  32.                 fos.close();
  33.             }
  34.         } catch (FileNotFoundException e) {
  35.             e.printStackTrace();
  36.         } catch (IOException e) {
  37.             e.printStackTrace();
  38.         }
  39.     }

  40. // 程序入口 截取当前屏幕
  41.     public static void shootLoacleView(Activity a,String picpath) {
  42.         ScreenShot.savePic(ScreenShot.takeScreenShot(a), picpath);
  43.     }
复制代码

当视图超过一个屏幕的时候,可能是listview,也可能是Scrollview,这时候,其实截图就是对listview或者Scrollview进行截图:

  1. /**
  2.      * 截取scrollview的屏幕
  3.      * **/
  4.     public static Bitmap getScrollViewBitmap(ScrollView scrollView,String picpath) {
  5.         int h = 0;
  6.         Bitmap bitmap;
  7.         // 获取listView实际高度
  8.         for (int i = 0; i < scrollView.getChildCount(); i++) {
  9.             h += scrollView.getChildAt(i).getHeight();
  10.         }
  11.         Log.d(TAG, "实际高度:" + h);
  12.         Log.d(TAG, " 高度:" + scrollView.getHeight());
  13.         // 创建对应大小的bitmap
  14.         bitmap = Bitmap.createBitmap(scrollView.getWidth(), h,
  15.                 Bitmap.Config.ARGB_8888);
  16.         final Canvas canvas = new Canvas(bitmap);
  17.         scrollView.draw(canvas);
  18.         // 测试输出
  19.         FileOutputStream out = null;
  20.         try {
  21.             out = new FileOutputStream(picpath);
  22.         } catch (FileNotFoundException e) {
  23.             e.printStackTrace();
  24.         }
  25.         try {
  26.             if (null != out) {
  27.                 bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
  28.                 out.flush();
  29.                 out.close();
  30.             }
  31.         } catch (IOException e) {
  32.         }
  33.         return bitmap;
  34.     }

  35.     private static String TAG = "Listview and ScrollView item 截图:";

  36.     /**
  37.      *  截图listview
  38.      * **/
  39.     public static Bitmap getListViewBitmap(ListView listView,String picpath) {
  40.         int h = 0;
  41.         Bitmap bitmap;
  42.         // 获取listView实际高度
  43.         for (int i = 0; i < listView.getChildCount(); i++) {
  44.             h += listView.getChildAt(i).getHeight();
  45.         }
  46.         Log.d(TAG, "实际高度:" + h);
  47.         Log.d(TAG, "list 高度:" + listView.getHeight());
  48.         // 创建对应大小的bitmap
  49.         bitmap = Bitmap.createBitmap(listView.getWidth(), h,
  50.                 Bitmap.Config.ARGB_8888);
  51.         final Canvas canvas = new Canvas(bitmap);
  52.         listView.draw(canvas);
  53.         // 测试输出
  54.         FileOutputStream out = null;
  55.         try {
  56.             out = new FileOutputStream(picpath);
  57.         } catch (FileNotFoundException e) {
  58.             e.printStackTrace();
  59.         }
  60.         try {
  61.             if (null != out) {
  62.                 bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
  63.                 out.flush();
  64.                 out.close();
  65.             }
  66.         } catch (IOException e) {
  67.         }
  68.         return bitmap;
  69.     }

  70.     // 程序入口 截取ScrollView
  71.     public static void shootScrollView(ScrollView scrollView,String picpath) {
  72.         ScreenShot.savePic(getScrollViewBitmap(scrollView, picpath), picpath);
  73.     }

  74.     // 程序入口 截取ListView
  75.     public static void shootListView(ListView listView,String picpath) {
  76.         ScreenShot.savePic(getListViewBitmap(listView,picpath), picpath);
  77.     }
复制代码

截长图的效果图:





捡代码论坛-最全的游戏源码下载技术网站! - 论坛版权郑重声明:
1、本主题所有言论和图片纯属会员个人意见,与本论坛立场无关
2、本站所有主题由该帖子作者发表,该帖子作者与捡代码论坛-最全的游戏源码下载技术网站!享有帖子相关版权
3、捡代码论坛版权,详细了解请点击。
4、本站所有内容均由互联网收集整理、网友上传,并且以计算机技术研究交流为目的,仅供大家参考、学习,不存在任何商业目的与商业用途。
5、若您需要商业运营或用于其他商业活动,请您购买正版授权并合法使用。 我们不承担任何技术及版权问题,且不对任何资源负法律责任。
6、如无法链接失效或侵犯版权,请给我们来信:jiandaima@foxmail.com

回复

使用道具 举报

*滑块验证:
您需要登录后才可以回帖 登录 | 立 即 注 册

本版积分规则

技术支持
在线咨询
QQ咨询
3351529868

QQ|手机版|小黑屋|捡代码论坛-专业源码分享下载 ( 陕ICP备15015195号-1|网站地图

GMT+8, 2024-4-20 07:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表