GradientPanel cleanup

Move common functionality to the base class.
This commit is contained in:
Peter Osterlund 2020-03-28 07:12:12 +01:00
parent fca44e4262
commit 47b093446c
5 changed files with 28 additions and 50 deletions

View File

@ -18,27 +18,15 @@ package net.margaritov.preference.colorpicker;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;
class AlphaGradientPanel extends GradientPanel {
private float RECTANGLE_TRACKER_OFFSET = 2f;
private Paint trackerPaint = new Paint();
/** Constructor. */
AlphaGradientPanel(RectF rect, AHSVColor color, float density) {
super(rect, color, density, getAlphaPattern(rect, density));
RECTANGLE_TRACKER_OFFSET *= density;
trackerPaint.setColor(0xff1c1c1c);
trackerPaint.setStyle(Paint.Style.STROKE);
trackerPaint.setStrokeWidth(2f * density);
trackerPaint.setAntiAlias(true);
}
private static Drawable getAlphaPattern(RectF rect, float density) {
@ -60,16 +48,10 @@ class AlphaGradientPanel extends GradientPanel {
gradientPaint.setShader(alphaShader);
}
@Override
protected void drawTracker(Canvas canvas) {
float rectWidth = 4 * density / 2;
Point p = alphaToPoint(color.getAlpha());
RectF r = new RectF();
r.left = p.x - rectWidth;
r.right = p.x + rectWidth;
r.top = rect.top - RECTANGLE_TRACKER_OFFSET;
r.bottom = rect.bottom + RECTANGLE_TRACKER_OFFSET;
canvas.drawRoundRect(r, 2, 2, trackerPaint);
drawRectangleTracker(canvas, p, true);
}
@Override

View File

@ -216,7 +216,7 @@ public class ColorPickerView extends View {
}
private void setUpSatValPanel() {
final RectF dRect = mDrawingRect;
RectF dRect = mDrawingRect;
float panelSide = dRect.height() - BORDER_WIDTH_PX * 2;
panelSide -= PANEL_SPACING + ALPHA_PANEL_HEIGHT;
@ -230,7 +230,7 @@ public class ColorPickerView extends View {
}
private void setUpHuePanel() {
final RectF dRect = mDrawingRect;
RectF dRect = mDrawingRect;
float left = dRect.right - HUE_PANEL_WIDTH + BORDER_WIDTH_PX;
float right = dRect.right - BORDER_WIDTH_PX;
@ -242,7 +242,7 @@ public class ColorPickerView extends View {
}
private void setUpAlphaPanel() {
final RectF dRect = mDrawingRect;
RectF dRect = mDrawingRect;
float left = dRect.left + BORDER_WIDTH_PX;
float right = dRect.right - BORDER_WIDTH_PX;

View File

@ -32,6 +32,7 @@ abstract class GradientPanel {
private Paint borderPaint = new Paint();
protected Paint gradientPaint = new Paint();
protected Paint trackerPaint = new Paint();
/** Constructor. */
GradientPanel(RectF rect, AHSVColor color, float density, Drawable background) {
@ -40,6 +41,11 @@ abstract class GradientPanel {
this.density = density;
this.background = background;
borderPaint.setColor(0xff6E6E6E);
trackerPaint.setColor(0xff1c1c1c);
trackerPaint.setStyle(Paint.Style.STROKE);
trackerPaint.setStrokeWidth(2f * density);
trackerPaint.setAntiAlias(true);
}
boolean contains(Point point) {
@ -73,4 +79,18 @@ abstract class GradientPanel {
/** Draw "current color" tracker marker. */
abstract protected void drawTracker(Canvas canvas);
protected void drawRectangleTracker(Canvas canvas, Point p, boolean horizontal) {
float size = 2f * density;
RectF r = new RectF(rect);
r.inset(-size, -size);
if (horizontal) {
r.left = p.x - size;
r.right = p.x + size;
} else {
r.top = p.y - size;
r.bottom = p.y + size;
}
canvas.drawRoundRect(r, 2, 2, trackerPaint);
}
}

View File

@ -19,30 +19,17 @@ package net.margaritov.preference.colorpicker;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.graphics.Shader;
public class HueGradientPanel extends GradientPanel {
private float RECTANGLE_TRACKER_OFFSET = 2f;
private Paint trackerPaint = new Paint();
/** Constructor. */
HueGradientPanel(RectF rect, AHSVColor color, float density) {
super(rect, color, density, null);
RECTANGLE_TRACKER_OFFSET *= density;
Shader hueShader = new LinearGradient(rect.left, rect.top, rect.left, rect.bottom,
buildHueColorArray(), null, Shader.TileMode.CLAMP);
gradientPaint.setShader(hueShader);
trackerPaint.setColor(0xff1c1c1c);
trackerPaint.setStyle(Paint.Style.STROKE);
trackerPaint.setStrokeWidth(2f * density);
trackerPaint.setAntiAlias(true);
}
private int[] buildHueColorArray() {
@ -56,16 +43,10 @@ public class HueGradientPanel extends GradientPanel {
protected void setGradientPaint() {
}
@Override
protected void drawTracker(Canvas canvas) {
float rectHeight = 4 * density / 2;
Point p = hueToPoint(color.getHSV()[0]);
RectF r = new RectF();
r.left = rect.left - RECTANGLE_TRACKER_OFFSET;
r.right = rect.right + RECTANGLE_TRACKER_OFFSET;
r.top = p.y - rectHeight;
r.bottom = p.y + rectHeight;
canvas.drawRoundRect(r, 2, 2, trackerPaint);
drawRectangleTracker(canvas, p, false);
}
@Override

View File

@ -30,8 +30,6 @@ public class SatValGradientPanel extends GradientPanel {
private Shader valShader;
private Paint trackerPaint = new Paint();
/** Constructor. */
SatValGradientPanel(RectF rect, AHSVColor color, float density) {
super(rect, color, density, null);
@ -40,10 +38,6 @@ public class SatValGradientPanel extends GradientPanel {
valShader = new LinearGradient(rect.left, rect.top, rect.left, rect.bottom,
0xffffffff, 0xff000000, Shader.TileMode.CLAMP);
trackerPaint.setStyle(Paint.Style.STROKE);
trackerPaint.setStrokeWidth(2f * density);
trackerPaint.setAntiAlias(true);
}
@Override
@ -59,6 +53,7 @@ public class SatValGradientPanel extends GradientPanel {
gradientPaint.setShader(shader);
}
@Override
protected void drawTracker(Canvas canvas) {
float[] hsv = color.getHSV();
Point p = satValToPoint(hsv[1], hsv[2]);