修复剪裁窗口画布控件缩放和布局问题

- 修复ZoomContainerView缩放按钮无效问题,添加onLayout正确缩放子视图
- 修复CropCanvasView裁剪框显示不一致问题
- 更新libappbase版本到15.15.20
- 优化剪裁窗口布局
This commit is contained in:
2026-04-28 09:15:43 +08:00
parent 31ea5c8fbb
commit 1cca476acd
5 changed files with 75 additions and 27 deletions

View File

@@ -221,8 +221,30 @@ public class CropCanvasView extends View {
}
public void scaleToView(int viewWidth, int viewHeight) {
requestLayout();
invalidate();
if (viewWidth > 0 && viewHeight > 0 && canvasWidth > 0 && canvasHeight > 0) {
float scale = Math.max((float) viewWidth / canvasWidth, (float) viewHeight / canvasHeight);
int oldCanvasW = canvasWidth;
int oldCanvasH = canvasHeight;
canvasWidth = (int) (canvasWidth * scale);
canvasHeight = (int) (canvasHeight * scale);
float left = (canvasWidth - imageWidth) / 2f;
float top = (canvasHeight - imageHeight) / 2f;
imageBounds.set(left, top, left + imageWidth, top + imageHeight);
canvasBounds.set(0, 0, canvasWidth, canvasHeight);
if (cropRect != null) {
float scaleX = (float) canvasWidth / oldCanvasW;
float scaleY = (float) canvasHeight / oldCanvasH;
cropRect.left *= scaleX;
cropRect.top *= scaleY;
cropRect.right *= scaleX;
cropRect.bottom *= scaleY;
}
requestLayout();
invalidate();
}
}
public RectF getCropRect() {
@@ -295,15 +317,10 @@ public class CropCanvasView extends View {
canvas.restore();
canvas.drawRect(cropRect, borderPaint);
float scX = imageToScreenX(cropRect.left);
float scY = imageToScreenY(cropRect.top);
float scR = imageToScreenX(cropRect.right);
float scB = imageToScreenY(cropRect.bottom);
canvas.drawCircle(scX, scY, 12, cornerPaint);
canvas.drawCircle(scR, scY, 12, cornerPaint);
canvas.drawCircle(scX, scB, 12, cornerPaint);
canvas.drawCircle(scR, scB, 12, cornerPaint);
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);
} else {
canvas.drawRect(cropRect, borderPaint);

View File

@@ -72,16 +72,27 @@ public class ZoomContainerView extends FrameLayout {
public void zoomIn() {
scaleFactor = Math.min(maxScale, scaleFactor + ZOOM_STEP);
invalidate();
requestLayout();
requestMeasure();
}
public void zoomOut() {
scaleFactor = Math.max(minScale, scaleFactor - ZOOM_STEP);
invalidate();
requestLayout();
requestMeasure();
}
private void requestMeasure() {
removeCallbacks(measureRunable);
post(measureRunable);
}
private Runnable measureRunable = new Runnable() {
@Override
public void run() {
requestLayout();
invalidate();
}
};
public float getScaleFactor() {
return scaleFactor;
}
@@ -94,17 +105,37 @@ public class ZoomContainerView extends FrameLayout {
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int childCount = getChildCount();
if (childCount > 0) {
View child = getChildAt(0);
measureChild(child, widthMeasureSpec, heightMeasureSpec);
int childW = (int) (child.getMeasuredWidth() * scaleFactor);
int childH = (int) (child.getMeasuredHeight() * scaleFactor);
setMeasuredDimension(
MeasureSpec.getSize(widthMeasureSpec),
MeasureSpec.getSize(heightMeasureSpec)
);
child.measure(widthMeasureSpec, heightMeasureSpec);
int childW = child.getMeasuredWidth();
int childH = child.getMeasuredHeight();
int scaledW = (int) (childW * scaleFactor);
int scaledH = (int) (childH * scaleFactor);
int widthSpec = MeasureSpec.makeMeasureSpec(scaledW, MeasureSpec.EXACTLY);
int heightSpec = MeasureSpec.makeMeasureSpec(scaledH, MeasureSpec.EXACTLY);
child.measure(widthSpec, heightSpec);
setMeasuredDimension(scaledW, scaledH);
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
int childCount = getChildCount();
if (childCount > 0) {
View child = getChildAt(0);
int childW = child.getMeasuredWidth();
int childH = child.getMeasuredHeight();
int scaledW = (int) (childW * scaleFactor);
int scaledH = (int) (childH * scaleFactor);
int parentW = right - left;
int parentH = bottom - top;
int childLeft = (parentW - scaledW) / 2;
int childTop = (parentH - scaledH) / 2;
child.layout(childLeft, childTop, childLeft + scaledW, childTop + scaledH);
}
}