前段时间作的一个书店项目其阅读模块中用到了WebView + js,今天把WebView这块用到的几个特性记录下。html
其主要用到了webView的快照与屏幕的截屏。部分代码以下:web
[html] view plaincopycanvas
/** eclipse
* 截取webView可视区域的截图 spa
* @param webView 前提:WebView要设置webView.setDrawingCacheEnabled(true); .net
* @return orm
*/ htm
private Bitmap captureWebViewVisibleSize(WebView webView){ blog
Bitmap bmp = webView.getDrawingCache(); ip
return bmp;
}
这个方法只截取屏幕中显示出来部分的webView画面,未显示的部分不会被截取。
[html] view plaincopy
/**
* 截取webView快照(webView加载的整个内容的大小)
* @param webView
* @return
*/
private Bitmap captureWebView(WebView webView){
Picture snapShot = webView.capturePicture();
Bitmap bmp = Bitmap.createBitmap(snapShot.getWidth(),snapShot.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
snapShot.draw(canvas);
return bmp;
}
这个看好与上一个是不一样的,他是截取webView的整个页面,未显示的也会被截取。
[html] view plaincopy
/**
* 截屏
* @param context
* @return
*/
private Bitmap captureScreen(Activity context){
View cv = context.getWindow().getDecorView();
Bitmap bmp = Bitmap.createBitmap(cv.getWidth(), cv.getHeight(),Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
cv.draw(canvas);
return bmp;
[html] view plaincopy
}
这个不用多说你们都明白就是手机屏幕的快照~~
核心方法就这些了。知道你们都喜欢要项目源码,最好是下载下来能导入eclipse运行的(我是好喜欢这样滴博文)~~
我这写好的Demo是把截取的快照存储到SD卡中,须要的猛击这里哈~~~