android分享功能以及界面开发,Android实现合并生成分享图片功能

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

发布时间:2020-08-23 11:43:07

来源:脚本之家

阅读:159

作者:Chaek

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

Android 自定义View课程表表格

6b1ef5afe90426741ef08b1c06b3ad98.png

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值