diff --git a/gallery/src/main/java/cc/winboll/studio/gallery/CropActivity.java b/gallery/src/main/java/cc/winboll/studio/gallery/CropActivity.java index b68daca..e60ccca 100644 --- a/gallery/src/main/java/cc/winboll/studio/gallery/CropActivity.java +++ b/gallery/src/main/java/cc/winboll/studio/gallery/CropActivity.java @@ -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(); } diff --git a/gallery/src/main/java/cc/winboll/studio/gallery/CropCanvasView.java b/gallery/src/main/java/cc/winboll/studio/gallery/CropCanvasView.java index 92285c9..fdcc8c1 100644 --- a/gallery/src/main/java/cc/winboll/studio/gallery/CropCanvasView.java +++ b/gallery/src/main/java/cc/winboll/studio/gallery/CropCanvasView.java @@ -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; diff --git a/gallery/src/main/res/drawable/bg_color_circle.xml b/gallery/src/main/res/drawable/bg_color_circle.xml new file mode 100644 index 0000000..a9fbe48 --- /dev/null +++ b/gallery/src/main/res/drawable/bg_color_circle.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/gallery/src/main/res/drawable/bg_color_circle_border.xml b/gallery/src/main/res/drawable/bg_color_circle_border.xml new file mode 100644 index 0000000..cc52daa --- /dev/null +++ b/gallery/src/main/res/drawable/bg_color_circle_border.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/gallery/src/main/res/layout/activity_crop.xml b/gallery/src/main/res/layout/activity_crop.xml index 809df2b..ca85d7c 100644 --- a/gallery/src/main/res/layout/activity_crop.xml +++ b/gallery/src/main/res/layout/activity_crop.xml @@ -21,6 +21,27 @@ android:src="@drawable/ic_close" android:background="?attr/selectableItemBackgroundBorderless"/> + + + + + + + + + + + + + + + \ No newline at end of file