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

发布时间:2020-08-23 11:43:07 作者:Chaek
来源:脚本之家 阅读:404

有时候分享功能都是很需要分享一个当前屏幕的界面的截图因,以前做校内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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

推荐阅读:
  1. Android布局如何生成分享图片
  2. android7.0实现分享图片到朋友圈功能

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android 分享图片 roi

上一篇:微信小程序 实现动态显示和隐藏某个控件

下一篇:JS字符串与二进制的相互转化实例代码详解

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》