解决ScrollView长截图的几个小问题

先说一下ScrollView截图的截图方式:

1、获取Child的高度

2、根据高度宽度创建对应的Bitmap

3、ScrollView.draw(Canvas)绘制,得到所需长截图

其中可能出现的问题是,长截图的长宽都是对的,但所得截图只有屏幕显示的那一部分。

这个时候就要注意Padding、Padding、Padding了。

把ScrollView child中的padding都去掉,就能得到完整截图了。

但所得的截图可能没有背景,这是因为ScrollView 本身就没背景。

解决方法也很简单,传入背景图(一般会用.9.png),先在Canvas中绘制背景,再绘制ScrollView就可以了。

public static Bitmap getLongScreenBitmap(ScrollView scrollView,Drawable drawable){
    Bitmap bitmap;
    int h = 0;
    int childCount = scrollView.getChildCount();
    for(int i = 0 ; i < childCount;i++){
        h += scrollView.getChildAt(i).getMeasuredHeight();
    }
    bitmap = Bitmap.createBitmap(scrollView.getWidth(),h, Bitmap.Config.ARGB_8888);
    drawable.setBounds(0,0,bitmap.getWidth(),bitmap.getHeight());
    Canvas canvas = new Canvas(bitmap);
    drawable.draw(canvas);               //绘制背景
    scrollView.draw(canvas);
    return bitmap;
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值