Android使用WebView实现截图分享功能
作者:-仰望星空- 时间:2023-04-17 08:03:48
在APP项目的开发过程中,经常会用到分享图片的功能,有时候还需要根据当前用户信息获取指定的分享图片,比如要求在用户分享图中显示用户名、Uid、用户头像等信息。想到的实现方法主要有两点:
1.通过android SDK自带的Canvas方法进行绘制。
2.通过webView实现客户端与H5交互,然后将H5界面做截图处理。
本文主要介绍第二种方式的实现过程,第一种方式的实现方法,后续有时间会在博客中做说明,下面开始本文内容。
首先确定我们要实现的逻辑:
1.客户端与H5的交互,客户端将用户信息(用户名、Uid、用户头像等)发送给H5;
2.客户端截取WebView功能的实现;
3.分享功能的添加。
1.客户端与H5交互
在界面布局中添加webView布局,对WebView布局进行初始化操作(此处需要网络权限,不做单独处理)
wv_imgweb = (WebView) findViewById(R.id.h5_wv_imgweb);
WebSettings webSettings = wv_imgweb.getSettings();
//此处可更加具体的H5界面功能进行相应的WebSettings设置,本文只是演示基本效果
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(false);
wv_imgweb.requestFocusFromTouch();
wv_imgweb.setDrawingCacheEnabled(true);
wv_imgweb.setVerticalScrollBarEnabled(false);
wv_imgweb.setHorizontalScrollBarEnabled(false);
wv_imgweb.setVerticalScrollbarOverlay(false);
wv_imgweb.setHorizontalScrollbarOverlay(false);
wv_imgweb.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
//加载逻辑的处理
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
//加载逻辑的处理
}
});
//添加用户信息参数,加载H5分享地址
wv_imgweb.loadUrl(h5_url);
2.截图功能的实现
上文中的WebView页面加载完成后,即可调用截图功能,并将图片保存在本地指定文件夹(此处需要存储权限,不做单独处理)
android中WebView可以通过多种方式实现截图,下面分别做不同介绍:
2.1截取屏幕内内容
第一种方式是通过Bitmap bitmap = webView.getDrawingCache()截取屏幕内显示内容,注意调用此方法setDrawingCacheEnabled(true)必须设置为true;
2.2截取整个WebView内容
第二种方式是通过Picture snapShot = wv_imgweb.capturePicture()来截取整个WebView的内容
Picture snapShot = wv_imgweb.capturePicture();
if (snapShot != null && snapShot.getWidth() > 0 && snapShot.getHeight() > 0) {
Bitmap bitmap = Bitmap.createBitmap(snapShot.getWidth(), snapShot.getHeight(), Bitmap.Config.ARGB_8888);//设置相应的图片质量
Canvas canvas = new Canvas(bitmap);
snapShot.draw(canvas);
//将截取的图片保存到本地
try {
File appFile = new File(Environment.getExternalStorageDirectory() + "/testpic/app");
if (!appFile.exists() && !appFile.isDirectory()) {
appFile.mkdirs();
}
String fileName = Environment.getExternalStorageDirectory().getPath() + "/testpic/app/share.jpg";
FileOutputStream fos = new FileOutputStream(fileName);
//设置保存本地图片质量
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, fos);
fos.close();
} catch (Exception e) {
UIUtils.setLogInfo("eee", e.getMessage());
}
}
2.3截取整个WebView内容
Android 为了提高各方面的绘制速度(如滚动操作),为每一个 View 建立一个缓存,使用 View.buildDrawingCache 为自己的 View 建立相应的缓存, 这个 cache 就是一个 bitmap 对象。利用这个功能可以对整个屏幕视图进行截屏并生成 Bitmap ,也可以获得指定的 View 的 Bitmap 对象。
wv_imgweb.measure(View.MeasureSpec.makeMeasureSpec(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
wv_imgweb.layout(0, 0, wv_imgweb.getMeasuredWidth(), wv_imgweb.getMeasuredHeight());
wv_imgweb.setDrawingCacheEnabled(true);
wv_imgweb.buildDrawingCache();
Bitmap longImage = Bitmap.createBitmap(wv_imgweb.getMeasuredWidth(),
wv_imgweb.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(longImage); // 画布的宽高和 WebView 保持一致
Paint paint = new Paint();
canvas.drawBitmap(longImage, 0, wv_imgweb.getMeasuredHeight(), paint);
wv_imgweb.draw(canvas);
//将截取的图片保存到本地
try {
File appFile = new File(Environment.getExternalStorageDirectory() + "/testpic/app");
if (!appFile.exists() && !appFile.isDirectory()) {
appFile.mkdirs();
}
String fileName = Environment.getExternalStorageDirectory().getPath() + "/testpic/app/share.jpg";
FileOutputStream fos = new FileOutputStream(fileName);
longImage.compress(Bitmap.CompressFormat.JPEG, 70, fos);
fos.close();
} catch (Exception e) {
UIUtils.setLogInfo("eee", e.getMessage());
}
问题:在5.0+上会发现,截取的快照只显示了webview中显示出来的那部分,没有显示出来的部分是空白的。通过google找到了原因,在5.0+版本上,Android对webview做了优化,旨在减少内存占用以提高性能。因此在默认情况下会智能的绘制html中需要绘制的部分,其实就是当前屏幕展示的html内容,因此会出现未显示的图像是空白的。解决办法是调用enableSlowWholeDocumentDraw()方法。这个方法需要在webview创建之前调用,在Activity里就是在setContentView前去调用,此方法会有显著的性能开销。
3.分享图片
通过原生或第三方分享功能,将上述生成的图片添加到代码中,完成分享。
如果有不同的见解,欢迎留言指正。
来源:https://blog.csdn.net/lhy349/article/details/79710468
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解处理Java中的大对象的方法
![](https://img.aspxhome.com/file/2023/1/81911_0s.jpg)
SpringBoot JPA使用配置过程详解
C#实现剪刀石头布游戏
![](https://img.aspxhome.com/file/2023/7/91377_0s.jpg)
Android中GridView和ArrayAdapter用法实例分析
详解Java中的泛型
![](https://img.aspxhome.com/file/2023/4/83564_0s.png)
详细讲解Java的泛型
Spring整合Junit的使用详解
Windows 10卸载JDK1.8超详细图文教程
![](https://img.aspxhome.com/file/2023/9/98399_0s.jpg)
java语言注解基础概念详解
![](https://img.aspxhome.com/file/2023/2/102632_0s.jpg)
c# webapi 配置swagger的方法
浅谈@Value和@Bean的执行顺序问题
23种设计模式(19)java责任链模式
![](https://img.aspxhome.com/file/2023/7/89487_0s.jpg)
浅析Android Service中实现弹出对话框的坑
C# 获取文件夹里所有文件名的详细代码
Java新特性之Nashorn_动力节点Java学院整理
![](https://img.aspxhome.com/file/2023/2/62822_0s.png)
分享WCF聊天程序--WCFChat实现代码
![](https://img.aspxhome.com/file/2023/8/83248_0s.jpg)
ConcurrentHashMap是如何实现线程安全的你知道吗
![](https://img.aspxhome.com/file/2023/5/60195_0s.jpg)
C# 根据字符串生成二维码的实例代码
![](https://img.aspxhome.com/file/2023/4/79084_0s.png)
SpringBoot-JPA删除不成功,只执行了查询语句问题
SpringBoot JSON全局日期格式转换器实现方式
![](https://img.aspxhome.com/file/2023/4/132204_0s.png)