Android实现分享长图并且添加全图水印
作者:lqh 发布时间:2021-08-04 19:30:57
标签:Android,水印图片
Android实现分享长图并且添加全图水印
前言:
长图一般是ScrollView和ListView。
我们需要取得这两个控件的完整显示的图片。原理很简单,搞一张和控件长宽一致的画布(就是创建一个高宽相等的bitmap)。然后调用控件的draw方法把自己画到画布上去。
分别贴出两个控件的长图获取方法
/**
* 截取scrollview的屏幕
**/
public static Bitmap getScrollViewBitmap(ScrollView scrollView) {
int h = 0;
Bitmap bitmap;
for (int i = 0; i < scrollView.getChildCount(); i++) {
h += scrollView.getChildAt(i).getHeight();
}
// 创建对应大小的bitmap
bitmap = Bitmap.createBitmap(ScreenUtils.getScreenWidth(scrollView.getContext()), h,
Bitmap.Config.ARGB_4444);
final Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.parseColor("#f2f7fa"));
scrollView.draw(canvas);
return bitmap;
}
/**
* 截图listview
**/
public static Bitmap getListViewBitmap(ListView listView, String picpath) {
int h = 0;
Bitmap bitmap;
// 获取listView实际高度
for (int i = 0; i < listView.getChildCount(); i++) {
h += listView.getChildAt(i).getHeight();
}
listView.getHeight());
// 创建对应大小的bitmap
bitmap = Bitmap.createBitmap(listView.getWidth(), h,
Bitmap.Config.RGB_565);
final Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.WHITE);
listView.draw(canvas);
// 测试输出
FileOutputStream out = null;
try {
out = new FileOutputStream(picpath);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
if (null != out) {
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
}
} catch (IOException e) {
}
return bitmap;
}
奉送个获取具体view的显示图的方法
/**
* 生成某个view的图片
*
* @author gengqiquan
* @date 2017/3/20 上午10:34
*/
public static Bitmap getViewDrawingCacheBitmap(View view) {
view = view.getRootView();
if (!view.isDrawingCacheEnabled()) {
view.setDrawingCacheEnabled(true);
}
view.destroyDrawingCache();
view.buildDrawingCache();
Bitmap bm = view.getDrawingCache();
view.setDrawingCacheEnabled(false);
return bm;
}
再奉送个生成某个LinearLayout图片的方法
/**
* 生成某个LinearLayout的图片
*
* @author gengqiquan
* @date 2017/3/20 上午10:34
*/
public static Bitmap getLinearLayoutBitmap(LinearLayout linearLayout) {
int h = 0;
// 获取LinearLayout实际高度
for (int i = 0; i < linearLayout.getChildCount(); i++) {
linearLayout.getChildAt(i).measure(0, 0);
h += linearLayout.getChildAt(i).getMeasuredHeight();
}
linearLayout.measure(0, 0);
// 创建对应大小的bitmap
Bitmap bitmap = Bitmap.createBitmap(linearLayout.getMeasuredWidth(), h,
Bitmap.Config.RGB_565);
final Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.WHITE);
linearLayout.draw(canvas);
return bitmap;
}
完了产品肯定会让你在下面或者上面加上公司的logo图片的,嗯。好人做到低,再送个拼接图片的方法
/**
*拼接图片
* @param first 分享的长图
* @param second 公司logo图
*@author gengqiquan
*@date 2017/3/25 下午4:56
*/
public static Bitmap add2Bitmap(Bitmap first, Bitmap second) {
float scale = ((float) first.getWidth()) / second.getWidth();
second = ImageUtil.scaleImg(second, scale);
int width = first.getWidth();
int height = first.getHeight() + second.getHeight();
Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
Canvas canvas = new Canvas(result);
canvas.drawBitmap(first, 0, 0, null);
canvas.drawBitmap(second, 0, first.getHeight(), null);
return result;
}
再来个添加全图水印的方法
/**
* @param first 原始图
* @param mark 水印图
* @author gengqiquan
* @date 2017/3/25 下午4:58
*/
public static Bitmap waterMark(Bitmap first, Bitmap mark) {
float scale = ((float) first.getWidth()) / mark.getWidth();
mark = ImageUtil.scaleImg(mark, scale);
int width = first.getWidth();
int height = first.getHeight();
Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
Canvas canvas = new Canvas(result);
canvas.drawBitmap(first, 0, 0, null);
int h = 0;
while (h < height + mark.getHeight()) {
canvas.drawBitmap(mark, 0, h, null);
h = h + mark.getHeight();
}
return result;
}
坑爹的chrome有bug。一在这个位置点换行接向上按键就崩溃。写个博客崩溃了七八次。最近真的是运气不好。
其实我是想说:由于最近被注入了个对象,依赖性比较强,所以这段时间很少写博客了,后面补上。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
来源:http://blog.csdn.net/gengqiquan/article/details/65938021
0
投稿
猜你喜欢
- 不废话了,直接给大家贴代码了。class term { String str; int id; &
- 一:将String字符串放在最前面防止发生NullPointerException异常,我们通常把String字符串放在equals方法的左
- 延迟队列的使用场景还比较多,例如:1、超时未收到支付回调,主动查询支付状态;2、规定时间内,订单未支付,自动取消;。。。总之,但凡需要在未来
- 使用Java的方式配置Spring我们现在要完全不使用Spring的xml配置,全权使用Java来配置Spring!JavaConfig是S
- springboottest测试依赖和使用<dependency> <groupId>or
- Select Count(*)的返回值类型<select id="queryAlarmStatisticalAnalysis
- 一、类加载流程类加载的流程可以简单分为三步:加载连接初始化而其中的连接又可以细分为三步:验证准备解析下面会分别对各个流程进行介绍。1.1 类
- 服务器提交了协议冲突. Section=ResponseHeader Detail=CR 后面必须是 
- Android使用RecyclerView1. 什么是RecyclerViewRecyclerView 是 Android-support-
- 职责链模式简介及UML职责链也叫责任链,他是一种行为型模式,它为请求创建了一个接收请求者对象的链,并将请求沿着这条链传递到目标对象去处理。该
- 1,IDEA中Lombok作用数据库: 库 表 字段 对应的值 user表(id,name,age)实体对象pojo: 用来封装数据库中的数
- NPOI 2.0将DataTable对象转换为Excel 2007文件提供下载using NPOI.SS.UserModel;using N
- Java空字符串与null的区别:1、类型 null表示的是一个对象的值,而并不是一个字符串。例如声明一个对象的引用,String a =
- 引言: 在Spring Boot应用中,基于数据某个字段进行排序是一个非常常用的需求,这里将给出Sort的三种常用用法,基于分页的应用,大家
- 一、问题描述我们开发项目时,经常会和第三方接口打交道,有些第三方项目为了安全起见,会把项目部署在"内网"环境,不对外暴露
- 自己写了一个Swap测试类,代码如下:swap不能交换原生数据类型以及字符串类型。public class Swap5{ public st
- cookie和session的区别和联系cookie是本地客户端用来存储少量数据信息的,保存在客户端,用户能够很容易的获取,安全性不高,存储
- 之前的文章都是讲解springmvc+spring+mybatis 的整合,而很少有springmvc+spring+hibernate 因
- 前言最近新整了个博客网站,同事在gitee上找的,还不错,gitee上的地址在这里:拾壹博客管理系统。别人的业务,再好也有不满足自己的地方,
- 1. 为什么写这篇文章?事情是这样的,在 2021年6月10日早上我在CSDN上发布了文章《你真的懂Java怎么输出Hello World吗