优化剪裁窗口颜色拾取功能
- 按下手指时预览颜色,放开时确认拾取 - 拖动时实时跟随手指位置更新颜色 - 分离颜色预览和确认拾取的回调接口
This commit is contained in:
@@ -104,6 +104,12 @@ public class CropActivity extends AppCompatActivity {
|
|||||||
@Override
|
@Override
|
||||||
public void onBackgroundColorChanged(int color) {
|
public void onBackgroundColorChanged(int color) {
|
||||||
colorView.setBackgroundColor(color);
|
colorView.setBackgroundColor(color);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
cropCanvasView.setOnColorPickedListener(new CropCanvasView.OnColorPickedListener() {
|
||||||
|
@Override
|
||||||
|
public void onColorPicked(int color) {
|
||||||
cropCanvasView.setColorPickMode(false);
|
cropCanvasView.setColorPickMode(false);
|
||||||
btnColorPick.setAlpha(1.0f);
|
btnColorPick.setAlpha(1.0f);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,11 +16,20 @@ public class CropCanvasView extends View {
|
|||||||
void onBackgroundColorChanged(int color);
|
void onBackgroundColorChanged(int color);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public interface OnColorPickedListener {
|
||||||
|
void onColorPicked(int color);
|
||||||
|
}
|
||||||
|
|
||||||
private OnBackgroundColorChangedListener backgroundColorChangedListener;
|
private OnBackgroundColorChangedListener backgroundColorChangedListener;
|
||||||
|
private OnColorPickedListener colorPickedListener;
|
||||||
|
|
||||||
public void setOnBackgroundColorChangedListener(OnBackgroundColorChangedListener listener) {
|
public void setOnBackgroundColorChangedListener(OnBackgroundColorChangedListener listener) {
|
||||||
this.backgroundColorChangedListener = listener;
|
this.backgroundColorChangedListener = listener;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setOnColorPickedListener(OnColorPickedListener listener) {
|
||||||
|
this.colorPickedListener = listener;
|
||||||
|
}
|
||||||
private Paint imagePaint;
|
private Paint imagePaint;
|
||||||
private Paint borderPaint;
|
private Paint borderPaint;
|
||||||
private Paint cornerPaint;
|
private Paint cornerPaint;
|
||||||
@@ -55,6 +64,7 @@ public class CropCanvasView extends View {
|
|||||||
private float initialSpan;
|
private float initialSpan;
|
||||||
private int backgroundColor = Color.BLUE;
|
private int backgroundColor = Color.BLUE;
|
||||||
private boolean colorPickMode = false;
|
private boolean colorPickMode = false;
|
||||||
|
private int previewColor = 0;
|
||||||
private float pickX, pickY;
|
private float pickX, pickY;
|
||||||
|
|
||||||
private float displayScale = 1.0f;
|
private float displayScale = 1.0f;
|
||||||
@@ -465,12 +475,40 @@ public class CropCanvasView extends View {
|
|||||||
if (originalBitmap != null && !originalBitmap.isRecycled()) {
|
if (originalBitmap != null && !originalBitmap.isRecycled()) {
|
||||||
float imgX = screenToImageX(x);
|
float imgX = screenToImageX(x);
|
||||||
float imgY = screenToImageY(y);
|
float imgY = screenToImageY(y);
|
||||||
int bmpX = (int) ((imgX - imageBounds.left) / displayScale * displayBitmapScale);
|
int bmpX = (int) ((imgX - imageBounds.left) * displayBitmapScale);
|
||||||
int bmpY = (int) ((imgY - imageBounds.top) / displayScale * displayBitmapScale);
|
int bmpY = (int) ((imgY - imageBounds.top) * displayBitmapScale);
|
||||||
bmpX = Math.max(0, Math.min(bmpX, originalBitmap.getWidth() - 1));
|
bmpX = Math.max(0, Math.min(bmpX, originalBitmap.getWidth() - 1));
|
||||||
bmpY = Math.max(0, Math.min(bmpY, originalBitmap.getHeight() - 1));
|
bmpY = Math.max(0, Math.min(bmpY, originalBitmap.getHeight() - 1));
|
||||||
int color = originalBitmap.getPixel(bmpX, bmpY);
|
previewColor = originalBitmap.getPixel(bmpX, bmpY);
|
||||||
setBackgroundColor(color);
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user