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 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
* the finger tracker will get clipped when
@ -270,7 +264,8 @@ public class ColorPickerView extends View {
}
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);
}
@ -425,70 +420,6 @@ public class ColorPickerView extends View {
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
public boolean onTouchEvent(MotionEvent event) {
boolean update = false;
@ -525,17 +456,14 @@ public class ColorPickerView extends View {
int startY = mStartTouchPoint.y;
if (mHueRect.contains(startX, startY)) {
mLastTouchedPanel = PANEL_HUE;
mHue = pointToHue(event.getY());
update = true;
} else if (mSatValRect.contains(startX, startY)) {
mLastTouchedPanel = PANEL_SAT_VAL;
float[] result = pointToSatVal(event.getX(), event.getY());
mSat = result[0];
mVal = result[1];
update = true;
} else if (mAlphaRect != null && mAlphaRect.contains(startX, startY)) {
mLastTouchedPanel = PANEL_ALPHA;
mAlpha = pointToAlpha((int)event.getX());
update = true;
}
@ -584,7 +512,7 @@ public class ColorPickerView extends View {
if (mode == MeasureSpec.AT_MOST || mode == MeasureSpec.EXACTLY) {
return size;
} 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) {
return size;
} else { // (mode == MeasureSpec.UNSPECIFIED)
return getPrefferedHeight();
return getPreferredHeight();
}
}
private int getPrefferedWidth() {
int width = getPrefferedHeight();
private int getPreferredWidth() {
int width = getPreferredHeight();
if (mShowAlphaPanel)
width -= (PANEL_SPACING + ALPHA_PANEL_HEIGHT);
return (int) (width + HUE_PANEL_WIDTH + PANEL_SPACING);
}
private int getPrefferedHeight() {
private int getPreferredHeight() {
int height = (int)(200 * mDensity);
if (mShowAlphaPanel)
height += PANEL_SPACING + ALPHA_PANEL_HEIGHT;