截长图

/**
* 截取scrollview的屏幕
*
* @param scrollView
* @return
*/
public static Bitmap getBitmapByViewS(ScrollView scrollView) {
int h = 0;
Bitmap bitmap = null;
// 获取scrollview实际高度
for (int i = 0; i < scrollView.getChildCount(); i++) {
h += scrollView.getChildAt(i).getHeight();
scrollView.getChildAt(i).setBackgroundColor(
Color.parseColor(“#ffc8c8”));
}
// 创建对应大小的bitmap
bitmap = Bitmap.createBitmap(scrollView.getWidth(), h,
Bitmap.Config.RGB_565);
final Canvas canvas = new Canvas(bitmap);
scrollView.draw(canvas);
return bitmap;
}

/***
 * 去黑影
 * @param secondBitmap
 * @param id
 * @return
 */
//首先传入两张图片
public static Bitmap mergeThumbnailBitmap(Bitmap secondBitmap,int id) {
    //以其中一张图片的大小作为画布的大小,或者也可以自己自定义
    Bitmap bitmap = Bitmap.createBitmap(secondBitmap.getWidth(), id, secondBitmap.getConfig());
    //生成画布
    Canvas canvas = new Canvas(bitmap);
    //因为我传入的secondBitmap的大小是不固定的,所以我要将传来的secondBitmap调整到和画布一样的大小
    float w = secondBitmap.getWidth();
    float h = id;
    Matrix m = new Matrix();
    //确定secondBitmap大小比例
    m.setScale(w / secondBitmap.getWidth(), h / secondBitmap.getHeight());
    Paint paint = new Paint();
    //给画笔设定透明值,想将哪个图片进行透明化,就将画笔用到那张图片上
    canvas.drawBitmap(secondBitmap, m, paint);

    return bitmap;
}



/**
 * 压缩图片
 *
 * @param image
 * @return
 */
public static Bitmap compressImage(Bitmap image) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    // 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
    image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    int options = 100;
    // 循环判断如果压缩后图片是否大于100kb,大于继续压缩
    while (baos.toByteArray().length / 1024 > 100) {
        // 重置baos
        baos.reset();
        // 这里压缩options%,把压缩后的数据存放到baos中
        image.compress(Bitmap.CompressFormat.JPEG, options, baos);
        // 每次都减少10
        options -= 10;
    }
    // 把压缩后的数据baos存放到ByteArrayInputStream中
    ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
    // 把ByteArrayInputStream数据生成图片
    Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);
    return bitmap;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值