修复裁剪窗口画布控件裁剪源问题
- 添加getCanvasBitmap()方法创建包含背景的画布位图 - 裁剪源使用画布位图,与预览显示一致
This commit is contained in:
@@ -1,8 +1,8 @@
|
|||||||
#Created by .winboll/winboll_app_build.gradle
|
#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
|
stageCount=7
|
||||||
libraryProject=
|
libraryProject=
|
||||||
baseVersion=15.0
|
baseVersion=15.0
|
||||||
publishVersion=15.0.6
|
publishVersion=15.0.6
|
||||||
buildCount=23
|
buildCount=30
|
||||||
baseBetaVersion=15.0.7
|
baseBetaVersion=15.0.7
|
||||||
|
|||||||
@@ -147,28 +147,31 @@ public class CropActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
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 cropRect = cropCanvasView.getCropRect();
|
||||||
RectF imageBounds = cropCanvasView.getImageBounds();
|
|
||||||
|
|
||||||
float cropX = cropRect.left - imageBounds.left;
|
int bmpX = (int) cropRect.left;
|
||||||
float cropY = cropRect.top - imageBounds.top;
|
int bmpY = (int) cropRect.top;
|
||||||
float cropW = cropRect.width();
|
int bmpW = (int) cropRect.width();
|
||||||
float cropH = cropRect.height();
|
int bmpH = (int) cropRect.height();
|
||||||
|
|
||||||
int bmpX = (int) cropX;
|
bmpX = Math.max(0, Math.min(bmpX, canvasBitmap.getWidth() - 1));
|
||||||
int bmpY = (int) cropY;
|
bmpY = Math.max(0, Math.min(bmpY, canvasBitmap.getHeight() - 1));
|
||||||
int bmpW = (int) cropW;
|
bmpW = Math.max(1, Math.min(bmpW, canvasBitmap.getWidth() - bmpX));
|
||||||
int bmpH = (int) cropH;
|
bmpH = Math.max(1, Math.min(bmpH, canvasBitmap.getHeight() - bmpY));
|
||||||
|
|
||||||
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));
|
|
||||||
|
|
||||||
|
LogUtils.d(TAG, "saveCroppedCover: cropRect=" + cropRect);
|
||||||
LogUtils.d(TAG, "saveCroppedCover: crop=(" + bmpX + "," + bmpY + "," + bmpW + "," + bmpH + ")");
|
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());
|
LogUtils.d(TAG, "saveCroppedCover: cropped size=" + cropped.getWidth() + "x" + cropped.getHeight());
|
||||||
|
|
||||||
File coverDir = new File(getFilesDir(), "covers");
|
File coverDir = new File(getFilesDir(), "covers");
|
||||||
|
|||||||
@@ -120,6 +120,28 @@ public class CropCanvasView extends View {
|
|||||||
return displayBitmapScale;
|
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) {
|
public void initCanvas(int imgWidth, int imgHeight, float ratio) {
|
||||||
this.imageWidth = imgWidth;
|
this.imageWidth = imgWidth;
|
||||||
this.imageHeight = imgHeight;
|
this.imageHeight = imgHeight;
|
||||||
@@ -143,7 +165,7 @@ public class CropCanvasView extends View {
|
|||||||
imageBounds.set(left, top, left + imageWidth, top + imageHeight);
|
imageBounds.set(left, top, left + imageWidth, top + imageHeight);
|
||||||
canvasBounds.set(0, 0, canvasWidth, canvasHeight);
|
canvasBounds.set(0, 0, canvasWidth, canvasHeight);
|
||||||
|
|
||||||
cropRect = new RectF(0, 0, canvasWidth, canvasHeight);
|
cropRect = new RectF(canvasBounds);
|
||||||
|
|
||||||
requestLayout();
|
requestLayout();
|
||||||
invalidate();
|
invalidate();
|
||||||
|
|||||||
Reference in New Issue
Block a user