添加剪裁窗口颜色拾取功能
- 在工具栏添加颜色图标显示当前背景颜色 - 添加背景颜色变化监听器 - 修复画布放大时颜色拾取坐标计算 - 拾取颜色后自动退出拾取模式
This commit is contained in:
@@ -98,6 +98,19 @@ public class CropActivity extends AppCompatActivity {
|
|||||||
});
|
});
|
||||||
|
|
||||||
cropCanvasView = findViewById(R.id.crop_canvas_view);
|
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();
|
loadImage();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,15 @@ import android.view.MotionEvent;
|
|||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
|
||||||
public class CropCanvasView extends 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 imagePaint;
|
||||||
private Paint borderPaint;
|
private Paint borderPaint;
|
||||||
private Paint cornerPaint;
|
private Paint cornerPaint;
|
||||||
@@ -39,6 +48,8 @@ public class CropCanvasView extends View {
|
|||||||
private RectF imageBounds = new RectF();
|
private RectF imageBounds = new RectF();
|
||||||
private RectF canvasBounds = new RectF();
|
private RectF canvasBounds = new RectF();
|
||||||
private Bitmap originalBitmap;
|
private Bitmap originalBitmap;
|
||||||
|
private Bitmap canvasBitmap;
|
||||||
|
private float bitmapScale = 1.0f;
|
||||||
private Bitmap displayBitmap;
|
private Bitmap displayBitmap;
|
||||||
private RectF initialSpanRect;
|
private RectF initialSpanRect;
|
||||||
private float initialSpan;
|
private float initialSpan;
|
||||||
@@ -239,6 +250,9 @@ public class CropCanvasView extends View {
|
|||||||
|
|
||||||
public void setBackgroundColor(int color) {
|
public void setBackgroundColor(int color) {
|
||||||
this.backgroundColor = color;
|
this.backgroundColor = color;
|
||||||
|
if (backgroundColorChangedListener != null) {
|
||||||
|
backgroundColorChangedListener.onBackgroundColorChanged(color);
|
||||||
|
}
|
||||||
invalidate();
|
invalidate();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -448,10 +462,16 @@ public class CropCanvasView extends View {
|
|||||||
|
|
||||||
if (colorPickMode) {
|
if (colorPickMode) {
|
||||||
if (event.getAction() == MotionEvent.ACTION_DOWN) {
|
if (event.getAction() == MotionEvent.ACTION_DOWN) {
|
||||||
pickX = x;
|
if (originalBitmap != null && !originalBitmap.isRecycled()) {
|
||||||
pickY = y;
|
float imgX = screenToImageX(x);
|
||||||
int color = getColorAt(x, y);
|
float imgY = screenToImageY(y);
|
||||||
setBackgroundColor(color);
|
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;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
6
gallery/src/main/res/drawable/bg_color_circle.xml
Normal file
6
gallery/src/main/res/drawable/bg_color_circle.xml
Normal 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>
|
||||||
6
gallery/src/main/res/drawable/bg_color_circle_border.xml
Normal file
6
gallery/src/main/res/drawable/bg_color_circle_border.xml
Normal 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>
|
||||||
@@ -21,6 +21,27 @@
|
|||||||
android:src="@drawable/ic_close"
|
android:src="@drawable/ic_close"
|
||||||
android:background="?attr/selectableItemBackgroundBorderless"/>
|
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
|
<ImageView
|
||||||
android:id="@+id/btn_color_pick"
|
android:id="@+id/btn_color_pick"
|
||||||
android:layout_width="48dp"
|
android:layout_width="48dp"
|
||||||
|
|||||||
20
gallery/src/main/res/layout/item_color_icon.xml
Normal file
20
gallery/src/main/res/layout/item_color_icon.xml
Normal 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>
|
||||||
Reference in New Issue
Block a user