Code cleanup

This commit is contained in:
Peter Osterlund 2020-03-22 16:41:00 +01:00
parent 7ea6a9862f
commit d3c4be4737
2 changed files with 35 additions and 81 deletions

View File

@ -31,22 +31,18 @@ import android.view.View;
*/ */
public class ColorPickerPanelView extends View { public class ColorPickerPanelView extends View {
/** /** The width in pixels of the border surrounding the color panel. */
* The width in pixels of the border
* surrounding the color panel.
*/
private final static float BORDER_WIDTH_PX = 1; private final static float BORDER_WIDTH_PX = 1;
private float mDensity = 1f; private float mDensity = 1f;
private int mBorderColor = 0xff6E6E6E;
private int mColor = 0xff000000; private int mColor = 0xff000000;
private Paint mBorderPaint; private Paint mBorderPaint = new Paint();
private Paint mColorPaint; private Paint mColorPaint = new Paint();
private RectF mDrawingRect; private RectF mDrawingRect;
private RectF mColorRect; private RectF mColorRect;
private AlphaPatternDrawable mAlphaPattern; private AlphaPatternDrawable mAlphaPattern;
@ -65,27 +61,21 @@ public class ColorPickerPanelView extends View {
} }
private void init() { private void init() {
mBorderPaint = new Paint();
mColorPaint = new Paint();
mDensity = getContext().getResources().getDisplayMetrics().density; mDensity = getContext().getResources().getDisplayMetrics().density;
} }
@Override @Override
protected void onDraw(Canvas canvas) { protected void onDraw(Canvas canvas) {
final RectF rect = mColorRect;
if (BORDER_WIDTH_PX > 0) { if (BORDER_WIDTH_PX > 0) {
mBorderPaint.setColor(mBorderColor); mBorderPaint.setColor(0xff6E6E6E);
canvas.drawRect(mDrawingRect, mBorderPaint); canvas.drawRect(mDrawingRect, mBorderPaint);
} }
if (mAlphaPattern != null) { if (mAlphaPattern != null)
mAlphaPattern.draw(canvas); mAlphaPattern.draw(canvas);
}
mColorPaint.setColor(mColor); mColorPaint.setColor(mColor);
canvas.drawRect(mColorRect, mColorPaint);
canvas.drawRect(rect, mColorPaint);
} }
@Override @Override
@ -121,12 +111,10 @@ public class ColorPickerPanelView extends View {
mAlphaPattern = new AlphaPatternDrawable((int)(5 * mDensity)); mAlphaPattern = new AlphaPatternDrawable((int)(5 * mDensity));
mAlphaPattern.setBounds( mAlphaPattern.setBounds(Math.round(mColorRect.left),
Math.round(mColorRect.left), Math.round(mColorRect.top),
Math.round(mColorRect.top), Math.round(mColorRect.right),
Math.round(mColorRect.right), Math.round(mColorRect.bottom));
Math.round(mColorRect.bottom)
);
} }
/** /**
@ -138,24 +126,9 @@ public class ColorPickerPanelView extends View {
} }
/** /**
* Get the color currently show by this view. * Get the color currently shown by this view.
*/ */
public int getColor() { public int getColor() {
return mColor; return mColor;
} }
/**
* Set the color of the border surrounding the panel.
*/
public void setBorderColor(int color) {
mBorderColor = color;
invalidate();
}
/**
* Get the color of the border surrounding the panel.
*/
public int getBorderColor() {
return mBorderColor;
}
} }

View File

@ -34,41 +34,28 @@ import android.view.MotionEvent;
import android.view.View; import android.view.View;
/** /**
* Displays a color picker to the user and allow them * Displays a color picker to the user and allow them to select a color.
* to select a color.
* @author Daniel Nilsson * @author Daniel Nilsson
*/ */
@SuppressLint("ClickableViewAccessibility") @SuppressLint("ClickableViewAccessibility")
public class ColorPickerView extends View { public class ColorPickerView extends View {
/** The width in pixels of the border surrounding all color panels. */
/**
* The width in pixels of the border
* surrounding all color panels.
*/
private final static float BORDER_WIDTH_PX = 1; private final static float BORDER_WIDTH_PX = 1;
/** /** The width in dp of the hue panel. */
* The width in dp of the hue panel.
*/
private float HUE_PANEL_WIDTH = 30f; private float HUE_PANEL_WIDTH = 30f;
/**
* The height in dp of the alpha panel /** The height in dp of the alpha panel */
*/ private float ALPHA_PANEL_HEIGHT = 20f;
private float ALPHA_PANEL_HEIGHT = 20f;
/** /** The distance in dp between the different color panels. */
* The distance in dp between the different
* color panels.
*/
private float PANEL_SPACING = 10f; private float PANEL_SPACING = 10f;
/**
* The radius in dp of the color palette tracker circle. /** The radius in dp of the color palette tracker circle. */
*/
private float PALETTE_CIRCLE_TRACKER_RADIUS = 5f; private float PALETTE_CIRCLE_TRACKER_RADIUS = 5f;
/**
* The dp which the tracker of the hue or alpha panel /** The dp which the tracker of the hue or alpha panel will extend outside of its bounds. */
* will extend outside of its bounds. private float RECTANGLE_TRACKER_OFFSET = 2f;
*/
private float RECTANGLE_TRACKER_OFFSET = 2f;
private float mDensity = 1f; private float mDensity = 1f;
@ -91,26 +78,20 @@ public class ColorPickerView extends View {
private final int mBorderColor = 0xff6E6E6E; private final int mBorderColor = 0xff6E6E6E;
/** /** Offset from the edge we must have or else the finger tracker will
* Offset from the edge we must have or else * get clipped when it is drawn outside of the view. */
* the finger tracker will get clipped when
* it is drawn outside of the view.
*/
private float mDrawingOffset; private float mDrawingOffset;
/* /** Distance form the edges of the view of where we are allowed to draw. */
* Distance form the edges of the view private RectF mDrawingRect;
* of where we are allowed to draw.
*/
private RectF mDrawingRect;
private RectF mSatValRect; private RectF mSatValRect;
private RectF mHueRect; private RectF mHueRect;
private RectF mAlphaRect; private RectF mAlphaRect;
private AlphaPatternDrawable mAlphaPattern; private AlphaPatternDrawable mAlphaPattern;
private Point mStartTouchPoint = null; private Point mStartTouchPoint = null;
public interface OnColorChangedListener { public interface OnColorChangedListener {
void onColorChanged(int color); void onColorChanged(int color);