// 获取bitmap对象
private void prepareShareData() {
mTopView.setDrawingCacheEnabled(true);
if (mTopBitmap == null) {
// 获取topview的bitmap
mTopBitmap = Bitmap.createBitmap(mTopView.getDrawingCache(true));
}
mBottomView.setDrawingCacheEnabled(true);
if (mBottomBitmap == null) {
// 获取bottom view的bitmap
mBottomBitmap = Bitmap.createBitmap(mBottomView.getDrawingCache(true));
}
}
接下来组合两个bitmap的方法
private Bitmap combineBitmap(Bitmap p1, Bitmap p2) {
int width1 = p1.getWidth();
int height1 = p1.getHeight();
int width2 = p1.getWidth();
int height2 = p1.getHeight();
Bitmap bitmap = Bitmap.createBitmap(Math.max(width1, width2), height1 + height2, Bitmap.Config.ARGB_8888);
Paint paint = new Paint();
Canvas canvas = new Canvas(bitmap);
// 将bitmap分别画在bitmpat的画布上
canvas.drawBitmap(p1, new Rect(0, 0, width1, height1), new Rect(0, 0, width1, height1), paint);
canvas.drawBitmap(p2, new Rect(0, 0, width2, height2), new Rect(0, height1, width2, height1 + height2), paint);
return bitmap;
}
// 最后保存bitmap图片
private void saveBitmapToFile(String path, Bitmap bitmap) throws FileNotFoundException {
File file = new File(path);
FileOutputStream fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
}