需求:
比如說我要截取一個dialog的view,但是dialog可能內部有一個scrollview,有一部分內容顯示不出來误算,但是要求截屏的時候可以截全。具體代碼實現如下
/**
* 獲取成績單圖片
**/
private Bitmap getBitmap() {
int offset = DYDensityUtils.dip2px(10);
ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) mScrollView.getLayoutParams();
int width = mClContent.getWidth() + layoutParams.leftMargin + layoutParams.rightMargin;
int height = mClContent.getHeight() + layoutParams.topMargin + layoutParams.bottomMargin + offset;
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Drawable headBg = getResources().getDrawable(R.drawable.vs_room_data_broadcast_head_bg);
int headHeight = (int) (width / 2.8f);
headBg.setBounds(0, 0, width, headHeight);
headBg.draw(canvas);
Drawable title = getResources().getDrawable(R.drawable.vs_room_data_broadcast_title_room);
title.setBounds((width - title.getIntrinsicWidth()) / 2, offset, (width + title.getIntrinsicWidth()) / 2, title.getIntrinsicHeight() + offset);
title.draw(canvas);
Drawable bodyBg = getResources().getDrawable(R.drawable.vs_shape_white_bottom_corner_16);
bodyBg.setBounds(0, headHeight - offset, width, height);
bodyBg.draw(canvas);
canvas.translate(layoutParams.leftMargin, layoutParams.topMargin);
mScrollView.draw(canvas);
return bitmap;
}
錯誤不足之處或相關建議歡迎大家評論指出咖杂,謝謝蚊夫!如果覺得內容可以的話麻煩喜歡(?)一下