添加剪裁窗口颜色拾取功能

- 在工具栏添加颜色图标显示当前背景颜色
- 添加背景颜色变化监听器
- 修复画布放大时颜色拾取坐标计算
- 拾取颜色后自动退出拾取模式
This commit is contained in:
2026-04-28 12:35:55 +08:00
parent a7e2646eca
commit 474ddcbb3b
6 changed files with 90 additions and 4 deletions

View File

@@ -98,6 +98,19 @@ public class CropActivity extends AppCompatActivity {
});
cropCanvasView = findViewById(R.id.crop_canvas_view);
final View colorView = findViewById(R.id.color_view);
cropCanvasView.setOnBackgroundColorChangedListener(new CropCanvasView.OnBackgroundColorChangedListener() {
@Override
public void onBackgroundColorChanged(int color) {
colorView.setBackgroundColor(color);
cropCanvasView.setColorPickMode(false);
btnColorPick.setAlpha(1.0f);
}
});
colorView.setBackgroundColor(cropCanvasView.getBackgroundColor());
loadImage();
}

View File

@@ -12,6 +12,15 @@ import android.view.MotionEvent;
import android.view.View;
public class CropCanvasView extends View {
public interface OnBackgroundColorChangedListener {
void onBackgroundColorChanged(int color);
}
private OnBackgroundColorChangedListener backgroundColorChangedListener;
public void setOnBackgroundColorChangedListener(OnBackgroundColorChangedListener listener) {
this.backgroundColorChangedListener = listener;
}
private Paint imagePaint;
private Paint borderPaint;
private Paint cornerPaint;
@@ -39,6 +48,8 @@ public class CropCanvasView extends View {
private RectF imageBounds = new RectF();
private RectF canvasBounds = new RectF();
private Bitmap originalBitmap;
private Bitmap canvasBitmap;
private float bitmapScale = 1.0f;
private Bitmap displayBitmap;
private RectF initialSpanRect;
private float initialSpan;
@@ -239,6 +250,9 @@ public class CropCanvasView extends View {
public void setBackgroundColor(int color) {
this.backgroundColor = color;
if (backgroundColorChangedListener != null) {
backgroundColorChangedListener.onBackgroundColorChanged(color);
}
invalidate();
}
@@ -448,10 +462,16 @@ public class CropCanvasView extends View {
if (colorPickMode) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
pickX = x;
pickY = y;
int color = getColorAt(x, y);
setBackgroundColor(color);
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);
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);
}
return true;
}
return true;

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@android:color/holo_green_dark"/>
<size android:width="32dp" android:height="32dp"/>
</shape>

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke android:width="2dp" android:color="@android:color/white"/>
<size android:width="36dp" android:height="36dp"/>
</shape>

View File

@@ -21,6 +21,27 @@
android:src="@drawable/ic_close"
android:background="?attr/selectableItemBackgroundBorderless"/>
<FrameLayout
android:id="@+id/color_icon_container"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginStart="8dp">
<View
android:id="@+id/color_view"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_gravity="center"
android:background="@drawable/bg_color_circle"/>
<View
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_gravity="center"
android:background="@drawable/bg_color_circle_border"/>
</FrameLayout>
<ImageView
android:id="@+id/btn_color_pick"
android:layout_width="48dp"

View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="40dp"
android:layout_height="40dp">
<View
android:id="@+id/color_view"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_gravity="center"
android:background="@drawable/bg_color_circle"/>
<View
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_gravity="center"
android:background="@drawable/bg_color_circle_border"/>
</FrameLayout>