android webView的快照与屏幕的截屏。部分代码如下:
[html]
/**
* 截取webView可视区域的截图
* @param webView 前提:WebView要设置webView.setDrawingCacheEnabled(true);
* @return
*/
private Bitmap captureWebViewVisibleSize(WebView webView){
Bitmap bmp = webView.getDrawingCache();
return bmp;
}
/**
* 截取webView可视区域的截图
* @param webView 前提:WebView要设置webView.setDrawingCacheEnabled(true);
* @return
*/
private Bitmap captureWebViewVisibleSize(WebView webView){
Bitmap bmp = webView.getDrawingCache();
return bmp;
}
这个方法只截取屏幕中显示出来部分的webView画面,未显示的部分不会被截取。
[html]
/**
* 截取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快照(webView加载的整个内容的大小)
* @param webView
* @return
*/ www.2cto.com
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]
/**
* 截屏
* @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;
}
/**
* 截屏
* @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;
}
/**
* 截取可见屏幕部分的view视图
* @param activity
* @return
*/
public static Bitmap shotScreen(Activity activity) {
View view = activity.getWindow().getDecorView();
Display display = activity.getWindowManager().getDefaultDisplay();
view.layout(0, 0, display.getWidth(), display.getHeight());
// 允许当前窗口保存缓存信息,这样getDrawingCache()方法才会返回一个Bitmap
view.setDrawingCacheEnabled(true);
Bitmap bmp = Bitmap.createBitmap(view.getDrawingCache());
return bmp;
}
看看下面例子: /** * 此demo实现了截屏并将截图放到指定的控件上缩放显示 * 技术要点: * 1.截屏 * 2.缩放功能 * @author: 严程 * @CreateDate: 2012-08-09 */ public class Demo4Activity extends Activity { private Button but; private ImageButton img; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); but = (Button)findViewById(R.id.but); but.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 实现截屏 View view = Demo4Activity.this.getWindow().getDecorView(); // 启动缓存 view.setDrawingCacheEnabled(true); // 接收数据 Bitmap bitmap = view.getDrawingCache(); img.setImageBitmap(zoomBitmap(bitmap)); } }); img = (ImageButton)findViewById(R.id.img); img.setBackgroundColor(Color.RED); } /** * 实现图缩放 * @param target * @return */ public Bitmap zoomBitmap(Bitmap target){ // 得到图片的高宽 int width = target.getWidth(); int height = target.getHeight(); Matrix matrix = new Matrix(); // 算出图片的高宽缩放比例 float scaleWidth = ((float)300)/ width; float scaleHeight = ((float)300)/ height; matrix.postScale(scaleWidth, scaleHeight); // Bitmap result = Bitmap.createBitmap(target,0,0,width,height, matrix,true); return Bitmap.createBitmap(target,0,0,width,height, matrix,true); } }