White space changes and warning fixes in color picker code

This commit is contained in:
Peter Osterlund 2020-02-13 20:29:30 +01:00
parent 421aeb8a30
commit 8789e8f9c0
5 changed files with 1361 additions and 1529 deletions

View File

@ -33,7 +33,7 @@ import android.graphics.drawable.Drawable;
*/
public class AlphaPatternDrawable extends Drawable {
private int mRectangleSize = 10;
private int mRectangleSize;
private Paint mPaint = new Paint();
private Paint mPaintWhite = new Paint();
@ -43,7 +43,7 @@ public class AlphaPatternDrawable extends Drawable {
private int numRectanglesVertical;
/**
* Bitmap in which the pattern will be cahched.
* Bitmap in which the pattern will be cached.
*/
private Bitmap mBitmap;
@ -65,65 +65,47 @@ public class AlphaPatternDrawable extends Drawable {
@Override
public void setAlpha(int alpha) {
throw new UnsupportedOperationException("Alpha is not supported by this drawwable.");
throw new UnsupportedOperationException("Alpha is not supported by this drawable.");
}
@Override
public void setColorFilter(ColorFilter cf) {
throw new UnsupportedOperationException("ColorFilter is not supported by this drawwable.");
throw new UnsupportedOperationException("ColorFilter is not supported by this drawable.");
}
@Override
protected void onBoundsChange(Rect bounds) {
super.onBoundsChange(bounds);
int height = bounds.height();
int width = bounds.width();
numRectanglesHorizontal = (int) Math.ceil((width / mRectangleSize));
numRectanglesVertical = (int) Math.ceil(height / mRectangleSize);
numRectanglesHorizontal = bounds.width() / mRectangleSize;
numRectanglesVertical = bounds.height() / mRectangleSize;
generatePatternBitmap();
}
/**
* This will generate a bitmap with the pattern
* as big as the rectangle we were allow to draw on.
* We do this to chache the bitmap so we don't need to
* We do this to cache the bitmap so we don't need to
* recreate it each time draw() is called since it
* takes a few milliseconds.
*/
private void generatePatternBitmap() {
if(getBounds().width() <= 0 || getBounds().height() <= 0){
if (getBounds().width() <= 0 || getBounds().height() <= 0)
return;
}
mBitmap = Bitmap.createBitmap(getBounds().width(), getBounds().height(), Config.ARGB_8888);
Canvas canvas = new Canvas(mBitmap);
Rect r = new Rect();
boolean verticalStartWhite = true;
for (int i = 0; i <= numRectanglesVertical; i++) {
boolean isWhite = verticalStartWhite;
for (int j = 0; j <= numRectanglesHorizontal; j++) {
boolean isWhite = (i + j) % 2 == 0;
r.top = i * mRectangleSize;
r.left = j * mRectangleSize;
r.bottom = r.top + mRectangleSize;
r.right = r.left + mRectangleSize;
canvas.drawRect(r, isWhite ? mPaintWhite : mPaintGray);
isWhite = !isWhite;
}
verticalStartWhite = !verticalStartWhite;
}
}
}

View File

@ -57,7 +57,6 @@ public class ColorPickerDialog
getWindow().setFormat(PixelFormat.RGBA_8888);
setUp(color);
}
private void setUp(int color) {
@ -82,12 +81,10 @@ public class ColorPickerDialog
mColorPicker.setOnColorChangedListener(this);
mOldColor.setColor(color);
mColorPicker.setColor(color, true);
}
@Override
public void onColorChanged(int color) {
mNewColor.setColor(color);
/*
@ -95,7 +92,6 @@ public class ColorPickerDialog
mListener.onColorChanged(color);
}
*/
}
public void setAlphaSliderVisible(boolean visible) {

View File

@ -28,7 +28,6 @@ import android.view.View;
* It can be used to show the currently selected color which you will get from
* the {@link ColorPickerView}.
* @author Daniel Nilsson
*
*/
public class ColorPickerPanelView extends View {
@ -71,10 +70,8 @@ public class ColorPickerPanelView extends View {
mDensity = getContext().getResources().getDisplayMetrics().density;
}
@Override
protected void onDraw(Canvas canvas) {
final RectF rect = mColorRect;
if (BORDER_WIDTH_PX > 0) {
@ -93,7 +90,6 @@ public class ColorPickerPanelView extends View {
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
@ -111,7 +107,6 @@ public class ColorPickerPanelView extends View {
mDrawingRect.bottom = h - getPaddingBottom();
setUpColorRect();
}
private void setUpColorRect() {
@ -132,7 +127,6 @@ public class ColorPickerPanelView extends View {
Math.round(mColorRect.right),
Math.round(mColorRect.bottom)
);
}
/**
@ -167,5 +161,4 @@ public class ColorPickerPanelView extends View {
public int getBorderColor() {
return mBorderColor;
}
}

View File

@ -31,7 +31,6 @@ import android.widget.ImageView;
import android.widget.LinearLayout;
/**
* A preference type that allows a user to choose a time
* @author Sergey Margaritov
*/
public class ColorPickerPreference
@ -41,9 +40,9 @@ public class ColorPickerPreference
Preference.OnPreferenceClickListener,
ColorPickerDialog.OnColorChangedListener {
View mView;
ColorPickerDialog mDialog;
int mDefaultValue = Color.BLACK;
private View mView;
private ColorPickerDialog mDialog;
private int mDefaultValue = Color.BLACK;
private int mValue = Color.BLACK;
private float mDensity = 0;
private boolean mAlphaSliderEnabled = false;
@ -100,7 +99,6 @@ public class ColorPickerPreference
setPreviewColor();
}
@SuppressWarnings("deprecation")
private void setPreviewColor() {
if (mView == null) return;
ImageView iView = new ImageView(getContext());
@ -130,10 +128,9 @@ public class ColorPickerPreference
Bitmap bm = Bitmap.createBitmap(d, d, Config.ARGB_8888);
int w = bm.getWidth();
int h = bm.getHeight();
int c = color;
for (int i = 0; i < w; i++) {
for (int j = i; j < h; j++) {
c = (i <= 1 || j <= 1 || i >= w-2 || j >= h-2) ? Color.GRAY : color;
int c = (i <= 1 || j <= 1 || i >= w-2 || j >= h-2) ? Color.GRAY : color;
bm.setPixel(i, j, c);
if (i != j) {
bm.setPixel(j, i, c);
@ -193,7 +190,7 @@ public class ColorPickerPreference
return false;
}
protected void showDialog(Bundle state) {
private void showDialog(Bundle state) {
mDialog = new ColorPickerDialog(getContext(), getValue(), getTitle());
mDialog.setOnColorChangedListener(this);
if (mAlphaSliderEnabled) {
@ -214,11 +211,11 @@ public class ColorPickerPreference
}
/**
* For custom purposes. Not used by ColorPickerPreferrence
* For custom purposes. Not used by ColorPickerPreference
* @param color
* @author Unknown
*/
public static String convertToARGB(int color) {
private static String convertToARGB(int color) {
String alpha = Integer.toHexString(Color.alpha(color));
String red = Integer.toHexString(Color.red(color));
String green = Integer.toHexString(Color.green(color));
@ -249,8 +246,7 @@ public class ColorPickerPreference
* @throws NumberFormatException
* @author Unknown
*/
public static int convertToColorInt(String argb) throws NumberFormatException {
private static int convertToColorInt(String argb) throws NumberFormatException {
if (argb.startsWith("#")) {
argb = argb.replace("#", "");
}

View File

@ -171,7 +171,6 @@ public class ColorPickerView extends View {
}
private void initPaintTools() {
mSatValPaint = new Paint();
mSatValTrackerPaint = new Paint();
mHuePaint = new Paint();
@ -207,7 +206,6 @@ public class ColorPickerView extends View {
}
private int[] buildHueColorArray() {
int[] hue = new int[361];
int count = 0;
@ -218,31 +216,28 @@ public class ColorPickerView extends View {
return hue;
}
@Override
protected void onDraw(Canvas canvas) {
if(mDrawingRect.width() <= 0 || mDrawingRect.height() <= 0) return;
if (mDrawingRect.width() <= 0 || mDrawingRect.height() <= 0)
return;
drawSatValPanel(canvas);
drawHuePanel(canvas);
drawAlphaPanel(canvas);
}
private void drawSatValPanel(Canvas canvas) {
final RectF rect = mSatValRect;
if (BORDER_WIDTH_PX > 0) {
mBorderPaint.setColor(mBorderColor);
canvas.drawRect(mDrawingRect.left, mDrawingRect.top, rect.right + BORDER_WIDTH_PX, rect.bottom + BORDER_WIDTH_PX, mBorderPaint);
canvas.drawRect(mDrawingRect.left, mDrawingRect.top, rect.right + BORDER_WIDTH_PX,
rect.bottom + BORDER_WIDTH_PX, mBorderPaint);
}
if (mValShader == null) {
if (mValShader == null)
mValShader = new LinearGradient(rect.left, rect.top, rect.left, rect.bottom,
0xffffffff, 0xff000000, TileMode.CLAMP);
}
int rgb = Color.HSVToColor(new float[]{mHue,1f,1f});
@ -260,11 +255,9 @@ public class ColorPickerView extends View {
mSatValTrackerPaint.setColor(0xffdddddd);
canvas.drawCircle(p.x, p.y, PALETTE_CIRCLE_TRACKER_RADIUS, mSatValTrackerPaint);
}
private void drawHuePanel(Canvas canvas) {
final RectF rect = mHueRect;
if (BORDER_WIDTH_PX > 0) {
@ -293,14 +286,12 @@ public class ColorPickerView extends View {
r.top = p.y - rectHeight;
r.bottom = p.y + rectHeight;
canvas.drawRoundRect(r, 2, 2, mHueTrackerPaint);
}
private void drawAlphaPanel(Canvas canvas) {
if(!mShowAlphaPanel || mAlphaRect == null || mAlphaPattern == null) return;
if (!mShowAlphaPanel || mAlphaRect == null || mAlphaPattern == null)
return;
final RectF rect = mAlphaRect;
@ -313,7 +304,6 @@ public class ColorPickerView extends View {
mBorderPaint);
}
mAlphaPattern.draw(canvas);
float[] hsv = new float[]{mHue,mSat,mVal};
@ -323,7 +313,6 @@ public class ColorPickerView extends View {
mAlphaShader = new LinearGradient(rect.left, rect.top, rect.right, rect.top,
color, acolor, TileMode.CLAMP);
mAlphaPaint.setShader(mAlphaShader);
canvas.drawRect(rect, mAlphaPaint);
@ -343,12 +332,9 @@ public class ColorPickerView extends View {
r.bottom = rect.bottom + RECTANGLE_TRACKER_OFFSET;
canvas.drawRoundRect(r, 2, 2, mHueTrackerPaint);
}
private Point hueToPoint(float hue) {
final RectF rect = mHueRect;
final float height = rect.height();
@ -361,13 +347,11 @@ public class ColorPickerView extends View {
}
private Point satValToPoint(float sat, float val) {
final RectF rect = mSatValRect;
final float height = rect.height();
final float width = rect.width();
Point p = new Point();
p.x = (int) (sat * width + rect.left);
p.y = (int) ((1f - val) * height + rect.top);
@ -375,47 +359,36 @@ public class ColorPickerView extends View {
}
private Point alphaToPoint(int alpha) {
final RectF rect = mAlphaRect;
final float width = rect.width();
Point p = new Point();
p.x = (int) (width - (alpha * width / 0xff) + rect.left);
p.y = (int) rect.top;
return p;
}
private float[] pointToSatVal(float x, float y) {
final RectF rect = mSatValRect;
float[] result = new float[2];
float width = rect.width();
float height = rect.height();
if (x < rect.left){
if (x < rect.left)
x = 0f;
}
else if(x > rect.right){
else if (x > rect.right)
x = width;
}
else{
else
x = x - rect.left;
}
if (y < rect.top){
if (y < rect.top)
y = 0f;
}
else if(y > rect.bottom){
else if (y > rect.bottom)
y = height;
}
else{
else
y = y - rect.top;
}
result[0] = 1.f / width * x;
result[1] = 1.f - (1.f / height * y);
@ -424,133 +397,87 @@ public class ColorPickerView extends View {
}
private float pointToHue(float y) {
final RectF rect = mHueRect;
float height = rect.height();
if (y < rect.top){
if (y < rect.top)
y = 0f;
}
else if(y > rect.bottom){
else if (y > rect.bottom)
y = height;
}
else{
else
y = y - rect.top;
}
return 360f - (y * 360f / height);
}
private int pointToAlpha(int x) {
final RectF rect = mAlphaRect;
final int width = (int) rect.width();
if(x < rect.left){
if (x < rect.left)
x = 0;
}
else if(x > rect.right){
else if (x > rect.right)
x = width;
}
else{
else
x = x - (int)rect.left;
}
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){
if (sat < 0f)
sat = 0f;
}
else if(sat > 1f){
else if (sat > 1f)
sat = 1f;
}
if(val < 0f){
if (val < 0f)
val = 0f;
}
else if(val > 1f){
else if (val > 1f)
val = 1f;
}
mSat = sat;
mVal = val;
update = true;
break;
case PANEL_HUE:
float hue = mHue - y * 10f;
if(hue < 0f){
if (hue < 0f)
hue = 0f;
}
else if(hue > 360f){
else if (hue > 360f)
hue = 360f;
}
mHue = hue;
update = true;
break;
case PANEL_ALPHA:
if (!mShowAlphaPanel || mAlphaRect == null) {
update = false;
}
else{
} else {
int alpha = (int) (mAlpha - x*10);
if(alpha < 0){
if (alpha < 0)
alpha = 0;
}
else if(alpha > 0xff){
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}));
}
@ -559,99 +486,65 @@ public class ColorPickerView extends View {
return true;
}
return super.onTrackballEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
boolean update = false;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mStartTouchPoint = new Point((int)event.getX(), (int)event.getY());
update = moveTrackersIfNeeded(event);
break;
case MotionEvent.ACTION_MOVE:
update = moveTrackersIfNeeded(event);
break;
case MotionEvent.ACTION_UP:
mStartTouchPoint = null;
update = moveTrackersIfNeeded(event);
break;
}
if (update) {
if(mListener != null){
if (mListener != null)
mListener.onColorChanged(Color.HSVToColor(mAlpha, new float[]{mHue, mSat, mVal}));
}
invalidate();
return true;
}
return super.onTouchEvent(event);
}
private boolean moveTrackersIfNeeded(MotionEvent event) {
if(mStartTouchPoint == null) return false;
if (mStartTouchPoint == null)
return false;
boolean update = false;
int startX = mStartTouchPoint.x;
int startY = mStartTouchPoint.y;
if (mHueRect.contains(startX, startY)) {
mLastTouchedPanel = PANEL_HUE;
mHue = pointToHue(event.getY());
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());
mSat = result[0];
mVal = result[1];
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());
update = true;
}
return update;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = 0;
int height = 0;
@ -665,30 +558,23 @@ public class ColorPickerView extends View {
heightAllowed = chooseHeight(heightMode, heightAllowed);
if (!mShowAlphaPanel) {
height = (int) (widthAllowed - PANEL_SPACING - HUE_PANEL_WIDTH);
//If calculated height (based on the width) is more than the allowed height.
if (height > heightAllowed || getTag().equals("landscape")) {
height = heightAllowed;
width = (int) (height + PANEL_SPACING + HUE_PANEL_WIDTH);
}
else{
} else {
width = widthAllowed;
}
}
else{
} else {
width = (int) (heightAllowed - ALPHA_PANEL_HEIGHT + HUE_PANEL_WIDTH);
if (width > widthAllowed) {
width = widthAllowed;
height = (int) (widthAllowed - HUE_PANEL_WIDTH + ALPHA_PANEL_HEIGHT);
}
else{
} else {
height = heightAllowed;
}
}
setMeasuredDimension(width, height);
@ -711,31 +597,19 @@ public class ColorPickerView extends View {
}
private int getPrefferedWidth() {
int width = getPrefferedHeight();
if(mShowAlphaPanel){
if (mShowAlphaPanel)
width -= (PANEL_SPACING + ALPHA_PANEL_HEIGHT);
}
return (int) (width + HUE_PANEL_WIDTH + PANEL_SPACING);
}
private int getPrefferedHeight() {
int height = (int)(200 * mDensity);
if(mShowAlphaPanel){
if (mShowAlphaPanel)
height += PANEL_SPACING + ALPHA_PANEL_HEIGHT;
}
return height;
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
@ -752,13 +626,11 @@ public class ColorPickerView extends View {
}
private void setUpSatValRect() {
final RectF dRect = mDrawingRect;
float panelSide = dRect.height() - BORDER_WIDTH_PX * 2;
if(mShowAlphaPanel){
if (mShowAlphaPanel)
panelSide -= PANEL_SPACING + ALPHA_PANEL_HEIGHT;
}
float left = dRect.left + BORDER_WIDTH_PX;
float top = dRect.top + BORDER_WIDTH_PX;
@ -773,15 +645,16 @@ public class ColorPickerView extends View {
float left = dRect.right - HUE_PANEL_WIDTH + BORDER_WIDTH_PX;
float top = dRect.top + BORDER_WIDTH_PX;
float bottom = dRect.bottom - BORDER_WIDTH_PX - (mShowAlphaPanel ? (PANEL_SPACING + ALPHA_PANEL_HEIGHT) : 0);
float bottom = dRect.bottom - BORDER_WIDTH_PX -
(mShowAlphaPanel ? (PANEL_SPACING + ALPHA_PANEL_HEIGHT) : 0);
float right = dRect.right - BORDER_WIDTH_PX;
mHueRect = new RectF(left, top, right, bottom);
}
private void setUpAlphaRect() {
if(!mShowAlphaPanel) return;
if (!mShowAlphaPanel)
return;
final RectF dRect = mDrawingRect;
@ -799,10 +672,8 @@ public class ColorPickerView extends View {
Math.round(mAlphaRect.right),
Math.round(mAlphaRect.bottom)
);
}
/**
* Set a OnColorChangedListener to get notified when the color
* selected by the user has changed.
@ -851,7 +722,6 @@ public class ColorPickerView extends View {
* your OnColorChangedListener.
*/
public void setColor(int color, boolean callback) {
int alpha = Color.alpha(color);
int red = Color.red(color);
int blue = Color.blue(color);
@ -866,9 +736,8 @@ public class ColorPickerView extends View {
mSat = hsv[1];
mVal = hsv[2];
if(callback && mListener != null){
if (callback && mListener != null)
mListener.onColorChanged(Color.HSVToColor(mAlpha, new float[]{mHue, mSat, mVal}));
}
invalidate();
}
@ -891,7 +760,6 @@ public class ColorPickerView extends View {
* @param visible
*/
public void setAlphaSliderVisible(boolean visible) {
if (mShowAlphaPanel != visible) {
mShowAlphaPanel = visible;
@ -907,14 +775,11 @@ public class ColorPickerView extends View {
requestLayout();
}
}
public void setSliderTrackerColor(int color) {
mSliderTrackerColor = color;
mHueTrackerPaint.setColor(mSliderTrackerColor);
invalidate();
}