You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
932 lines
20 KiB
932 lines
20 KiB
11 years ago
|
/*
|
||
|
* Copyright (C) 2010 Daniel Nilsson
|
||
|
*
|
||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
* you may not use this file except in compliance with the License.
|
||
|
* You may obtain a copy of the License at
|
||
|
*
|
||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||
|
*
|
||
|
* Unless required by applicable law or agreed to in writing, software
|
||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
* See the License for the specific language governing permissions and
|
||
|
* limitations under the License.
|
||
|
*/
|
||
|
|
||
|
package net.margaritov.preference.colorpicker;
|
||
|
|
||
|
import android.content.Context;
|
||
|
import android.graphics.Canvas;
|
||
|
import android.graphics.Color;
|
||
|
import android.graphics.ComposeShader;
|
||
|
import android.graphics.LinearGradient;
|
||
|
import android.graphics.Paint;
|
||
10 years ago
|
import android.graphics.Paint.Align;
|
||
|
import android.graphics.Paint.Style;
|
||
11 years ago
|
import android.graphics.Point;
|
||
|
import android.graphics.PorterDuff;
|
||
|
import android.graphics.RectF;
|
||
|
import android.graphics.Shader;
|
||
|
import android.graphics.Shader.TileMode;
|
||
|
import android.util.AttributeSet;
|
||
|
import android.view.MotionEvent;
|
||
|
import android.view.View;
|
||
|
|
||
|
/**
|
||
|
* Displays a color picker to the user and allow them
|
||
|
* to select a color. A slider for the alpha channel is
|
||
|
* also available. Enable it by setting
|
||
|
* setAlphaSliderVisible(boolean) to true.
|
||
|
* @author Daniel Nilsson
|
||
|
*/
|
||
|
public class ColorPickerView extends View {
|
||
|
|
||
10 years ago
|
private final static int PANEL_SAT_VAL = 0;
|
||
|
private final static int PANEL_HUE = 1;
|
||
|
private final static int PANEL_ALPHA = 2;
|
||
11 years ago
|
|
||
|
/**
|
||
|
* The width in pixels of the border
|
||
|
* surrounding all color panels.
|
||
|
*/
|
||
10 years ago
|
private final static float BORDER_WIDTH_PX = 1;
|
||
11 years ago
|
|
||
|
/**
|
||
|
* The width in dp of the hue panel.
|
||
|
*/
|
||
10 years ago
|
private float HUE_PANEL_WIDTH = 30f;
|
||
11 years ago
|
/**
|
||
|
* The height in dp of the alpha panel
|
||
|
*/
|
||
10 years ago
|
private float ALPHA_PANEL_HEIGHT = 20f;
|
||
11 years ago
|
/**
|
||
|
* The distance in dp between the different
|
||
|
* color panels.
|
||
|
*/
|
||
10 years ago
|
private float PANEL_SPACING = 10f;
|
||
11 years ago
|
/**
|
||
|
* The radius in dp of the color palette tracker circle.
|
||
|
*/
|
||
10 years ago
|
private float PALETTE_CIRCLE_TRACKER_RADIUS = 5f;
|
||
11 years ago
|
/**
|
||
|
* The dp which the tracker of the hue or alpha panel
|
||
|
* will extend outside of its bounds.
|
||
|
*/
|
||
10 years ago
|
private float RECTANGLE_TRACKER_OFFSET = 2f;
|
||
11 years ago
|
|
||
|
|
||
10 years ago
|
private float mDensity = 1f;
|
||
11 years ago
|
|
||
10 years ago
|
private OnColorChangedListener mListener;
|
||
11 years ago
|
|
||
10 years ago
|
private Paint mSatValPaint;
|
||
|
private Paint mSatValTrackerPaint;
|
||
11 years ago
|
|
||
10 years ago
|
private Paint mHuePaint;
|
||
|
private Paint mHueTrackerPaint;
|
||
11 years ago
|
|
||
10 years ago
|
private Paint mAlphaPaint;
|
||
|
private Paint mAlphaTextPaint;
|
||
11 years ago
|
|
||
10 years ago
|
private Paint mBorderPaint;
|
||
11 years ago
|
|
||
10 years ago
|
private Shader mValShader;
|
||
|
private Shader mSatShader;
|
||
|
private Shader mHueShader;
|
||
|
private Shader mAlphaShader;
|
||
11 years ago
|
|
||
10 years ago
|
private int mAlpha = 0xff;
|
||
|
private float mHue = 360f;
|
||
|
private float mSat = 0f;
|
||
|
private float mVal = 0f;
|
||
11 years ago
|
|
||
10 years ago
|
private String mAlphaSliderText = "";
|
||
|
private int mSliderTrackerColor = 0xff1c1c1c;
|
||
|
private int mBorderColor = 0xff6E6E6E;
|
||
|
private boolean mShowAlphaPanel = false;
|
||
11 years ago
|
|
||
|
/*
|
||
|
* To remember which panel that has the "focus" when
|
||
|
* processing hardware button data.
|
||
|
*/
|
||
10 years ago
|
private int mLastTouchedPanel = PANEL_SAT_VAL;
|
||
11 years ago
|
|
||
|
/**
|
||
|
* Offset from the edge we must have or else
|
||
|
* the finger tracker will get clipped when
|
||
|
* it is drawn outside of the view.
|
||
|
*/
|
||
10 years ago
|
private float mDrawingOffset;
|
||
11 years ago
|
|
||
|
|
||
|
/*
|
||
|
* Distance form the edges of the view
|
||
|
* of where we are allowed to draw.
|
||
|
*/
|
||
10 years ago
|
private RectF mDrawingRect;
|
||
11 years ago
|
|
||
10 years ago
|
private RectF mSatValRect;
|
||
|
private RectF mHueRect;
|
||
|
private RectF mAlphaRect;
|
||
11 years ago
|
|
||
10 years ago
|
private AlphaPatternDrawable mAlphaPattern;
|
||
11 years ago
|
|
||
10 years ago
|
private Point mStartTouchPoint = null;
|
||
11 years ago
|
|
||
|
public interface OnColorChangedListener {
|
||
|
public void onColorChanged(int color);
|
||
|
}
|
||
|
|
||
10 years ago
|
public ColorPickerView(Context context) {
|
||
11 years ago
|
this(context, null);
|
||
|
}
|
||
|
|
||
|
public ColorPickerView(Context context, AttributeSet attrs) {
|
||
|
this(context, attrs, 0);
|
||
|
}
|
||
|
|
||
|
public ColorPickerView(Context context, AttributeSet attrs, int defStyle) {
|
||
|
super(context, attrs, defStyle);
|
||
|
init();
|
||
|
}
|
||
|
|
||
10 years ago
|
private void init() {
|
||
11 years ago
|
mDensity = getContext().getResources().getDisplayMetrics().density;
|
||
|
PALETTE_CIRCLE_TRACKER_RADIUS *= mDensity;
|
||
|
RECTANGLE_TRACKER_OFFSET *= mDensity;
|
||
|
HUE_PANEL_WIDTH *= mDensity;
|
||
|
ALPHA_PANEL_HEIGHT *= mDensity;
|
||
|
PANEL_SPACING = PANEL_SPACING * mDensity;
|
||
|
|
||
|
mDrawingOffset = calculateRequiredOffset();
|
||
|
|
||
|
initPaintTools();
|
||
|
|
||
|
//Needed for receiving trackball motion events.
|
||
|
setFocusable(true);
|
||
|
setFocusableInTouchMode(true);
|
||
|
}
|
||
|
|
||
10 years ago
|
private void initPaintTools() {
|
||
11 years ago
|
|
||
|
mSatValPaint = new Paint();
|
||
|
mSatValTrackerPaint = new Paint();
|
||
|
mHuePaint = new Paint();
|
||
|
mHueTrackerPaint = new Paint();
|
||
|
mAlphaPaint = new Paint();
|
||
|
mAlphaTextPaint = new Paint();
|
||
|
mBorderPaint = new Paint();
|
||
|
|
||
|
|
||
|
mSatValTrackerPaint.setStyle(Style.STROKE);
|
||
|
mSatValTrackerPaint.setStrokeWidth(2f * mDensity);
|
||
|
mSatValTrackerPaint.setAntiAlias(true);
|
||
|
|
||
|
mHueTrackerPaint.setColor(mSliderTrackerColor);
|
||
|
mHueTrackerPaint.setStyle(Style.STROKE);
|
||
|
mHueTrackerPaint.setStrokeWidth(2f * mDensity);
|
||
|
mHueTrackerPaint.setAntiAlias(true);
|
||
|
|
||
|
mAlphaTextPaint.setColor(0xff1c1c1c);
|
||
|
mAlphaTextPaint.setTextSize(14f * mDensity);
|
||
|
mAlphaTextPaint.setAntiAlias(true);
|
||
|
mAlphaTextPaint.setTextAlign(Align.CENTER);
|
||
|
mAlphaTextPaint.setFakeBoldText(true);
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
10 years ago
|
private float calculateRequiredOffset() {
|
||
11 years ago
|
float offset = Math.max(PALETTE_CIRCLE_TRACKER_RADIUS, RECTANGLE_TRACKER_OFFSET);
|
||
|
offset = Math.max(offset, BORDER_WIDTH_PX * mDensity);
|
||
|
|
||
|
return offset * 1.5f;
|
||
|
}
|
||
|
|
||
10 years ago
|
private int[] buildHueColorArray() {
|
||
11 years ago
|
|
||
|
int[] hue = new int[361];
|
||
|
|
||
|
int count = 0;
|
||
10 years ago
|
for (int i = hue.length - 1; i >= 0; i--, count++) {
|
||
11 years ago
|
hue[count] = Color.HSVToColor(new float[]{i, 1f, 1f});
|
||
|
}
|
||
|
|
||
|
return hue;
|
||
|
}
|
||
|
|
||
|
|
||
|
@Override
|
||
|
protected void onDraw(Canvas canvas) {
|
||
|
|
||
10 years ago
|
if (mDrawingRect.width() <= 0 || mDrawingRect.height() <= 0) {
|
||
|
return;
|
||
|
}
|
||
11 years ago
|
|
||
|
drawSatValPanel(canvas);
|
||
|
drawHuePanel(canvas);
|
||
|
drawAlphaPanel(canvas);
|
||
|
|
||
|
}
|
||
|
|
||
10 years ago
|
private void drawSatValPanel(Canvas canvas) {
|
||
11 years ago
|
|
||
10 years ago
|
final RectF rect = mSatValRect;
|
||
11 years ago
|
|
||
10 years ago
|
if (BORDER_WIDTH_PX > 0) {
|
||
11 years ago
|
mBorderPaint.setColor(mBorderColor);
|
||
10 years ago
|
canvas.drawRect(mDrawingRect.left, mDrawingRect.top, rect.right + BORDER_WIDTH_PX,
|
||
|
rect.bottom + BORDER_WIDTH_PX, mBorderPaint);
|
||
11 years ago
|
}
|
||
|
|
||
|
if (mValShader == null) {
|
||
10 years ago
|
mValShader = new LinearGradient(rect.left, rect.top, rect.left, rect.bottom, 0xffffffff, 0xff000000,
|
||
|
TileMode.CLAMP);
|
||
11 years ago
|
}
|
||
|
|
||
10 years ago
|
int rgb = Color.HSVToColor(new float[]{mHue, 1f, 1f});
|
||
11 years ago
|
|
||
10 years ago
|
mSatShader = new LinearGradient(rect.left, rect.top, rect.right, rect.top, 0xffffffff, rgb, TileMode.CLAMP);
|
||
11 years ago
|
ComposeShader mShader = new ComposeShader(mValShader, mSatShader, PorterDuff.Mode.MULTIPLY);
|
||
|
mSatValPaint.setShader(mShader);
|
||
|
|
||
|
canvas.drawRect(rect, mSatValPaint);
|
||
|
|
||
|
Point p = satValToPoint(mSat, mVal);
|
||
|
|
||
|
mSatValTrackerPaint.setColor(0xff000000);
|
||
|
canvas.drawCircle(p.x, p.y, PALETTE_CIRCLE_TRACKER_RADIUS - 1f * mDensity, mSatValTrackerPaint);
|
||
|
|
||
|
mSatValTrackerPaint.setColor(0xffdddddd);
|
||
|
canvas.drawCircle(p.x, p.y, PALETTE_CIRCLE_TRACKER_RADIUS, mSatValTrackerPaint);
|
||
|
|
||
|
}
|
||
|
|
||
10 years ago
|
private void drawHuePanel(Canvas canvas) {
|
||
11 years ago
|
|
||
|
final RectF rect = mHueRect;
|
||
|
|
||
10 years ago
|
if (BORDER_WIDTH_PX > 0) {
|
||
11 years ago
|
mBorderPaint.setColor(mBorderColor);
|
||
10 years ago
|
canvas.drawRect(rect.left - BORDER_WIDTH_PX, rect.top - BORDER_WIDTH_PX, rect.right + BORDER_WIDTH_PX,
|
||
|
rect.bottom + BORDER_WIDTH_PX, mBorderPaint);
|
||
11 years ago
|
}
|
||
|
|
||
|
if (mHueShader == null) {
|
||
10 years ago
|
mHueShader = new LinearGradient(rect.left, rect.top, rect.left, rect.bottom, buildHueColorArray(), null,
|
||
|
TileMode.CLAMP);
|
||
11 years ago
|
mHuePaint.setShader(mHueShader);
|
||
|
}
|
||
|
|
||
|
canvas.drawRect(rect, mHuePaint);
|
||
|
|
||
|
float rectHeight = 4 * mDensity / 2;
|
||
|
|
||
|
Point p = hueToPoint(mHue);
|
||
|
|
||
|
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, mHueTrackerPaint);
|
||
|
|
||
|
}
|
||
|
|
||
10 years ago
|
private void drawAlphaPanel(Canvas canvas) {
|
||
11 years ago
|
|
||
10 years ago
|
if (!mShowAlphaPanel || mAlphaRect == null || mAlphaPattern == null) {
|
||
|
return;
|
||
|
}
|
||
11 years ago
|
|
||
|
final RectF rect = mAlphaRect;
|
||
|
|
||
10 years ago
|
if (BORDER_WIDTH_PX > 0) {
|
||
11 years ago
|
mBorderPaint.setColor(mBorderColor);
|
||
10 years ago
|
canvas.drawRect(rect.left - BORDER_WIDTH_PX, rect.top - BORDER_WIDTH_PX, rect.right + BORDER_WIDTH_PX,
|
||
|
rect.bottom + BORDER_WIDTH_PX, mBorderPaint);
|
||
11 years ago
|
}
|
||
|
|
||
|
|
||
|
mAlphaPattern.draw(canvas);
|
||
|
|
||
10 years ago
|
float[] hsv = new float[]{mHue, mSat, mVal};
|
||
11 years ago
|
int color = Color.HSVToColor(hsv);
|
||
|
int acolor = Color.HSVToColor(0, hsv);
|
||
|
|
||
10 years ago
|
mAlphaShader = new LinearGradient(rect.left, rect.top, rect.right, rect.top, color, acolor, TileMode.CLAMP);
|
||
11 years ago
|
|
||
|
|
||
|
mAlphaPaint.setShader(mAlphaShader);
|
||
|
|
||
|
canvas.drawRect(rect, mAlphaPaint);
|
||
|
|
||
10 years ago
|
if (mAlphaSliderText != null && mAlphaSliderText != "") {
|
||
11 years ago
|
canvas.drawText(mAlphaSliderText, rect.centerX(), rect.centerY() + 4 * mDensity, mAlphaTextPaint);
|
||
|
}
|
||
|
|
||
|
float rectWidth = 4 * mDensity / 2;
|
||
|
|
||
|
Point p = alphaToPoint(mAlpha);
|
||
|
|
||
|
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, mHueTrackerPaint);
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
10 years ago
|
private Point hueToPoint(float hue) {
|
||
11 years ago
|
|
||
|
final RectF rect = mHueRect;
|
||
|
final float height = rect.height();
|
||
|
|
||
|
Point p = new Point();
|
||
|
|
||
|
p.y = (int) (height - (hue * height / 360f) + rect.top);
|
||
|
p.x = (int) rect.left;
|
||
|
|
||
|
return p;
|
||
|
}
|
||
|
|
||
10 years ago
|
private Point satValToPoint(float sat, float val) {
|
||
11 years ago
|
|
||
|
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);
|
||
|
|
||
|
return p;
|
||
|
}
|
||
|
|
||
10 years ago
|
private Point alphaToPoint(int alpha) {
|
||
11 years ago
|
|
||
|
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;
|
||
|
|
||
|
}
|
||
|
|
||
10 years ago
|
private float[] pointToSatVal(float x, float y) {
|
||
11 years ago
|
|
||
|
final RectF rect = mSatValRect;
|
||
|
float[] result = new float[2];
|
||
|
|
||
|
float width = rect.width();
|
||
|
float height = rect.height();
|
||
|
|
||
10 years ago
|
if (x < rect.left) {
|
||
11 years ago
|
x = 0f;
|
||
10 years ago
|
} else if (x > rect.right) {
|
||
11 years ago
|
x = width;
|
||
10 years ago
|
} else {
|
||
11 years ago
|
x = x - rect.left;
|
||
|
}
|
||
|
|
||
10 years ago
|
if (y < rect.top) {
|
||
11 years ago
|
y = 0f;
|
||
10 years ago
|
} else if (y > rect.bottom) {
|
||
11 years ago
|
y = height;
|
||
10 years ago
|
} else {
|
||
11 years ago
|
y = y - rect.top;
|
||
|
}
|
||
|
|
||
|
|
||
|
result[0] = 1.f / width * x;
|
||
|
result[1] = 1.f - (1.f / height * y);
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
|
||
10 years ago
|
private float pointToHue(float y) {
|
||
11 years ago
|
|
||
|
final RectF rect = mHueRect;
|
||
|
|
||
|
float height = rect.height();
|
||
|
|
||
10 years ago
|
if (y < rect.top) {
|
||
11 years ago
|
y = 0f;
|
||
10 years ago
|
} else if (y > rect.bottom) {
|
||
11 years ago
|
y = height;
|
||
10 years ago
|
} else {
|
||
11 years ago
|
y = y - rect.top;
|
||
|
}
|
||
|
|
||
|
return 360f - (y * 360f / height);
|
||
|
}
|
||
|
|
||
10 years ago
|
private int pointToAlpha(int x) {
|
||
11 years ago
|
|
||
|
final RectF rect = mAlphaRect;
|
||
|
final int width = (int) rect.width();
|
||
|
|
||
10 years ago
|
if (x < rect.left) {
|
||
11 years ago
|
x = 0;
|
||
10 years ago
|
} else if (x > rect.right) {
|
||
11 years ago
|
x = width;
|
||
10 years ago
|
} else {
|
||
|
x = x - (int) rect.left;
|
||
11 years ago
|
}
|
||
|
|
||
|
return 0xff - (x * 0xff / width);
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
@Override
|
||
|
public boolean onTrackballEvent(MotionEvent event) {
|
||
|
|
||
|
float x = event.getX();
|
||
|
float y = event.getY();
|
||
|
|
||
|
boolean update = false;
|
||
|
|
||
|
|
||
10 years ago
|
if (event.getAction() == MotionEvent.ACTION_MOVE) {
|
||
11 years ago
|
|
||
10 years ago
|
switch (mLastTouchedPanel) {
|
||
11 years ago
|
|
||
10 years ago
|
case PANEL_SAT_VAL:
|
||
11 years ago
|
|
||
10 years ago
|
float sat, val;
|
||
11 years ago
|
|
||
10 years ago
|
sat = mSat + x / 50f;
|
||
|
val = mVal - y / 50f;
|
||
11 years ago
|
|
||
10 years ago
|
if (sat < 0f) {
|
||
|
sat = 0f;
|
||
|
} else if (sat > 1f) {
|
||
|
sat = 1f;
|
||
|
}
|
||
11 years ago
|
|
||
10 years ago
|
if (val < 0f) {
|
||
|
val = 0f;
|
||
|
} else if (val > 1f) {
|
||
|
val = 1f;
|
||
|
}
|
||
11 years ago
|
|
||
10 years ago
|
mSat = sat;
|
||
|
mVal = val;
|
||
11 years ago
|
|
||
10 years ago
|
update = true;
|
||
11 years ago
|
|
||
10 years ago
|
break;
|
||
11 years ago
|
|
||
10 years ago
|
case PANEL_HUE:
|
||
11 years ago
|
|
||
10 years ago
|
float hue = mHue - y * 10f;
|
||
11 years ago
|
|
||
10 years ago
|
if (hue < 0f) {
|
||
|
hue = 0f;
|
||
|
} else if (hue > 360f) {
|
||
|
hue = 360f;
|
||
|
}
|
||
11 years ago
|
|
||
10 years ago
|
mHue = hue;
|
||
11 years ago
|
|
||
10 years ago
|
update = true;
|
||
11 years ago
|
|
||
10 years ago
|
break;
|
||
11 years ago
|
|
||
10 years ago
|
case PANEL_ALPHA:
|
||
11 years ago
|
|
||
10 years ago
|
if (!mShowAlphaPanel || mAlphaRect == null) {
|
||
|
update = false;
|
||
|
} else {
|
||
11 years ago
|
|
||
10 years ago
|
int alpha = (int) (mAlpha - x * 10);
|
||
11 years ago
|
|
||
10 years ago
|
if (alpha < 0) {
|
||
|
alpha = 0;
|
||
|
} else if (alpha > 0xff) {
|
||
|
alpha = 0xff;
|
||
|
}
|
||
11 years ago
|
|
||
10 years ago
|
mAlpha = alpha;
|
||
11 years ago
|
|
||
|
|
||
10 years ago
|
update = true;
|
||
|
}
|
||
11 years ago
|
|
||
10 years ago
|
break;
|
||
11 years ago
|
}
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
10 years ago
|
if (update) {
|
||
11 years ago
|
|
||
10 years ago
|
if (mListener != null) {
|
||
11 years ago
|
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;
|
||
|
|
||
10 years ago
|
switch (event.getAction()) {
|
||
11 years ago
|
|
||
10 years ago
|
case MotionEvent.ACTION_DOWN:
|
||
11 years ago
|
|
||
10 years ago
|
mStartTouchPoint = new Point((int) event.getX(), (int) event.getY());
|
||
11 years ago
|
|
||
10 years ago
|
update = moveTrackersIfNeeded(event);
|
||
11 years ago
|
|
||
10 years ago
|
break;
|
||
11 years ago
|
|
||
10 years ago
|
case MotionEvent.ACTION_MOVE:
|
||
11 years ago
|
|
||
10 years ago
|
update = moveTrackersIfNeeded(event);
|
||
11 years ago
|
|
||
10 years ago
|
break;
|
||
11 years ago
|
|
||
10 years ago
|
case MotionEvent.ACTION_UP:
|
||
11 years ago
|
|
||
10 years ago
|
mStartTouchPoint = null;
|
||
11 years ago
|
|
||
10 years ago
|
update = moveTrackersIfNeeded(event);
|
||
11 years ago
|
|
||
10 years ago
|
break;
|
||
11 years ago
|
|
||
|
}
|
||
|
|
||
10 years ago
|
if (update) {
|
||
11 years ago
|
|
||
10 years ago
|
if (mListener != null) {
|
||
11 years ago
|
mListener.onColorChanged(Color.HSVToColor(mAlpha, new float[]{mHue, mSat, mVal}));
|
||
|
}
|
||
|
|
||
|
invalidate();
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
|
||
|
return super.onTouchEvent(event);
|
||
|
}
|
||
|
|
||
10 years ago
|
private boolean moveTrackersIfNeeded(MotionEvent event) {
|
||
11 years ago
|
|
||
10 years ago
|
if (mStartTouchPoint == null) {
|
||
|
return false;
|
||
|
}
|
||
11 years ago
|
|
||
|
boolean update = false;
|
||
|
|
||
|
int startX = mStartTouchPoint.x;
|
||
|
int startY = mStartTouchPoint.y;
|
||
|
|
||
|
|
||
10 years ago
|
if (mHueRect.contains(startX, startY)) {
|
||
11 years ago
|
mLastTouchedPanel = PANEL_HUE;
|
||
|
|
||
|
mHue = pointToHue(event.getY());
|
||
|
|
||
|
update = true;
|
||
10 years ago
|
} else if (mSatValRect.contains(startX, startY)) {
|
||
11 years ago
|
|
||
|
mLastTouchedPanel = PANEL_SAT_VAL;
|
||
|
|
||
|
float[] result = pointToSatVal(event.getX(), event.getY());
|
||
|
|
||
|
mSat = result[0];
|
||
|
mVal = result[1];
|
||
|
|
||
|
update = true;
|
||
10 years ago
|
} else if (mAlphaRect != null && mAlphaRect.contains(startX, startY)) {
|
||
11 years ago
|
|
||
|
mLastTouchedPanel = PANEL_ALPHA;
|
||
|
|
||
10 years ago
|
mAlpha = pointToAlpha((int) event.getX());
|
||
11 years ago
|
|
||
|
update = true;
|
||
|
}
|
||
|
|
||
|
|
||
|
return update;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
||
|
|
||
|
int width = 0;
|
||
|
int height = 0;
|
||
10 years ago
|
|
||
11 years ago
|
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
|
||
|
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
|
||
10 years ago
|
|
||
11 years ago
|
int widthAllowed = MeasureSpec.getSize(widthMeasureSpec);
|
||
|
int heightAllowed = MeasureSpec.getSize(heightMeasureSpec);
|
||
10 years ago
|
|
||
11 years ago
|
widthAllowed = chooseWidth(widthMode, widthAllowed);
|
||
|
heightAllowed = chooseHeight(heightMode, heightAllowed);
|
||
10 years ago
|
|
||
|
if (!mShowAlphaPanel) {
|
||
|
|
||
11 years ago
|
height = (int) (widthAllowed - PANEL_SPACING - HUE_PANEL_WIDTH);
|
||
|
|
||
|
//If calculated height (based on the width) is more than the allowed height.
|
||
10 years ago
|
if (height > heightAllowed || getTag().equals("landscape")) {
|
||
11 years ago
|
height = heightAllowed;
|
||
|
width = (int) (height + PANEL_SPACING + HUE_PANEL_WIDTH);
|
||
10 years ago
|
} else {
|
||
11 years ago
|
width = widthAllowed;
|
||
|
}
|
||
10 years ago
|
} else {
|
||
11 years ago
|
|
||
|
width = (int) (heightAllowed - ALPHA_PANEL_HEIGHT + HUE_PANEL_WIDTH);
|
||
|
|
||
10 years ago
|
if (width > widthAllowed) {
|
||
11 years ago
|
width = widthAllowed;
|
||
|
height = (int) (widthAllowed - HUE_PANEL_WIDTH + ALPHA_PANEL_HEIGHT);
|
||
10 years ago
|
} else {
|
||
11 years ago
|
height = heightAllowed;
|
||
|
}
|
||
|
|
||
|
}
|
||
10 years ago
|
|
||
11 years ago
|
setMeasuredDimension(width, height);
|
||
|
}
|
||
|
|
||
10 years ago
|
private int chooseWidth(int mode, int size) {
|
||
11 years ago
|
if (mode == MeasureSpec.AT_MOST || mode == MeasureSpec.EXACTLY) {
|
||
|
return size;
|
||
|
} else { // (mode == MeasureSpec.UNSPECIFIED)
|
||
|
return getPrefferedWidth();
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
private int chooseHeight(int mode, int size) {
|
||
11 years ago
|
if (mode == MeasureSpec.AT_MOST || mode == MeasureSpec.EXACTLY) {
|
||
|
return size;
|
||
|
} else { // (mode == MeasureSpec.UNSPECIFIED)
|
||
|
return getPrefferedHeight();
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
private int getPrefferedWidth() {
|
||
11 years ago
|
|
||
|
int width = getPrefferedHeight();
|
||
|
|
||
10 years ago
|
if (mShowAlphaPanel) {
|
||
11 years ago
|
width -= (PANEL_SPACING + ALPHA_PANEL_HEIGHT);
|
||
|
}
|
||
|
|
||
|
|
||
|
return (int) (width + HUE_PANEL_WIDTH + PANEL_SPACING);
|
||
|
|
||
|
}
|
||
|
|
||
10 years ago
|
private int getPrefferedHeight() {
|
||
11 years ago
|
|
||
10 years ago
|
int height = (int) (200 * mDensity);
|
||
11 years ago
|
|
||
10 years ago
|
if (mShowAlphaPanel) {
|
||
11 years ago
|
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);
|
||
|
|
||
|
mDrawingRect = new RectF();
|
||
|
mDrawingRect.left = mDrawingOffset + getPaddingLeft();
|
||
10 years ago
|
mDrawingRect.right = w - mDrawingOffset - getPaddingRight();
|
||
11 years ago
|
mDrawingRect.top = mDrawingOffset + getPaddingTop();
|
||
|
mDrawingRect.bottom = h - mDrawingOffset - getPaddingBottom();
|
||
|
|
||
|
setUpSatValRect();
|
||
|
setUpHueRect();
|
||
|
setUpAlphaRect();
|
||
|
}
|
||
|
|
||
10 years ago
|
private void setUpSatValRect() {
|
||
11 years ago
|
|
||
10 years ago
|
final RectF dRect = mDrawingRect;
|
||
11 years ago
|
float panelSide = dRect.height() - BORDER_WIDTH_PX * 2;
|
||
|
|
||
10 years ago
|
if (mShowAlphaPanel) {
|
||
11 years ago
|
panelSide -= PANEL_SPACING + ALPHA_PANEL_HEIGHT;
|
||
|
}
|
||
|
|
||
|
float left = dRect.left + BORDER_WIDTH_PX;
|
||
|
float top = dRect.top + BORDER_WIDTH_PX;
|
||
|
float bottom = top + panelSide;
|
||
|
float right = left + panelSide;
|
||
|
|
||
10 years ago
|
mSatValRect = new RectF(left, top, right, bottom);
|
||
11 years ago
|
}
|
||
|
|
||
10 years ago
|
private void setUpHueRect() {
|
||
|
final RectF dRect = mDrawingRect;
|
||
11 years ago
|
|
||
|
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 right = dRect.right - BORDER_WIDTH_PX;
|
||
|
|
||
|
mHueRect = new RectF(left, top, right, bottom);
|
||
|
}
|
||
|
|
||
|
private void setUpAlphaRect() {
|
||
|
|
||
10 years ago
|
if (!mShowAlphaPanel) {
|
||
|
return;
|
||
|
}
|
||
11 years ago
|
|
||
10 years ago
|
final RectF dRect = mDrawingRect;
|
||
11 years ago
|
|
||
|
float left = dRect.left + BORDER_WIDTH_PX;
|
||
|
float top = dRect.bottom - ALPHA_PANEL_HEIGHT + BORDER_WIDTH_PX;
|
||
|
float bottom = dRect.bottom - BORDER_WIDTH_PX;
|
||
|
float right = dRect.right - BORDER_WIDTH_PX;
|
||
|
|
||
|
mAlphaRect = new RectF(left, top, right, bottom);
|
||
|
|
||
|
mAlphaPattern = new AlphaPatternDrawable((int) (5 * mDensity));
|
||
10 years ago
|
mAlphaPattern.setBounds(Math.round(mAlphaRect.left), Math.round(mAlphaRect.top), Math.round(mAlphaRect.right),
|
||
|
Math.round(mAlphaRect.bottom));
|
||
11 years ago
|
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Set a OnColorChangedListener to get notified when the color
|
||
|
* selected by the user has changed.
|
||
|
* @param listener
|
||
|
*/
|
||
10 years ago
|
public void setOnColorChangedListener(OnColorChangedListener listener) {
|
||
11 years ago
|
mListener = listener;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Set the color of the border surrounding all panels.
|
||
|
* @param color
|
||
|
*/
|
||
10 years ago
|
public void setBorderColor(int color) {
|
||
11 years ago
|
mBorderColor = color;
|
||
|
invalidate();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Get the color of the border surrounding all panels.
|
||
|
*/
|
||
10 years ago
|
public int getBorderColor() {
|
||
11 years ago
|
return mBorderColor;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Get the current color this view is showing.
|
||
|
* @return the current color.
|
||
|
*/
|
||
10 years ago
|
public int getColor() {
|
||
|
return Color.HSVToColor(mAlpha, new float[]{mHue, mSat, mVal});
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Set the color the view should show.
|
||
|
* @param color The color that should be selected.
|
||
|
*/
|
||
10 years ago
|
public void setColor(int color) {
|
||
11 years ago
|
setColor(color, false);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Set the color this view should show.
|
||
|
* @param color The color that should be selected.
|
||
|
* @param callback If you want to get a callback to
|
||
|
* your OnColorChangedListener.
|
||
|
*/
|
||
10 years ago
|
public void setColor(int color, boolean callback) {
|
||
11 years ago
|
|
||
|
int alpha = Color.alpha(color);
|
||
|
int red = Color.red(color);
|
||
|
int blue = Color.blue(color);
|
||
|
int green = Color.green(color);
|
||
|
|
||
|
float[] hsv = new float[3];
|
||
|
|
||
|
Color.RGBToHSV(red, green, blue, hsv);
|
||
|
|
||
|
mAlpha = alpha;
|
||
|
mHue = hsv[0];
|
||
|
mSat = hsv[1];
|
||
|
mVal = hsv[2];
|
||
|
|
||
10 years ago
|
if (callback && mListener != null) {
|
||
11 years ago
|
mListener.onColorChanged(Color.HSVToColor(mAlpha, new float[]{mHue, mSat, mVal}));
|
||
|
}
|
||
|
|
||
|
invalidate();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Get the drawing offset of the color picker view.
|
||
|
* The drawing offset is the distance from the side of
|
||
|
* a panel to the side of the view minus the padding.
|
||
|
* Useful if you want to have your own panel below showing
|
||
|
* the currently selected color and want to align it perfectly.
|
||
|
* @return The offset in pixels.
|
||
|
*/
|
||
10 years ago
|
public float getDrawingOffset() {
|
||
11 years ago
|
return mDrawingOffset;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Set if the user is allowed to adjust the alpha panel. Default is false.
|
||
|
* If it is set to false no alpha will be set.
|
||
|
* @param visible
|
||
|
*/
|
||
10 years ago
|
public void setAlphaSliderVisible(boolean visible) {
|
||
11 years ago
|
|
||
10 years ago
|
if (mShowAlphaPanel != visible) {
|
||
11 years ago
|
mShowAlphaPanel = visible;
|
||
|
|
||
|
/*
|
||
|
* Reset all shader to force a recreation.
|
||
|
* Otherwise they will not look right after
|
||
|
* the size of the view has changed.
|
||
|
*/
|
||
|
mValShader = null;
|
||
|
mSatShader = null;
|
||
|
mHueShader = null;
|
||
10 years ago
|
mAlphaShader = null;
|
||
|
;
|
||
11 years ago
|
|
||
|
requestLayout();
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
10 years ago
|
public void setSliderTrackerColor(int color) {
|
||
11 years ago
|
mSliderTrackerColor = color;
|
||
|
|
||
|
mHueTrackerPaint.setColor(mSliderTrackerColor);
|
||
|
|
||
|
invalidate();
|
||
|
}
|
||
|
|
||
10 years ago
|
public int getSliderTrackerColor() {
|
||
11 years ago
|
return mSliderTrackerColor;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Set the text that should be shown in the
|
||
|
* alpha slider. Set to null to disable text.
|
||
|
* @param res string resource id.
|
||
|
*/
|
||
10 years ago
|
public void setAlphaSliderText(int res) {
|
||
11 years ago
|
String text = getContext().getString(res);
|
||
|
setAlphaSliderText(text);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Set the text that should be shown in the
|
||
|
* alpha slider. Set to null to disable text.
|
||
|
* @param text Text that should be shown.
|
||
|
*/
|
||
10 years ago
|
public void setAlphaSliderText(String text) {
|
||
11 years ago
|
mAlphaSliderText = text;
|
||
|
invalidate();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Get the current value of the text
|
||
|
* that will be shown in the alpha
|
||
|
* slider.
|
||
|
* @return
|
||
|
*/
|
||
10 years ago
|
public String getAlphaSliderText() {
|
||
11 years ago
|
return mAlphaSliderText;
|
||
|
}
|
||
|
}
|