Android View截屏长图拼接(RecyclerView)
Android View截屏长图拼接(NestedScrollView)
话不多说直接上代码
/**
* 高清截屏拼接图片(上下拼接)
*
* @param topView
* @param bottomView
* @return
*/
public static Bitmap mergeBitmapTopBottom(View topView, View bottomView) {
int topW = topView.getWidth();
int topH = topView.getHeight();
int bottomW = bottomView.getWidth();
int bottomH = bottomView.getHeight();
Bitmap topBmp = Bitmap.createBitmap(topW, topH, Bitmap.Config.RGB_565);
Canvas topCanvas = new Canvas(topBmp);
/** 如果不设置canvas画布为白色,则生成透明 */
topCanvas.drawColor(Color.WHITE);
topView.layout(0, 0, topW, topH);
topView.draw(topCanvas);
Bitmap bottomBmp = Bitmap.createBitmap(bottomW, bottomH, Bitmap.Config.RGB_565);
Canvas bottomCanvas = new Canvas(bottomBmp);
/** 如果不设置canvas画布为白色,则生成透明 */
bottomCanvas.drawColor(Color.WHITE);
bottomView.layout(0, 0, bottomW, bottomH);
bottomView.draw(bottomCanvas);
Bitmap bitmap = Bitmap.createBitmap(topW, topH + bottomH, Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
Rect topRect = new Rect(0, 0, topW, topH);
Rect bottomRect = new Rect(0, 0, bottomW, bottomH);
Rect bottomDst = new Rect(0, topH, bottomW, topH + bottomH);
canvas.drawBitmap(topBmp, topRect, topRect, null);
canvas.drawBitmap(bottomBmp, bottomRect, bottomDst, null);
topBmp.recycle();
bottomBmp.recycle();
topBmp = null;
bottomBmp = null;
return bitmap;
}