添加剪裁窗口颜色拾取功能
- 在工具栏添加颜色图标显示当前背景颜色 - 添加背景颜色变化监听器 - 修复画布放大时颜色拾取坐标计算 - 拾取颜色后自动退出拾取模式
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user