webview长截图与短截图+滑动控件ScrollView长截图保存在本地
2017-11-24 15:29
337 查看
需求里需要进行控件的截图,webview试了多种方法不行,最后找到一种稳定的方法。
以下代码分:短截图,长截图,保存在本地如下:
webview长截图在5.0以上手机请先添加此方法:
短截图方法:
长截图方法
ScrollView长截图方法:
保存在本地方法:
网上有一种长截图方法,测试后不能用,知道原因请回复探讨:
以下代码分:短截图,长截图,保存在本地如下:
webview长截图在5.0以上手机请先添加此方法:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { android.webkit.WebView.enableSlowWholeDocumentDraw(); } setContentView(R.layout.e_cc);
短截图方法:
public void getcurret() { Bitmap bitmap = Bitmap.createBitmap(mWebView.getWidth(), mWebView.getHeight(), Bitmap.Config.RGB_565); Canvas canvas = new Canvas(bitmap); canvas.drawBitmap(bitmap, mWebView.getWidth(), mWebView.getHeight(), new Paint()); mWebView.draw(canvas); save(bitmap, "wc.jpg"); }
长截图方法
public void getlong() { float scale = mWebView.getScale(); Log.e("scale:", scale + ""); int width = mWebView.getWidth(); int height = (int) (mWebView.getContentHeight() * scale + 0.5); Bitmap longImage = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); Canvas canvas = new Canvas(longImage); mWebView.draw(canvas); save(longImage, "wl.jpg"); }
ScrollView长截图方法:
public void getSV() { int h = 0; for (int i = 0; i < mScrollView.getChildCount(); i++) { View view = mScrollView.getChildAt(i); h = view.getHeight() + h; } Bitmap bitmap = Bitmap.createBitmap(mScrollView.getWidth(), h, Bitmap.Config.RGB_565); Canvas canvas = new Canvas(bitmap); mScrollView.draw(canvas); save(bitmap, "s.jpg"); }
保存在本地方法:
public void save(Bitmap bitmap, String s2) { String s1 = Environment.getExternalStorageDirectory().getAbsolutePath(); File file = new File(s1, "wt"); if (!file.exists()) { file.mkdirs(); } FileOutputStream out = null; File file1 = new File(file, s2); try { out = new FileOutputStream(file1); } catch (FileNotFoundException e) { e.printStackTrace(); } try { if (null != out) { bitmap.compress(Bitmap.CompressFormat.JPEG, 20, out); out.flush(); out.close(); Log.e("save:", "完成"); } } catch (IOException e) { e.printStackTrace(); } }
网上有一种长截图方法,测试后不能用,知道原因请回复探讨:
mWebView.measure(View.MeasureSpec.makeMeasureSpec(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); mWebView.layout(0, 0, mWebView.getMeasuredWidth(), mWebView.getMeasuredHeight()); mWebView.setDrawingCacheEnabled(true); mWebView.buildDrawingCache(); Bitmap longImage = Bitmap.createBitmap(mWebView.getMeasuredWidth(), mWebView.getMeasuredHeight(), Bitmap.Config.RGB_565); Canvas canvas = new Canvas(longImage); // 画布的宽高和 WebView 的网页保持一致 Paint paint = new Paint(); canvas.drawBitmap(longImage, 0, mWebView.getMeasuredHeight(), paint); mWebView.draw(canvas);
相关文章推荐
- 重新想象 Windows 8.1 Store Apps (81) - 控件增强: WebView 之加载本地 html, 智能替换 html 中的 url 引用, 通过 Share Contract 分享 WebView 中的内容, 为 WebView 截图
- android控件-ScrollView 和WebView之见滑动冲突解决
- webView截图保存到本地
- ScrollView中若嵌套有滑动的控件(如:WebView,ListView或GridView),另外该界面中还有其它的控件时,界面不显示最上面控件的问题。
- ScrollView中若嵌套有滑动的控件(如:WebView,ListView或GridView)时的焦点问题
- 可滑动控件(ListView,WebView,ScrollView)实现弹性滑动或者取消弹性滑动(上下或左右滑动)
- Android控件-ScrollView 和WebView之见滑动冲突解决
- ScrollView中若嵌套有滑动的控件(如:WebView,ListView或GridView)焦点问题
- 重新想象 Windows 8.1 Store Apps (81) - 控件增强: 加载本地 html, 智能替换 html 中的 url 引用, 通过 Share Contract 分享 WebView 中的内容, 为 WebView 截图
- Android webview长按图片保存到本地
- 基于webview的选择滑动控件(PC和wap版)
- 仿淘宝的商品详情拖动,ViewPager和ScrollView WebView的滑动冲突
- android WebView 控件加载本地sdcard中html文件图片的问题
- webview随scrollview一起滑动
- WebView控件中的javascript与Android本地功能交互
- Android控件:WebVIew(二)显示网页或本地html文件
- Android控件之WebView ScrollView问题录
- Android截屏截图方法汇总(Activity、View、ScrollView、ListView、RecycleView、WebView截屏截图)
- php web屏幕截图上传服务器和保存本地功能
- 系出名门Android(7) - 控件(View)之ZoomControls, Include, VideoView, WebView, RatingBar, Tab, Spinner, Chronometer, ScrollView