mirror of
https://github.com/peterosterlund2/droidfish.git
synced 2025-02-18 18:17:49 +01:00
White space changes and warning fixes in color picker code
This commit is contained in:
parent
421aeb8a30
commit
8789e8f9c0
|
@ -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){
|
||||
private void generatePatternBitmap() {
|
||||
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;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
@ -107,7 +103,7 @@ public class ColorPickerDialog
|
|||
* selected by the user has changed.
|
||||
* @param listener
|
||||
*/
|
||||
public void setOnColorChangedListener(OnColorChangedListener listener){
|
||||
public void setOnColorChangedListener(OnColorChangedListener listener) {
|
||||
mListener = listener;
|
||||
}
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
||||
|
@ -52,11 +51,11 @@ public class ColorPickerPanelView extends View {
|
|||
private AlphaPatternDrawable mAlphaPattern;
|
||||
|
||||
|
||||
public ColorPickerPanelView(Context context){
|
||||
public ColorPickerPanelView(Context context) {
|
||||
this(context, null);
|
||||
}
|
||||
|
||||
public ColorPickerPanelView(Context context, AttributeSet attrs){
|
||||
public ColorPickerPanelView(Context context, AttributeSet attrs) {
|
||||
this(context, attrs, 0);
|
||||
}
|
||||
|
||||
|
@ -65,24 +64,22 @@ public class ColorPickerPanelView extends View {
|
|||
init();
|
||||
}
|
||||
|
||||
private void init(){
|
||||
private void init() {
|
||||
mBorderPaint = new Paint();
|
||||
mColorPaint = new Paint();
|
||||
mDensity = getContext().getResources().getDisplayMetrics().density;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void onDraw(Canvas canvas) {
|
||||
|
||||
final RectF rect = mColorRect;
|
||||
|
||||
if(BORDER_WIDTH_PX > 0){
|
||||
if (BORDER_WIDTH_PX > 0) {
|
||||
mBorderPaint.setColor(mBorderColor);
|
||||
canvas.drawRect(mDrawingRect, mBorderPaint);
|
||||
}
|
||||
|
||||
if(mAlphaPattern != null){
|
||||
if (mAlphaPattern != null) {
|
||||
mAlphaPattern.draw(canvas);
|
||||
}
|
||||
|
||||
|
@ -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,10 +107,9 @@ public class ColorPickerPanelView extends View {
|
|||
mDrawingRect.bottom = h - getPaddingBottom();
|
||||
|
||||
setUpColorRect();
|
||||
|
||||
}
|
||||
|
||||
private void setUpColorRect(){
|
||||
private void setUpColorRect() {
|
||||
final RectF dRect = mDrawingRect;
|
||||
|
||||
float left = dRect.left + BORDER_WIDTH_PX;
|
||||
|
@ -132,14 +127,13 @@ public class ColorPickerPanelView extends View {
|
|||
Math.round(mColorRect.right),
|
||||
Math.round(mColorRect.bottom)
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the color that should be shown by this view.
|
||||
* @param color
|
||||
*/
|
||||
public void setColor(int color){
|
||||
public void setColor(int color) {
|
||||
mColor = color;
|
||||
invalidate();
|
||||
}
|
||||
|
@ -148,7 +142,7 @@ public class ColorPickerPanelView extends View {
|
|||
* Get the color currently show by this view.
|
||||
* @return
|
||||
*/
|
||||
public int getColor(){
|
||||
public int getColor() {
|
||||
return mColor;
|
||||
}
|
||||
|
||||
|
@ -156,7 +150,7 @@ public class ColorPickerPanelView extends View {
|
|||
* Set the color of the border surrounding the panel.
|
||||
* @param color
|
||||
*/
|
||||
public void setBorderColor(int color){
|
||||
public void setBorderColor(int color) {
|
||||
mBorderColor = color;
|
||||
invalidate();
|
||||
}
|
||||
|
@ -164,8 +158,7 @@ public class ColorPickerPanelView extends View {
|
|||
/**
|
||||
* Get the color of the border surrounding the panel.
|
||||
*/
|
||||
public int getBorderColor(){
|
||||
public int getBorderColor() {
|
||||
return mBorderColor;
|
||||
}
|
||||
|
||||
}
|
|
@ -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("#", "");
|
||||
}
|
||||
|
|
|
@ -140,7 +140,7 @@ public class ColorPickerView extends View {
|
|||
public void onColorChanged(int color);
|
||||
}
|
||||
|
||||
public ColorPickerView(Context context){
|
||||
public ColorPickerView(Context context) {
|
||||
this(context, null);
|
||||
}
|
||||
|
||||
|
@ -153,7 +153,7 @@ public class ColorPickerView extends View {
|
|||
init();
|
||||
}
|
||||
|
||||
private void init(){
|
||||
private void init() {
|
||||
mDensity = getContext().getResources().getDisplayMetrics().density;
|
||||
PALETTE_CIRCLE_TRACKER_RADIUS *= mDensity;
|
||||
RECTANGLE_TRACKER_OFFSET *= mDensity;
|
||||
|
@ -170,8 +170,7 @@ public class ColorPickerView extends View {
|
|||
setFocusableInTouchMode(true);
|
||||
}
|
||||
|
||||
private void initPaintTools(){
|
||||
|
||||
private void initPaintTools() {
|
||||
mSatValPaint = new Paint();
|
||||
mSatValTrackerPaint = new Paint();
|
||||
mHuePaint = new Paint();
|
||||
|
@ -199,50 +198,46 @@ public class ColorPickerView extends View {
|
|||
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
|
||||
}
|
||||
|
||||
private float calculateRequiredOffset(){
|
||||
private float calculateRequiredOffset() {
|
||||
float offset = Math.max(PALETTE_CIRCLE_TRACKER_RADIUS, RECTANGLE_TRACKER_OFFSET);
|
||||
offset = Math.max(offset, BORDER_WIDTH_PX * mDensity);
|
||||
|
||||
return offset * 1.5f;
|
||||
}
|
||||
|
||||
private int[] buildHueColorArray(){
|
||||
|
||||
private int[] buildHueColorArray() {
|
||||
int[] hue = new int[361];
|
||||
|
||||
int count = 0;
|
||||
for(int i = hue.length -1; i >= 0; i--, count++){
|
||||
for(int i = hue.length -1; i >= 0; i--, count++) {
|
||||
hue[count] = Color.HSVToColor(new float[]{i, 1f, 1f});
|
||||
}
|
||||
|
||||
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){
|
||||
|
||||
private void drawSatValPanel(Canvas canvas) {
|
||||
final RectF rect = mSatValRect;
|
||||
|
||||
if(BORDER_WIDTH_PX > 0){
|
||||
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,14 +255,12 @@ public class ColorPickerView extends View {
|
|||
|
||||
mSatValTrackerPaint.setColor(0xffdddddd);
|
||||
canvas.drawCircle(p.x, p.y, PALETTE_CIRCLE_TRACKER_RADIUS, mSatValTrackerPaint);
|
||||
|
||||
}
|
||||
|
||||
private void drawHuePanel(Canvas canvas){
|
||||
|
||||
private void drawHuePanel(Canvas canvas) {
|
||||
final RectF rect = mHueRect;
|
||||
|
||||
if(BORDER_WIDTH_PX > 0){
|
||||
if (BORDER_WIDTH_PX > 0) {
|
||||
mBorderPaint.setColor(mBorderColor);
|
||||
canvas.drawRect(rect.left - BORDER_WIDTH_PX,
|
||||
rect.top - BORDER_WIDTH_PX,
|
||||
|
@ -293,18 +286,16 @@ 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;
|
||||
private void drawAlphaPanel(Canvas canvas) {
|
||||
if (!mShowAlphaPanel || mAlphaRect == null || mAlphaPattern == null)
|
||||
return;
|
||||
|
||||
final RectF rect = mAlphaRect;
|
||||
|
||||
if(BORDER_WIDTH_PX > 0){
|
||||
if (BORDER_WIDTH_PX > 0) {
|
||||
mBorderPaint.setColor(mBorderColor);
|
||||
canvas.drawRect(rect.left - BORDER_WIDTH_PX,
|
||||
rect.top - BORDER_WIDTH_PX,
|
||||
|
@ -313,7 +304,6 @@ public class ColorPickerView extends View {
|
|||
mBorderPaint);
|
||||
}
|
||||
|
||||
|
||||
mAlphaPattern.draw(canvas);
|
||||
|
||||
float[] hsv = new float[]{mHue,mSat,mVal};
|
||||
|
@ -323,12 +313,11 @@ 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);
|
||||
|
||||
if(mAlphaSliderText != null && mAlphaSliderText!= ""){
|
||||
if (mAlphaSliderText != null && mAlphaSliderText!= "") {
|
||||
canvas.drawText(mAlphaSliderText, rect.centerX(), rect.centerY() + 4 * mDensity, mAlphaTextPaint);
|
||||
}
|
||||
|
||||
|
@ -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){
|
||||
|
||||
private Point hueToPoint(float hue) {
|
||||
final RectF rect = mHueRect;
|
||||
final float height = rect.height();
|
||||
|
||||
|
@ -360,62 +346,49 @@ public class ColorPickerView extends View {
|
|||
return p;
|
||||
}
|
||||
|
||||
private Point satValToPoint(float sat, float val){
|
||||
|
||||
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);
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
private Point alphaToPoint(int alpha){
|
||||
|
||||
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){
|
||||
|
||||
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);
|
||||
|
@ -423,135 +396,89 @@ public class ColorPickerView extends View {
|
|||
return result;
|
||||
}
|
||||
|
||||
private float pointToHue(float y){
|
||||
|
||||
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){
|
||||
|
||||
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){
|
||||
|
||||
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){
|
||||
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){
|
||||
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()){
|
||||
|
||||
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 (update) {
|
||||
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;
|
||||
private boolean moveTrackersIfNeeded(MotionEvent event) {
|
||||
if (mStartTouchPoint == null)
|
||||
return false;
|
||||
|
||||
boolean update = false;
|
||||
|
||||
int startX = mStartTouchPoint.x;
|
||||
int startY = mStartTouchPoint.y;
|
||||
|
||||
|
||||
if(mHueRect.contains(startX, startY)){
|
||||
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;
|
||||
|
||||
|
@ -664,37 +557,30 @@ public class ColorPickerView extends View {
|
|||
widthAllowed = chooseWidth(widthMode, widthAllowed);
|
||||
heightAllowed = chooseHeight(heightMode, heightAllowed);
|
||||
|
||||
if(!mShowAlphaPanel){
|
||||
|
||||
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")) {
|
||||
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){
|
||||
if (width > widthAllowed) {
|
||||
width = widthAllowed;
|
||||
height = (int) (widthAllowed - HUE_PANEL_WIDTH + ALPHA_PANEL_HEIGHT);
|
||||
}
|
||||
else{
|
||||
} else {
|
||||
height = heightAllowed;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
setMeasuredDimension(width, height);
|
||||
}
|
||||
|
||||
private int chooseWidth(int mode, int size){
|
||||
private int chooseWidth(int mode, int size) {
|
||||
if (mode == MeasureSpec.AT_MOST || mode == MeasureSpec.EXACTLY) {
|
||||
return size;
|
||||
} else { // (mode == MeasureSpec.UNSPECIFIED)
|
||||
|
@ -702,7 +588,7 @@ public class ColorPickerView extends View {
|
|||
}
|
||||
}
|
||||
|
||||
private int chooseHeight(int mode, int size){
|
||||
private int chooseHeight(int mode, int size) {
|
||||
if (mode == MeasureSpec.AT_MOST || mode == MeasureSpec.EXACTLY) {
|
||||
return size;
|
||||
} else { // (mode == MeasureSpec.UNSPECIFIED)
|
||||
|
@ -710,32 +596,20 @@ public class ColorPickerView extends View {
|
|||
}
|
||||
}
|
||||
|
||||
private int getPrefferedWidth(){
|
||||
|
||||
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(){
|
||||
|
||||
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);
|
||||
|
@ -751,14 +625,12 @@ public class ColorPickerView extends View {
|
|||
setUpAlphaRect();
|
||||
}
|
||||
|
||||
private void setUpSatValRect(){
|
||||
|
||||
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;
|
||||
|
@ -768,20 +640,21 @@ public class ColorPickerView extends View {
|
|||
mSatValRect = new RectF(left,top, right, bottom);
|
||||
}
|
||||
|
||||
private void setUpHueRect(){
|
||||
private void setUpHueRect() {
|
||||
final RectF dRect = mDrawingRect;
|
||||
|
||||
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,16 +672,14 @@ 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.
|
||||
* @param listener
|
||||
*/
|
||||
public void setOnColorChangedListener(OnColorChangedListener listener){
|
||||
public void setOnColorChangedListener(OnColorChangedListener listener) {
|
||||
mListener = listener;
|
||||
}
|
||||
|
||||
|
@ -816,7 +687,7 @@ public class ColorPickerView extends View {
|
|||
* Set the color of the border surrounding all panels.
|
||||
* @param color
|
||||
*/
|
||||
public void setBorderColor(int color){
|
||||
public void setBorderColor(int color) {
|
||||
mBorderColor = color;
|
||||
invalidate();
|
||||
}
|
||||
|
@ -824,7 +695,7 @@ public class ColorPickerView extends View {
|
|||
/**
|
||||
* Get the color of the border surrounding all panels.
|
||||
*/
|
||||
public int getBorderColor(){
|
||||
public int getBorderColor() {
|
||||
return mBorderColor;
|
||||
}
|
||||
|
||||
|
@ -832,7 +703,7 @@ public class ColorPickerView extends View {
|
|||
* Get the current color this view is showing.
|
||||
* @return the current color.
|
||||
*/
|
||||
public int getColor(){
|
||||
public int getColor() {
|
||||
return Color.HSVToColor(mAlpha, new float[]{mHue,mSat,mVal});
|
||||
}
|
||||
|
||||
|
@ -840,7 +711,7 @@ public class ColorPickerView extends View {
|
|||
* Set the color the view should show.
|
||||
* @param color The color that should be selected.
|
||||
*/
|
||||
public void setColor(int color){
|
||||
public void setColor(int color) {
|
||||
setColor(color, false);
|
||||
}
|
||||
|
||||
|
@ -850,8 +721,7 @@ public class ColorPickerView extends View {
|
|||
* @param callback If you want to get a callback to
|
||||
* your OnColorChangedListener.
|
||||
*/
|
||||
public void setColor(int color, boolean callback){
|
||||
|
||||
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();
|
||||
}
|
||||
|
@ -881,7 +750,7 @@ public class ColorPickerView extends View {
|
|||
* the currently selected color and want to align it perfectly.
|
||||
* @return The offset in pixels.
|
||||
*/
|
||||
public float getDrawingOffset(){
|
||||
public float getDrawingOffset() {
|
||||
return mDrawingOffset;
|
||||
}
|
||||
|
||||
|
@ -890,9 +759,8 @@ public class ColorPickerView extends View {
|
|||
* If it is set to false no alpha will be set.
|
||||
* @param visible
|
||||
*/
|
||||
public void setAlphaSliderVisible(boolean visible){
|
||||
|
||||
if(mShowAlphaPanel != visible){
|
||||
public void setAlphaSliderVisible(boolean visible) {
|
||||
if (mShowAlphaPanel != visible) {
|
||||
mShowAlphaPanel = visible;
|
||||
|
||||
/*
|
||||
|
@ -907,18 +775,15 @@ public class ColorPickerView extends View {
|
|||
|
||||
requestLayout();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void setSliderTrackerColor(int color){
|
||||
public void setSliderTrackerColor(int color) {
|
||||
mSliderTrackerColor = color;
|
||||
|
||||
mHueTrackerPaint.setColor(mSliderTrackerColor);
|
||||
|
||||
invalidate();
|
||||
}
|
||||
|
||||
public int getSliderTrackerColor(){
|
||||
public int getSliderTrackerColor() {
|
||||
return mSliderTrackerColor;
|
||||
}
|
||||
|
||||
|
@ -927,7 +792,7 @@ public class ColorPickerView extends View {
|
|||
* alpha slider. Set to null to disable text.
|
||||
* @param res string resource id.
|
||||
*/
|
||||
public void setAlphaSliderText(int res){
|
||||
public void setAlphaSliderText(int res) {
|
||||
String text = getContext().getString(res);
|
||||
setAlphaSliderText(text);
|
||||
}
|
||||
|
@ -937,7 +802,7 @@ public class ColorPickerView extends View {
|
|||
* alpha slider. Set to null to disable text.
|
||||
* @param text Text that should be shown.
|
||||
*/
|
||||
public void setAlphaSliderText(String text){
|
||||
public void setAlphaSliderText(String text) {
|
||||
mAlphaSliderText = text;
|
||||
invalidate();
|
||||
}
|
||||
|
@ -948,7 +813,7 @@ public class ColorPickerView extends View {
|
|||
* slider.
|
||||
* @return
|
||||
*/
|
||||
public String getAlphaSliderText(){
|
||||
public String getAlphaSliderText() {
|
||||
return mAlphaSliderText;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user