Cleanup in ColorPicker code

Also remove trackball event support from ColorPickerView.
This commit is contained in:
Peter Osterlund 2020-03-22 01:55:53 +01:00
parent 0c2884ddf8
commit 647be33b48

View File

@ -108,12 +108,6 @@ public class ColorPickerView extends View {
private int mBorderColor = 0xff6E6E6E; private int mBorderColor = 0xff6E6E6E;
private boolean mShowAlphaPanel = false; private boolean mShowAlphaPanel = false;
/*
* To remember which panel that has the "focus" when
* processing hardware button data.
*/
private int mLastTouchedPanel = PANEL_SAT_VAL;
/** /**
* Offset from the edge we must have or else * Offset from the edge we must have or else
* the finger tracker will get clipped when * the finger tracker will get clipped when
@ -270,7 +264,8 @@ public class ColorPickerView extends View {
} }
if (mHueShader == null) { if (mHueShader == null) {
mHueShader = new LinearGradient(rect.left, rect.top, rect.left, rect.bottom, buildHueColorArray(), null, TileMode.CLAMP); mHueShader = new LinearGradient(rect.left, rect.top, rect.left, rect.bottom,
buildHueColorArray(), null, TileMode.CLAMP);
mHuePaint.setShader(mHueShader); mHuePaint.setShader(mHueShader);
} }
@ -425,70 +420,6 @@ public class ColorPickerView extends View {
return 0xff - (x * 0xff / width); return 0xff - (x * 0xff / width);
} }
@Override
public boolean onTrackballEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
boolean update = false;
if (event.getAction() == MotionEvent.ACTION_MOVE) {
switch (mLastTouchedPanel) {
case PANEL_SAT_VAL:
float sat, val;
sat = mSat + x/50f;
val = mVal - y/50f;
if (sat < 0f)
sat = 0f;
else if (sat > 1f)
sat = 1f;
if (val < 0f)
val = 0f;
else if (val > 1f)
val = 1f;
mSat = sat;
mVal = val;
update = true;
break;
case PANEL_HUE:
float hue = mHue - y * 10f;
if (hue < 0f)
hue = 0f;
else if (hue > 360f)
hue = 360f;
mHue = hue;
update = true;
break;
case PANEL_ALPHA:
if (!mShowAlphaPanel || mAlphaRect == null) {
update = false;
} else {
int alpha = (int) (mAlpha - x*10);
if (alpha < 0)
alpha = 0;
else if (alpha > 0xff)
alpha = 0xff;
mAlpha = alpha;
update = true;
}
break;
}
}
if (update) {
if (mListener != null) {
mListener.onColorChanged(Color.HSVToColor(mAlpha, new float[]{mHue, mSat, mVal}));
}
invalidate();
return true;
}
return super.onTrackballEvent(event);
}
@Override @Override
public boolean onTouchEvent(MotionEvent event) { public boolean onTouchEvent(MotionEvent event) {
boolean update = false; boolean update = false;
@ -525,17 +456,14 @@ public class ColorPickerView extends View {
int startY = mStartTouchPoint.y; int startY = mStartTouchPoint.y;
if (mHueRect.contains(startX, startY)) { if (mHueRect.contains(startX, startY)) {
mLastTouchedPanel = PANEL_HUE;
mHue = pointToHue(event.getY()); mHue = pointToHue(event.getY());
update = true; update = true;
} else if (mSatValRect.contains(startX, startY)) { } else if (mSatValRect.contains(startX, startY)) {
mLastTouchedPanel = PANEL_SAT_VAL;
float[] result = pointToSatVal(event.getX(), event.getY()); float[] result = pointToSatVal(event.getX(), event.getY());
mSat = result[0]; mSat = result[0];
mVal = result[1]; mVal = result[1];
update = true; update = true;
} else if (mAlphaRect != null && mAlphaRect.contains(startX, startY)) { } else if (mAlphaRect != null && mAlphaRect.contains(startX, startY)) {
mLastTouchedPanel = PANEL_ALPHA;
mAlpha = pointToAlpha((int)event.getX()); mAlpha = pointToAlpha((int)event.getX());
update = true; update = true;
} }
@ -584,7 +512,7 @@ public class ColorPickerView extends View {
if (mode == MeasureSpec.AT_MOST || mode == MeasureSpec.EXACTLY) { if (mode == MeasureSpec.AT_MOST || mode == MeasureSpec.EXACTLY) {
return size; return size;
} else { // (mode == MeasureSpec.UNSPECIFIED) } else { // (mode == MeasureSpec.UNSPECIFIED)
return getPrefferedWidth(); return getPreferredWidth();
} }
} }
@ -592,18 +520,18 @@ public class ColorPickerView extends View {
if (mode == MeasureSpec.AT_MOST || mode == MeasureSpec.EXACTLY) { if (mode == MeasureSpec.AT_MOST || mode == MeasureSpec.EXACTLY) {
return size; return size;
} else { // (mode == MeasureSpec.UNSPECIFIED) } else { // (mode == MeasureSpec.UNSPECIFIED)
return getPrefferedHeight(); return getPreferredHeight();
} }
} }
private int getPrefferedWidth() { private int getPreferredWidth() {
int width = getPrefferedHeight(); int width = getPreferredHeight();
if (mShowAlphaPanel) if (mShowAlphaPanel)
width -= (PANEL_SPACING + ALPHA_PANEL_HEIGHT); width -= (PANEL_SPACING + ALPHA_PANEL_HEIGHT);
return (int) (width + HUE_PANEL_WIDTH + PANEL_SPACING); return (int) (width + HUE_PANEL_WIDTH + PANEL_SPACING);
} }
private int getPrefferedHeight() { private int getPreferredHeight() {
int height = (int)(200 * mDensity); int height = (int)(200 * mDensity);
if (mShowAlphaPanel) if (mShowAlphaPanel)
height += PANEL_SPACING + ALPHA_PANEL_HEIGHT; height += PANEL_SPACING + ALPHA_PANEL_HEIGHT;