当前位置: 移动技术网 > IT编程>移动开发>Android > Android实现合并生成分享图片功能

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

2019年07月23日  | 移动技术网IT编程  | 我要评论

都市摩天楼java,呼和浩特车管所网站,刘玄真吧

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

android 自定义view课程表表格

看到的是图片只显示到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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网