【Android】android获取长图截屏(显示之外的也要)

需求:
比如说我要截取一个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;
    }

错误不足之处或相关建议欢迎大家评论指出,谢谢!如果觉得内容可以的话麻烦喜欢(♥)一下

你可能感兴趣的:(【Android】android获取长图截屏(显示之外的也要))