public static Bitmap captureBackgroundAndPicture(RelativeLayout relativeLayout, ImageView imageView) {
// 创建一个Bitmap对象并将Canvas绘制到上面
Bitmap bitmap = Bitmap.createBitmap(relativeLayout.getWidth(), relativeLayout.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
// 在Canvas上绘制背景视图
relativeLayout.draw(canvas);
// 计算图片视图在Canvas中的位置
int[] location = new int[2];
imageView.getLocationOnScreen(location);
// 在Canvas上绘制图片视图
canvas.save();
canvas.translate(0, relativeLayout.getHeight());
canvas.clipRect(location[0], location[1] - relativeLayout.getHeight(), location[0] + imageView.getWidth(), location[1]);
imageView.draw(canvas);
canvas.restore();
return Bitmap.createScaledBitmap(bitmap, relativeLayout.getWidth(), relativeLayout.getHeight(), true);
}
use canvas to create bitmap in layout
最新推荐文章于 2024-07-16 20:48:57 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)