Android给scrollView截图超过屏幕大小形成长图

很多的时候,我们想要分享一个界面的所有内容,可是内容太多,超过了屏幕的大小,简单的截屏已经满足不了我们的需要,这时候我们就可以根据布局里scrollView的高度来截取图片。

代码如下:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.      * 截取scrollview的屏幕 
  3.      * @param scrollView 
  4.      * @return 
  5.      */  
  6.     public static Bitmap getBitmapByView(ScrollView scrollView) {  
  7.         int h = 0;  
  8.         Bitmap bitmap = null;  
  9.         // 获取scrollview实际高度  
  10.         for (int i = 0; i < scrollView.getChildCount(); i++) {  
  11.             h += scrollView.getChildAt(i).getHeight();  
  12.             scrollView.getChildAt(i).setBackgroundColor(  
  13.                     Color.parseColor("#ffffff"));  
  14.         }  
  15.         // 创建对应大小的bitmap  
  16.         bitmap = Bitmap.createBitmap(scrollView.getWidth(), h,  
  17.                 Bitmap.Config.RGB_565);  
  18.         final Canvas canvas = new Canvas(bitmap);  
  19.         scrollView.draw(canvas);  
  20.         return bitmap;  
  21.     }  
  22.   
  23.     /** 
  24.      * 压缩图片 
  25.      * @param image 
  26.      * @return 
  27.      */  
  28.     public static Bitmap compressImage(Bitmap image) {  
  29.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  30.         // 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中  
  31.         image.compress(Bitmap.CompressFormat.JPEG, 100, baos);  
  32.         int options = 100;  
  33.         // 循环判断如果压缩后图片是否大于100kb,大于继续压缩  
  34.         while (baos.toByteArray().length / 1024 > 100) {  
  35.             // 重置baos  
  36.             baos.reset();  
  37.             // 这里压缩options%,把压缩后的数据存放到baos中  
  38.             image.compress(Bitmap.CompressFormat.JPEG, options, baos);  
  39.             // 每次都减少10  
  40.             options -= 10;  
  41.         }  
  42.         // 把压缩后的数据baos存放到ByteArrayInputStream中  
  43.         ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());  
  44.         // 把ByteArrayInputStream数据生成图片  
  45.         Bitmap bitmap = BitmapFactory.decodeStream(isBm, nullnull);  
  46.         return bitmap;  
  47.     }  
  48.   
  49. /** 
  50.      * 保存到sdcard 
  51.      * @param b 
  52.      * @return 
  53.      */  
  54.     public static String savePic(Bitmap b) {  
  55.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss",  
  56.                 Locale.US);  
  57.         File outfile = new File("/sdcard/image");  
  58.         // 如果文件不存在,则创建一个新文件  
  59.         if (!outfile.isDirectory()) {  
  60.             try {  
  61.                 outfile.mkdir();  
  62.             } catch (Exception e) {  
  63.                 e.printStackTrace();  
  64.             }  
  65.         }  
  66.         String fname = outfile + "/" + sdf.format(new Date()) + ".png";  
  67.         FileOutputStream fos = null;  
  68.         try {  
  69.             fos = new FileOutputStream(fname);  
  70.             if (null != fos) {  
  71.                 b.compress(Bitmap.CompressFormat.PNG, 90, fos);  
  72.                 fos.flush();  
  73.                 fos.close();  
  74.             }  
  75.         } catch (FileNotFoundException e) {  
  76.             e.printStackTrace();  
  77.         } catch (IOException e) {  
  78.             e.printStackTrace();  
  79.         }  
  80.         return fname;  
  81.     }  


在需要用到的地方调用getBitmapByView()方法 即可:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. String fname = ScreenShot.savePic(ScreenShot.getBitmapByView(scrollView));  

但是这样写的话有时候会因为截取的图片太长太大而报outofmemory的错,所以为了避免内存溢出,程序崩掉,要注意用Config.RGB_565,会比ARGB_8888少占内存。还有就是把图片压缩一下,至少我这样就没有报oom的错了,即:
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. String fname = ScreenShot.savePic(ScreenShot.compressImage(ScreenShot  
  2.                         .getBitmapByView(scrollView)));  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值