android webView截长图
来源:互联网 发布:胡金铨 知乎 编辑:程序博客网 时间:2024/06/06 15:36
android 在webView里面截图大概有四种方式.
1.获取到DecorView然后将DecorView转换成bitmap然后写入到文件里面.
View view = getWindow().getDecorView(); Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); view.draw(canvas); Log.d(TAG,"bitmap--"+bitmap); try { String fileName = Environment.getExternalStorageDirectory().getPath()+"/webview_jietu.jpg"; FileOutputStream fos = new FileOutputStream(fileName); //压缩bitmap到输出流中 bitmap.compress(Bitmap.CompressFormat.JPEG, 70, fos); fos.close(); Toast.makeText(WebviewFromGetDecorView.this, "截屏成功", Toast.LENGTH_LONG).show(); } catch (Exception e) { Log.e(TAG, e.getMessage()); }finally { if(bitmap!=null) { bitmap.recycle(); } }
2.使用webViewpicture来实现该功能.(该方法被废弃了因此不建议使用)
Picture picture = webView.capturePicture(); int width = picture.getWidth(); int height = picture.getHeight(); if (width > 0 && height > 0) { Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); picture.draw(canvas); try { String fileName = Environment.getExternalStorageDirectory().getPath()+"/webview_jietu.jpg"; FileOutputStream fos = new FileOutputStream(fileName); //压缩bitmap到输出流中 bitmap.compress(Bitmap.CompressFormat.JPEG, 70, fos); fos.close(); Toast.makeText(WebviewFromCapture.this, "截屏成功", Toast.LENGTH_LONG).show(); bitmap.recycle(); } catch (Exception e) { Log.e(TAG, e.getMessage()); } }
3.使用webViewDraw来实现.(该方法被废弃了因此不建议使用)
float scale = webView.getScale(); int webViewHeight = (int) (webView.getContentHeight()*scale+0.5); Bitmap bitmap = Bitmap.createBitmap(webView.getWidth(),webViewHeight, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); webView.draw(canvas); try { String fileName = Environment.getExternalStorageDirectory().getPath()+"/webview_jietu.jpg"; FileOutputStream fos = new FileOutputStream(fileName); //压缩bitmap到输出流中 bitmap.compress(Bitmap.CompressFormat.JPEG, 70, fos); fos.close(); Toast.makeText(WebviewFromDraw.this, "截屏成功", Toast.LENGTH_LONG).show(); bitmap.recycle(); } catch (Exception e) { Log.e(TAG, e.getMessage()); }
4.使用webViewDrawCache来实现(建议使用).
Bitmap bitmap = webView.getDrawingCache(); try { String fileName = Environment.getExternalStorageDirectory().getPath() + "/webview_jietu.jpg"; FileOutputStream fos = new FileOutputStream(fileName); //压缩bitmap到输出流中 bitmap.compress(Bitmap.CompressFormat.JPEG, 70, fos); bitmap.recycle(); fos.close(); Toast.makeText(WebviewFromDrawCache.this, "截屏成功", Toast.LENGTH_LONG).show(); } catch (Exception e) { Log.e(TAG, e.getMessage()); } finally { bitmap.recycle(); }
注意:
在android5.0及以上版本使用webView进行截长图时,默认是截取可是区域内的内容.因此需要在支撑窗体内容之前加上如下方法.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { WebView.enableSlowWholeDocumentDraw(); } setContentView(R.layout.activity_webview);
阅读全文
0 0
- android webView截长图
- 【Android】【Webview】WebView 简介
- android WebView
- Android WebView
- Android WebView
- android webview
- Android WebView
- android WebView
- android WebView
- Android webview
- android webview
- android webview
- Android WebView
- Android WebView
- android WebView
- android webview
- android-webview
- android webview
- sublime3快速生成类或者方法的注释
- cellForRowAtIndexPath不执行
- oracle 联表更新 运行原理及效率
- ICEM技巧——恢复删除的块
- Oracle Nvl函数的正确用
- android webView截长图
- Oracle 11G RAC 生成AWR报告总结
- Ubuntu sudo 出现unable to resolve host 解决方法
- HDU3652_B-number_数位DP&记忆化搜索
- 消除数组中重复的元素
- Android进阶之路
- 轮播图加载Imageloader图片,Xlistview上拉下拉
- CommonUtils
- 挑战ACM迷宫 DFS+剪枝 BFS