/**
* 截取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;
}