安卓创建View截图
转自:http://mthli.github.io/post/%E5%AE%89%E5%8D%93%E5%88%9B%E5%BB%BAView%E6%88%AA%E5%9B%BE.html
/**
* @param view,当前想要创建截图的View
* @param width,设置截图的宽度
* @param height,设置截图的高度
* @param scroll,如果为真则从View的当前滚动位置开始绘制截图
* @param config,Bitmap的质量,比如ARGB_8888等
*
* @return 截图的Bitmap
*/
public static Bitmap capture(View view, float width, float height, boolean scroll, Bitmap.Config config) {
if (!view.isDrawingCacheEnabled()) {
view.setDrawingCacheEnabled(true);
}
Bitmap bitmap = Bitmap.createBitmap((int) width, (int) height, config);
bitmap.eraseColor(Color.WHITE);
Canvas canvas = new Canvas(bitmap);
int left = view.getLeft();
int top = view.getTop();
if (scroll) {
left = view.getScrollX();
top = view.getScrollY();
}
int status = canvas.save();
canvas.translate(-left, -top);
float scale = width / view.getWidth();
canvas.scale(scale, scale, left, top);
view.draw(canvas);
canvas.restoreToCount(status);
Paint alphaPaint = new Paint();
alphaPaint.setColor(Color.TRANSPARENT);
canvas.drawRect(0f, 0f, 1f, height, alphaPaint);
canvas.drawRect(width - 1f, 0f, width, height, alphaPaint);
canvas.drawRect(0f, 0f, width, 1f, alphaPaint);
canvas.drawRect(0f, height - 1f, width, height, alphaPaint);
canvas.setBitmap(null);
return bitmap;
}
发表评论