优化剪裁窗口颜色拾取功能

- 按下手指时预览颜色,放开时确认拾取
- 拖动时实时跟随手指位置更新颜色
- 分离颜色预览和确认拾取的回调接口
This commit is contained in:
2026-04-28 13:06:45 +08:00
parent 474ddcbb3b
commit b035d461aa
2 changed files with 48 additions and 4 deletions

View File

@@ -104,6 +104,12 @@ public class CropActivity extends AppCompatActivity {
@Override
public void onBackgroundColorChanged(int color) {
colorView.setBackgroundColor(color);
}
});
cropCanvasView.setOnColorPickedListener(new CropCanvasView.OnColorPickedListener() {
@Override
public void onColorPicked(int color) {
cropCanvasView.setColorPickMode(false);
btnColorPick.setAlpha(1.0f);
}

View File

@@ -16,11 +16,20 @@ public class CropCanvasView extends View {
void onBackgroundColorChanged(int color);
}
public interface OnColorPickedListener {
void onColorPicked(int color);
}
private OnBackgroundColorChangedListener backgroundColorChangedListener;
private OnColorPickedListener colorPickedListener;
public void setOnBackgroundColorChangedListener(OnBackgroundColorChangedListener listener) {
this.backgroundColorChangedListener = listener;
}
public void setOnColorPickedListener(OnColorPickedListener listener) {
this.colorPickedListener = listener;
}
private Paint imagePaint;
private Paint borderPaint;
private Paint cornerPaint;
@@ -55,6 +64,7 @@ public class CropCanvasView extends View {
private float initialSpan;
private int backgroundColor = Color.BLUE;
private boolean colorPickMode = false;
private int previewColor = 0;
private float pickX, pickY;
private float displayScale = 1.0f;
@@ -465,12 +475,40 @@ public class CropCanvasView extends View {
if (originalBitmap != null && !originalBitmap.isRecycled()) {
float imgX = screenToImageX(x);
float imgY = screenToImageY(y);
int bmpX = (int) ((imgX - imageBounds.left) / displayScale * displayBitmapScale);
int bmpY = (int) ((imgY - imageBounds.top) / displayScale * displayBitmapScale);
int bmpX = (int) ((imgX - imageBounds.left) * displayBitmapScale);
int bmpY = (int) ((imgY - imageBounds.top) * displayBitmapScale);
bmpX = Math.max(0, Math.min(bmpX, originalBitmap.getWidth() - 1));
bmpY = Math.max(0, Math.min(bmpY, originalBitmap.getHeight() - 1));
int color = originalBitmap.getPixel(bmpX, bmpY);
setBackgroundColor(color);
previewColor = originalBitmap.getPixel(bmpX, bmpY);
if (backgroundColorChangedListener != null) {
backgroundColorChangedListener.onBackgroundColorChanged(previewColor);
}
}
return true;
}
if (event.getAction() == MotionEvent.ACTION_MOVE) {
if (originalBitmap != null && !originalBitmap.isRecycled()) {
float imgX = screenToImageX(x);
float imgY = screenToImageY(y);
int bmpX = (int) ((imgX - imageBounds.left) * displayBitmapScale);
int bmpY = (int) ((imgY - imageBounds.top) * displayBitmapScale);
bmpX = Math.max(0, Math.min(bmpX, originalBitmap.getWidth() - 1));
bmpY = Math.max(0, Math.min(bmpY, originalBitmap.getHeight() - 1));
previewColor = originalBitmap.getPixel(bmpX, bmpY);
if (backgroundColorChangedListener != null) {
backgroundColorChangedListener.onBackgroundColorChanged(previewColor);
}
}
return true;
}
if (event.getAction() == MotionEvent.ACTION_UP) {
if (previewColor != 0) {
backgroundColor = previewColor;
if (colorPickedListener != null) {
colorPickedListener.onColorPicked(backgroundColor);
}
previewColor = 0;
invalidate();
}
return true;
}