Android实现分享长图并且添加全图水印
作者:lqh 时间:2021-08-04 19:30:57
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
标签:Android,水印图片
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
浅析Java中Apache BeanUtils和Spring BeanUtils的用法
2021-07-10 21:29:15
![](https://img.aspxhome.com/file/2023/4/63324_0s.jpg)
Java如何实现支付宝电脑支付基于servlet版本
2021-12-11 04:39:47
![](https://img.aspxhome.com/file/2023/7/81237_0s.png)
Android studio 出现 Unsupported major.minor version 52.0解决办法
2023-12-01 18:30:53
![](https://img.aspxhome.com/file/2023/5/138105_0s.png)
Java基础元注解基本原理示例详解
2022-08-09 23:21:16
![](https://img.aspxhome.com/file/2023/4/67984_0s.png)
java二叉树的几种遍历递归与非递归实现代码
2022-06-29 19:19:47
![](https://img.aspxhome.com/file/2023/7/64787_0s.jpg)
基于Mybatis Plus实现代码生成器CodeGenerator
2021-09-28 12:18:21
![](https://img.aspxhome.com/file/2023/4/130904_0s.png)
Java 异常的栈轨迹(Stack Trace)详解及实例代码
2023-12-13 12:19:02
C#把数字转换成大写金额的代码实例
2022-03-21 08:08:48
Android仿美团外卖菜单界面
2022-03-28 02:28:35
IDEA配置JRebel实现热部署的方法
2022-08-28 20:53:00
![](https://img.aspxhome.com/file/2023/1/100051_0s.png)
c# 使用异步编程的方法
2023-09-29 02:27:32
![](https://img.aspxhome.com/file/2023/7/113777_0s.png)
java开发之内部类的用法
2023-02-04 21:30:07
Android通过交互实现贝塞尔曲线的绘制
2022-10-06 05:04:08
![](https://img.aspxhome.com/file/2023/5/103905_0s.png)
Android之获取手机内部及sdcard存储空间的方法
2022-11-11 13:10:18
SpringMVC响应视图和结果视图详解
2022-03-07 05:08:37
Java继承方法重写实现原理及解析
2021-12-31 16:35:54
![](https://img.aspxhome.com/file/2023/1/71671_0s.png)
c#深拷贝文件夹示例
2023-07-24 07:50:40
cmd中javac和java使用及注意事项详解
2022-03-25 10:37:36
![](https://img.aspxhome.com/file/2023/5/101925_0s.png)
Java计时器StopWatch实现方法代码实例
2021-07-25 13:43:42
java中复杂查询sql语句该怎么写
2021-06-20 01:25:24
![](https://img.aspxhome.com/file/2023/9/69939_0s.png)