都不喜欢废话,网上方法也一堆,这里我就记录一下,emmmmm
/**
*此方法直接截取屏幕指定view区域的内容
* @param view 需要截取屏幕的图片view
* @return Bitmap
*/
private Bitmap getScreenshot2Bitmap(View view) throws Exception {
View screenView = getWindow().getDecorView();
screenView.setDrawingCacheEnabled(false);
screenView.buildDrawingCache();
//获取屏幕整张图片
Bitmap screenBitmap = screenView.getDrawingCache();
if (screenBitmap != null) {
//需要截取的长和宽
int outWidth = view.getWidth();
int outHeight = view.getHeight();
//获取需要截图部分的在屏幕上的坐标(view的左上角坐标)
int[] viewLocationArray = new int[2];
view.getLocationOnScreen(viewLocationArray);
//从屏幕截图中截取指定区域
screenBitmap = Bitmap.createBitmap(bitmap, viewLocationArray[0], viewLocationArray[1], outWidth, outHeight);
Toast.makeText(context, "截图成功", Toast.LENGTH_SHORT).show();
view.setDrawingCacheEnabled(false);
}
//记得加上,不然重复生成时 返回的还是第一次生成的bitmap截图
screenView.destroyDrawingCache();
return screenBitmap ;
}
/**
*此方法只能截取指定view区域上的内容,如果view上还有其他view,则无法截取
* 先测量和布局,再生成Bitmap
*/
public static Bitmap getBitmap(View view) {
// 测量
int widthSpec = View.MeasureSpec.makeMeasureSpec(screenWidth, View.MeasureSpec.AT_MOST);
int heightSpec = View.MeasureSpec.makeMeasureSpec(screenHeight, View.MeasureSpec.AT_MOST);
view.measure(widthSpec, heightSpec);
// 布局
int measuredWidth = view.getMeasuredWidth();
int measuredHeight = view.getMeasuredHeight();
view.layout(0, 0, measuredWidth, measuredHeight);
// 绘制
int width = view.getWidth();
int height = view.getHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
下一步操作就是保存图片到本地或操作图片.....
//todo 有空可以看下这几个方法 控制生成图片的
setDrawingCacheEnabled(boolean enabled)
buildDrawingCache()
getDrawingCache()
destroyDrawingCache()
WebView 截取长图:
直接上代码了 稳稳的 屏幕外也可以实现截取
public static void getViewBitmap(View view) {
WebView webview = (WebView) view;
//测量webview 实际的大小 包括屏幕外的
webview.measure(View.MeasureSpec.makeMeasureSpec(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
webview.layout(0, 0, webview.getMeasuredWidth(), webview.getMeasuredHeight());
//启用View的DrawingCache功能
webview.buildDrawingCache(true);
webview.setDrawingCacheEnabled(true);
webview.setVerticalScrollBarEnabled(false);
webview.buildDrawingCache();
// 延时保存图片 必须延时不然会出现截取部分白屏
new Handler().postDelayed(() -> {
Bitmap bitmap =convertViewToBitmap(webview, webview.getMeasuredWidth(), webview.getMeasuredHeight());
Canvas canvas = new Canvas(bitmap);
// 画布的宽高和 WebView 的网页保持一致
Paint paint = new Paint();
canvas.drawBitmap(bitmap, 0, webview.getMeasuredHeight(), paint);
webview.draw(canvas);
try {
//保存bitmap 为本地图片或做其他处理
saveFile(webview, bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}, 500);
}
/**
* 绘制view的bitmap
* @param view 要绘制的view
* @param bitmapWidth 绘制宽 传入实际宽高
* @param bitmapHeight 绘制高
* @return
*/
private static Bitmap convertViewToBitmap(View view, int bitmapWidth, int bitmapHeight) {
Bitmap bitmap = null;
try {
bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_4444);
view.draw(new Canvas(bitmap));
} catch (OutOfMemoryError ooe) {
System.gc();
return bitmap;
}
return bitmap;
}
注意事项 :
1.webview 在截取完成后 会失去焦点 不能在滚动了 (处理方法:reload 或手动滚动webview)
2.webview 会截取不到部分三方框架绘制的图 (可转换成image 后在进行截取)
3. 获取图片后 切记 执行webview.buildDrawingCache(false);
注:此方向法系转载
网友评论