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

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

成都创新互联公司专注于企业全网整合营销推广、网站重做改版、灵川网站定制设计、自适应品牌网站建设、H5开发商城系统网站开发、集团公司官网建设、外贸网站制作、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为灵川等各大城市提供网站开发制作服务。

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实现合并生成分享图片功能
标题URL:http://bzwzjz.com/article/gdecog.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 广安网站设计 高端品牌网站建设 营销型网站建设 成都网站建设 网站制作 成都企业网站建设 成都网站制作公司 企业网站建设 成都企业网站建设公司 网站建设开发 移动手机网站制作 成都网站设计 成都网站建设 网站设计制作报价 成都网站建设公司 成都网站建设 企业手机网站建设 成都h5网站建设 盐亭网站设计 网站设计公司 成都网站建设 宜宾网站设计