前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android实现合并生成分享图片功能

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

作者头像
砸漏
发布2020-11-05 10:42:51
1.3K0
发布2020-11-05 10:42:51
举报
文章被收录于专栏:恩蓝脚本恩蓝脚本

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

Android 自定义View课程表表格

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

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

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

代码语言:javascript
复制
// 获取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

代码语言:javascript
复制
// 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的截图

代码语言:javascript
复制
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

以上就是本文的全部内容,希望对大家的学习有所帮助。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档