Android实现合并生成分享图片功能

发布时间:2019-08-08 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Android实现合并生成分享图片功能脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

有时候分享功能都是很需要分享一个当前屏幕的界面的截图因,以前做校内APP的时候用到过,拿出来分享分享, 用以前写过的自定义课表软件。

Android 自定义View课程表表格

Android实现合并生成分享图片功能

看到的是图片只显示到11节处,下面的没有显示到 所以用到的 ScrollView

因此截图节截取ScrollView View的图片

一、首先计算出整个ScrollView 的高度度生成对应大小的的BITmap 然后把使用Canvas 将ScrollView 的界面绘制上去

 // 获取ScrollView 实际高度  h = 0;  for (int i = 0; i < scrollView.getChildCount(); i++) {   h += scrollView.getChildAt(i).getHeight();   scrollView.getChildAt(i).setBackgroundResource(andROId.R.color.white);  }  // 创建对应大小的bitmap  Bitmap bitmap = Bitmap.createBitmap(scrollView.getWidth(), h, Bitmap.config.argB_8888);  Canvas canvas = new Canvas(bitmap);  scrollView.draw(canvas); 

二、获取分享的头部和底部图片的Bitmap

 // BitmapFactory.decodeResource函数直接转换资文件为Bitmap    Bitmap head = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.share_term_table_header);  Bitmap foot = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.share_term_table_footer);

三、合并头部底部和界面View的截图

 if (head == null) {   return null;  }  int headWidth = head.getWidth();  int kebianwidth = kebiao.getWidth();  int fotwid = san.getWidth();   int headHeight = head.getHeight();  int kebiaoheight = kebiao.getHeight();  int footerheight = san.getHeight();  //生成三个图片合并大小的Bitmap  Bitmap newbmp = Bitmap.createBitmap(kebianwidth, headHeight + kebiaoheight + footerheight, Bitmap.Config.ARGB_8888);  Canvas cv = new Canvas(newbmp);  cv.drawBitmap(head, 0, 0, null);// 在 0,0坐标开始画入headBitmap   //因为手机不同图片的大小的可能小了 就绘制白色的界面填充剩下的界面  if (headWidth < kebianwidth) {   System.out.PRintln("绘制头");   Bitmap ne = Bitmap.createBitmap(kebianwidth - headWidth, headHeight, Bitmap.Config.ARGB_8888);   Canvas canvas = new Canvas(ne);   canvas.drawColor(Color.WHITE);   cv.drawBitmap(ne, headWidth, 0, null);  }  cv.drawBitmap(kebiao, 0, headHeight, null);// 在 0,headHeight坐标开始填充课表的Bitmap  cv.drawBitmap(san, 0, headHeight + kebiaoheight, null);// 在 0,headHeight + kebiaoheight坐标开始填充课表的Bitmap  //因为手机不同图片的大小的可能小了 就绘制白色的界面填充剩下的界面  if (fotwid < kebianwidth) {   System.out.println("绘制");   Bitmap ne = Bitmap.createBitmap(kebianwidth - fotwid, footerheight, Bitmap.Config.ARGB_8888);   Canvas canvas = new Canvas(ne);   canvas.drawColor(Color.WHITE);   cv.drawBitmap(ne, fotwid, headHeight + kebiaoheight, null);  }  cv.save(Canvas.ALL_SAVE_FLAG);// 保存  cv.reStore();// 存储  //回收  head.recycle();  kebiao.recycle();  san.recycle();

下载地址

环境Android Studio

csdn下载地址

查看GIT

android教程
脚本网站
android studio

脚本宝典总结

以上是脚本宝典为你收集整理的Android实现合并生成分享图片功能全部内容,希望文章能够帮你解决Android实现合并生成分享图片功能所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。