截屏的各种实现(全屏or长截屏)

全屏截图:

/**
* 传入的activity是要截屏的activity
*/
public static Bitmap getViewBitmap(Activity activity) {
        // View是你需要截图的View
        View view = activity.getWindow().getDecorView();
        //这两句必须写,否则getDrawingCache报空指针
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap b1 = view.getDrawingCache();

        // 获取状态栏高度
        Rect frame = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        int statusBarHeight = frame.top;

        // 获取屏幕长和高
        int width = activity.getResources().getDisplayMetrics().widthPixels;
        int height = activity.getResources().getDisplayMetrics().heightPixels;
        // 去掉标题栏
        Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height - statusBarHeight);
        view.destroyDrawingCache();
        return b;
    }

ScrollView或者ListView或者LinearLayout等ViewGroup的长截图:

public static Bitmap getViewGroupBitmap(ViewGroup viewGroup) {
        //viewGroup的总高度
        int h = 0;
        Bitmap bitmap; 

        // 适用于ListView或RecyclerView等求高度
        for (int i = 0; i < viewGroup.getChildCount(); i++) {
            h += viewGroup.getChildAt(i).getHeight();
        }

        // 若viewGroup是ScrollView,那么他的直接子元素有id的话,如下所示:
        // h = mLinearLayout.getHeight();

        }
        // 创建对应大小的bitmap(重点)
        bitmap = Bitmap.createBitmap(scrollView.getWidth(), h, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        scrollView.draw(canvas);
        return bitmap;
    }

总结:
1. 布局为ScrollView,ListView,RecyclerView等能滑动的,用for循环遍历子元素求实际高度。
ps:ScrollView由于只能有一个直接子元素,那么我们可以直接用他的子元素来求高度。
2. 布局为LinearLayout等ViewGroup,直接.getHeight()获取

注意:
1. getHeight(),getWidth()不能直接在avtivity生命周期中调用,因为activity尚未生成完毕之前,控件的长宽尚未测量,故所得皆为0
2. 用该方式实现长截屏需要注意背景色的问题,如果你的截图背景色出了问题,仔细检查XML文件,看看该背景色是否设置在你截屏的控件中

补充:
对于混合布局比如说:根RelativeLayout布局中有ViewGroup+RelativeLayout等子布局,可以分别测量他们的高度并生成bitmap对象,然后拼接在一起即可。

/**
*  上下拼接两个Bitmap,
*  drawBitmap的参数:1.需要画的bitmap
*                   2.裁剪矩形,bitmap会被该矩形裁剪
*                   3.放置在canvas的位置矩形,bitmap会被放置在该矩形的位置上
*                   4.画笔
*/
public static Bitmap mergeBitmap_TB_My(Bitmap topBitmap, Bitmap bottomBitmap) {
        int width = topBitmap.getWidth();
        int height = topBitmap.getHeight() + bottomBitmap.getHeight();
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        Rect rectTop = new Rect(0, 0, width, topBitmap.getHeight());
        Rect rectBottomRes = new Rect(0, 0, width, bottomBitmap.getHeight());
        RectF rectBottomDes = new RectF(0, topBitmap.getHeight(), width, height);
        canvas.drawBitmap(topBitmap, rectTop, rectTop, null);
        canvas.drawBitmap(bottomBitmap, rectBottomRes, rectBottomDes, null);
        return bitmap;
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一杯刘

我 风暴降生 打钱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值