裁剪框跟随画布控件同步缩放
- 添加containerScale属性跟踪缩放比例 - setContainerScale()同步缩放比例到裁剪框 - onDraw()中使用canvas.scale()缩放裁剪框绘制 - 角标半径按比例调整
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:59:23 CST 2026
|
#Tue Apr 28 10:15:27 CST 2026
|
||||||
stageCount=7
|
stageCount=7
|
||||||
libraryProject=
|
libraryProject=
|
||||||
baseVersion=15.0
|
baseVersion=15.0
|
||||||
publishVersion=15.0.6
|
publishVersion=15.0.6
|
||||||
buildCount=33
|
buildCount=36
|
||||||
baseBetaVersion=15.0.7
|
baseBetaVersion=15.0.7
|
||||||
|
|||||||
@@ -50,6 +50,7 @@ public class CropCanvasView extends View {
|
|||||||
private float displayOffsetX = 0f;
|
private float displayOffsetX = 0f;
|
||||||
private float displayOffsetY = 0f;
|
private float displayOffsetY = 0f;
|
||||||
private static final int MAX_DISPLAY_SIZE = 2048;
|
private static final int MAX_DISPLAY_SIZE = 2048;
|
||||||
|
private float containerScale = 1.0f;
|
||||||
|
|
||||||
public CropCanvasView(Context context) {
|
public CropCanvasView(Context context) {
|
||||||
super(context);
|
super(context);
|
||||||
@@ -120,6 +121,22 @@ public class CropCanvasView extends View {
|
|||||||
return displayBitmapScale;
|
return displayBitmapScale;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void onContainerScaled() {
|
||||||
|
containerScale = 1.0f;
|
||||||
|
invalidate();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setContainerScale(float scale) {
|
||||||
|
if (scale > 0 && scale != containerScale) {
|
||||||
|
containerScale = scale;
|
||||||
|
invalidate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getContainerScale() {
|
||||||
|
return containerScale;
|
||||||
|
}
|
||||||
|
|
||||||
public Bitmap getCanvasBitmap() {
|
public Bitmap getCanvasBitmap() {
|
||||||
if (canvasWidth <= 0 || canvasHeight <= 0) {
|
if (canvasWidth <= 0 || canvasHeight <= 0) {
|
||||||
return null;
|
return null;
|
||||||
@@ -338,19 +355,42 @@ public class CropCanvasView extends View {
|
|||||||
|
|
||||||
canvas.restore();
|
canvas.restore();
|
||||||
|
|
||||||
|
if (containerScale != 1.0f) {
|
||||||
|
canvas.save();
|
||||||
|
canvas.scale(containerScale, containerScale);
|
||||||
canvas.drawRect(cropRect, borderPaint);
|
canvas.drawRect(cropRect, borderPaint);
|
||||||
canvas.drawCircle(cropRect.left, cropRect.top, 12, cornerPaint);
|
float cornerRadius = 12f / containerScale;
|
||||||
canvas.drawCircle(cropRect.right, cropRect.top, 12, cornerPaint);
|
canvas.drawCircle(cropRect.left, cropRect.top, cornerRadius, cornerPaint);
|
||||||
canvas.drawCircle(cropRect.left, cropRect.bottom, 12, cornerPaint);
|
canvas.drawCircle(cropRect.right, cropRect.top, cornerRadius, cornerPaint);
|
||||||
canvas.drawCircle(cropRect.right, cropRect.bottom, 12, cornerPaint);
|
canvas.drawCircle(cropRect.left, cropRect.bottom, cornerRadius, cornerPaint);
|
||||||
|
canvas.drawCircle(cropRect.right, cropRect.bottom, cornerRadius, cornerPaint);
|
||||||
|
canvas.restore();
|
||||||
} else {
|
} else {
|
||||||
canvas.drawRect(cropRect, borderPaint);
|
canvas.drawRect(cropRect, borderPaint);
|
||||||
|
|
||||||
canvas.drawCircle(cropRect.left, cropRect.top, 12, cornerPaint);
|
canvas.drawCircle(cropRect.left, cropRect.top, 12, cornerPaint);
|
||||||
canvas.drawCircle(cropRect.right, cropRect.top, 12, cornerPaint);
|
canvas.drawCircle(cropRect.right, cropRect.top, 12, cornerPaint);
|
||||||
canvas.drawCircle(cropRect.left, cropRect.bottom, 12, cornerPaint);
|
canvas.drawCircle(cropRect.left, cropRect.bottom, 12, cornerPaint);
|
||||||
canvas.drawCircle(cropRect.right, cropRect.bottom, 12, cornerPaint);
|
canvas.drawCircle(cropRect.right, cropRect.bottom, 12, cornerPaint);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
if (containerScale != 1.0f) {
|
||||||
|
canvas.save();
|
||||||
|
canvas.scale(containerScale, containerScale);
|
||||||
|
canvas.drawRect(cropRect, borderPaint);
|
||||||
|
float cornerRadius = 12f / containerScale;
|
||||||
|
canvas.drawCircle(cropRect.left, cropRect.top, cornerRadius, cornerPaint);
|
||||||
|
canvas.drawCircle(cropRect.right, cropRect.top, cornerRadius, cornerPaint);
|
||||||
|
canvas.drawCircle(cropRect.left, cropRect.bottom, cornerRadius, cornerPaint);
|
||||||
|
canvas.drawCircle(cropRect.right, cropRect.bottom, cornerRadius, cornerPaint);
|
||||||
|
canvas.restore();
|
||||||
|
} else {
|
||||||
|
canvas.drawRect(cropRect, borderPaint);
|
||||||
|
canvas.drawCircle(cropRect.left, cropRect.top, 12, cornerPaint);
|
||||||
|
canvas.drawCircle(cropRect.right, cropRect.top, 12, cornerPaint);
|
||||||
|
canvas.drawCircle(cropRect.left, cropRect.bottom, 12, cornerPaint);
|
||||||
|
canvas.drawCircle(cropRect.right, cropRect.bottom, 12, cornerPaint);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -100,6 +100,13 @@ public class ZoomContainerView extends FrameLayout {
|
|||||||
|
|
||||||
public void setScaleFactor(float scale) {
|
public void setScaleFactor(float scale) {
|
||||||
scaleFactor = Math.max(minScale, Math.min(scale, maxScale));
|
scaleFactor = Math.max(minScale, Math.min(scale, maxScale));
|
||||||
|
int childCount = getChildCount();
|
||||||
|
if (childCount > 0) {
|
||||||
|
View child = getChildAt(0);
|
||||||
|
if (child instanceof CropCanvasView) {
|
||||||
|
((CropCanvasView) child).setContainerScale(scaleFactor);
|
||||||
|
}
|
||||||
|
}
|
||||||
requestLayout();
|
requestLayout();
|
||||||
invalidate();
|
invalidate();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user