From 47b093446c67b21dc9096e906ee3b40d2d9f5838 Mon Sep 17 00:00:00 2001 From: Peter Osterlund Date: Sat, 28 Mar 2020 07:12:12 +0100 Subject: [PATCH] GradientPanel cleanup Move common functionality to the base class. --- .../colorpicker/AlphaGradientPanel.java | 22 ++---------------- .../colorpicker/ColorPickerView.java | 6 ++--- .../preference/colorpicker/GradientPanel.java | 20 ++++++++++++++++ .../colorpicker/HueGradientPanel.java | 23 ++----------------- .../colorpicker/SatValGradientPanel.java | 7 +----- 5 files changed, 28 insertions(+), 50 deletions(-) diff --git a/DroidFishApp/src/main/java/net/margaritov/preference/colorpicker/AlphaGradientPanel.java b/DroidFishApp/src/main/java/net/margaritov/preference/colorpicker/AlphaGradientPanel.java index cb4b944..2962189 100644 --- a/DroidFishApp/src/main/java/net/margaritov/preference/colorpicker/AlphaGradientPanel.java +++ b/DroidFishApp/src/main/java/net/margaritov/preference/colorpicker/AlphaGradientPanel.java @@ -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 diff --git a/DroidFishApp/src/main/java/net/margaritov/preference/colorpicker/ColorPickerView.java b/DroidFishApp/src/main/java/net/margaritov/preference/colorpicker/ColorPickerView.java index d057bee..f52cd85 100644 --- a/DroidFishApp/src/main/java/net/margaritov/preference/colorpicker/ColorPickerView.java +++ b/DroidFishApp/src/main/java/net/margaritov/preference/colorpicker/ColorPickerView.java @@ -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; diff --git a/DroidFishApp/src/main/java/net/margaritov/preference/colorpicker/GradientPanel.java b/DroidFishApp/src/main/java/net/margaritov/preference/colorpicker/GradientPanel.java index af0ee5a..cee1c3b 100644 --- a/DroidFishApp/src/main/java/net/margaritov/preference/colorpicker/GradientPanel.java +++ b/DroidFishApp/src/main/java/net/margaritov/preference/colorpicker/GradientPanel.java @@ -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); + } } diff --git a/DroidFishApp/src/main/java/net/margaritov/preference/colorpicker/HueGradientPanel.java b/DroidFishApp/src/main/java/net/margaritov/preference/colorpicker/HueGradientPanel.java index aeb06a1..c078239 100644 --- a/DroidFishApp/src/main/java/net/margaritov/preference/colorpicker/HueGradientPanel.java +++ b/DroidFishApp/src/main/java/net/margaritov/preference/colorpicker/HueGradientPanel.java @@ -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 diff --git a/DroidFishApp/src/main/java/net/margaritov/preference/colorpicker/SatValGradientPanel.java b/DroidFishApp/src/main/java/net/margaritov/preference/colorpicker/SatValGradientPanel.java index 3d2716c..41737d0 100644 --- a/DroidFishApp/src/main/java/net/margaritov/preference/colorpicker/SatValGradientPanel.java +++ b/DroidFishApp/src/main/java/net/margaritov/preference/colorpicker/SatValGradientPanel.java @@ -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]);