修复裁剪窗口画布控件裁剪源问题

- 添加getCanvasBitmap()方法创建包含背景的画布位图
- 裁剪源使用画布位图,与预览显示一致
This commit is contained in:
2026-04-28 09:51:49 +08:00
parent 8d62e7df21
commit e07931fd3b
3 changed files with 44 additions and 19 deletions

View File

@@ -1,8 +1,8 @@
#Created by .winboll/winboll_app_build.gradle
#Tue Apr 28 09:22:58 CST 2026
#Tue Apr 28 09:48:58 CST 2026
stageCount=7
libraryProject=
baseVersion=15.0
publishVersion=15.0.6
buildCount=23
buildCount=30
baseBetaVersion=15.0.7

View File

@@ -147,28 +147,31 @@ public class CropActivity extends AppCompatActivity {
}
try {
Bitmap canvasBitmap = cropCanvasView.getCanvasBitmap();
if (canvasBitmap == null || canvasBitmap.isRecycled()) {
Toast.makeText(this, "Failed to get canvas bitmap", Toast.LENGTH_SHORT).show();
return;
}
RectF cropRect = cropCanvasView.getCropRect();
RectF imageBounds = cropCanvasView.getImageBounds();
float cropX = cropRect.left - imageBounds.left;
float cropY = cropRect.top - imageBounds.top;
float cropW = cropRect.width();
float cropH = cropRect.height();
int bmpX = (int) cropRect.left;
int bmpY = (int) cropRect.top;
int bmpW = (int) cropRect.width();
int bmpH = (int) cropRect.height();
int bmpX = (int) cropX;
int bmpY = (int) cropY;
int bmpW = (int) cropW;
int bmpH = (int) cropH;
bmpX = Math.max(0, Math.min(bmpX, originalBitmap.getWidth() - 1));
bmpY = Math.max(0, Math.min(bmpY, originalBitmap.getHeight() - 1));
bmpW = Math.max(1, Math.min(bmpW, originalBitmap.getWidth() - bmpX));
bmpH = Math.max(1, Math.min(bmpH, originalBitmap.getHeight() - bmpY));
bmpX = Math.max(0, Math.min(bmpX, canvasBitmap.getWidth() - 1));
bmpY = Math.max(0, Math.min(bmpY, canvasBitmap.getHeight() - 1));
bmpW = Math.max(1, Math.min(bmpW, canvasBitmap.getWidth() - bmpX));
bmpH = Math.max(1, Math.min(bmpH, canvasBitmap.getHeight() - bmpY));
LogUtils.d(TAG, "saveCroppedCover: cropRect=" + cropRect);
LogUtils.d(TAG, "saveCroppedCover: crop=(" + bmpX + "," + bmpY + "," + bmpW + "," + bmpH + ")");
LogUtils.d(TAG, "saveCroppedCover: original size=" + originalBitmap.getWidth() + "x" + originalBitmap.getHeight());
LogUtils.d(TAG, "saveCroppedCover: canvas size=" + canvasBitmap.getWidth() + "x" + canvasBitmap.getHeight());
Bitmap cropped = Bitmap.createBitmap(canvasBitmap, bmpX, bmpY, bmpW, bmpH);
canvasBitmap.recycle();
Bitmap cropped = Bitmap.createBitmap(originalBitmap, bmpX, bmpY, bmpW, bmpH);
LogUtils.d(TAG, "saveCroppedCover: cropped size=" + cropped.getWidth() + "x" + cropped.getHeight());
File coverDir = new File(getFilesDir(), "covers");

View File

@@ -120,6 +120,28 @@ public class CropCanvasView extends View {
return displayBitmapScale;
}
public Bitmap getCanvasBitmap() {
if (canvasWidth <= 0 || canvasHeight <= 0) {
return null;
}
Bitmap canvasBmp = Bitmap.createBitmap(canvasWidth, canvasHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(canvasBmp);
canvas.drawColor(backgroundColor);
if (displayBitmap != null && !displayBitmap.isRecycled()) {
if (displayBitmap == originalBitmap) {
canvas.drawBitmap(displayBitmap, imageBounds.left, imageBounds.top, imagePaint);
} else {
float invScale = 1f / displayBitmapScale;
canvas.save();
canvas.scale(invScale, invScale);
canvas.drawBitmap(displayBitmap, imageBounds.left * displayBitmapScale,
imageBounds.top * displayBitmapScale, imagePaint);
canvas.restore();
}
}
return canvasBmp;
}
public void initCanvas(int imgWidth, int imgHeight, float ratio) {
this.imageWidth = imgWidth;
this.imageHeight = imgHeight;
@@ -143,7 +165,7 @@ public class CropCanvasView extends View {
imageBounds.set(left, top, left + imageWidth, top + imageHeight);
canvasBounds.set(0, 0, canvasWidth, canvasHeight);
cropRect = new RectF(0, 0, canvasWidth, canvasHeight);
cropRect = new RectF(canvasBounds);
requestLayout();
invalidate();