From f2773b4c35141717482de11b020d262aa4882852 Mon Sep 17 00:00:00 2001 From: Arnaud Bienner Date: Wed, 7 May 2014 01:06:31 +0200 Subject: [PATCH 01/12] Start playing with this, and fix a few things related to init: - init components correctly with the default color value - keep colors as attributes in SeekBar, so we can update gradients when size changed (e.g. during init) with no need to call 'setGradient' methods again --- .../myapplication2/AdvancedColorPicker.java | 19 +++++---- .../com/example/myapplication2/SeekBar.java | 41 +++++++++++++++---- .../com/example/myapplication2/Wheel.java | 2 +- 3 files changed, 44 insertions(+), 18 deletions(-) diff --git a/MyApplication2/src/main/java/com/example/myapplication2/AdvancedColorPicker.java b/MyApplication2/src/main/java/com/example/myapplication2/AdvancedColorPicker.java index 74fa055..841954f 100644 --- a/MyApplication2/src/main/java/com/example/myapplication2/AdvancedColorPicker.java +++ b/MyApplication2/src/main/java/com/example/myapplication2/AdvancedColorPicker.java @@ -42,7 +42,8 @@ public class AdvancedColorPicker extends LinearLayout { // Conversions between HSV and RGB can be lossy, so we store both // separately so that all of the sliders can move independently private float[] mHSV = new float[] { 0f, 1f, 1f }; - private int mColor = Color.BLACK; + // TODO: use CYAN for now, to ease debug. Revert back to black as default value later. + private int mColor = Color.CYAN;//BLACK; private Bar mHueBar; private Bar mSatBar; @@ -94,7 +95,7 @@ public AdvancedColorPicker(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); LayoutInflater inflater = LayoutInflater.from(context); inflater.inflate(R.layout.advanced_color_picker, this); - Color.colorToHSV(DEFAULT_COLORS.get(2), mHSV); + Color.colorToHSV(mColor, mHSV); wheel = (Wheel) findViewById(R.id.wheel); for (Integer i : DEFAULT_COLORS) { @@ -172,6 +173,8 @@ public void onClick(View v) { }); updateGradients(mHSV); + setColor(mColor); + updateLabels(); } private Bar findBar(final int seekbarId, final int labelId, final int type) { @@ -276,15 +279,15 @@ private void updateGradients(float[] hsv) { grad[i] = Color.HSVToColor(new float[]{360*i/(SIZE-1), hsv[1], hsv[2]}); } - mHueBar.seekbar.setBackgroundGradientColors(grad, mHueBar.seekbar.getMeasuredWidth()); + mHueBar.seekbar.setBackgroundGradientColors(grad); mSatBar.seekbar.setGradientColors(new int[] { Color.HSVToColor(new float[]{hsv[0], 1, hsv[2]}), Color.HSVToColor(new float[]{hsv[0], 0, hsv[2]}), - }, mSatBar.seekbar.getMeasuredWidth()); + }); mValBar.seekbar.setGradientColors(new int[] { Color.HSVToColor(new float[]{hsv[0], hsv[1], 0}), Color.HSVToColor(new float[]{hsv[0], hsv[1], 1}), - }, mValBar.seekbar.getMeasuredWidth()); + }); int color = Color.HSVToColor(hsv); mHueBar.seekbar.setThumbColor(color); @@ -305,9 +308,9 @@ private void updateGradients(int color) { int r = Color.red(color); int g = Color.green(color); int b = Color.blue(color); - mRedBar.seekbar.setGradientColors(new int[]{Color.rgb(0, g, b), Color.rgb(255, g, b)}, mRedBar.seekbar.getMeasuredWidth()); - mGreenBar.seekbar.setGradientColors(new int[]{Color.rgb(r, 0, b), Color.rgb(r, 255, b)}, mGreenBar.seekbar.getMeasuredWidth()); - mBlueBar.seekbar.setGradientColors(new int[]{Color.rgb(r, g, 0), Color.rgb(r, g, 255)}, mBlueBar.seekbar.getMeasuredWidth()); + mRedBar.seekbar.setGradientColors(new int[]{Color.rgb(0, g, b), Color.rgb(255, g, b)}); + mGreenBar.seekbar.setGradientColors(new int[]{Color.rgb(r, 0, b), Color.rgb(r, 255, b)}); + mBlueBar.seekbar.setGradientColors(new int[]{Color.rgb(r, g, 0), Color.rgb(r, g, 255)}); mRedBar.seekbar.setThumbColor(color); mGreenBar.seekbar.setThumbColor(color); diff --git a/MyApplication2/src/main/java/com/example/myapplication2/SeekBar.java b/MyApplication2/src/main/java/com/example/myapplication2/SeekBar.java index 10cc3ab..e89cbd5 100644 --- a/MyApplication2/src/main/java/com/example/myapplication2/SeekBar.java +++ b/MyApplication2/src/main/java/com/example/myapplication2/SeekBar.java @@ -10,6 +10,8 @@ public class SeekBar extends android.widget.SeekBar { private BorderedBox mThumb; + private int[] mBackgroundColors; + private int[] mGradientColors; public SeekBar(Context context) { super(context); @@ -35,28 +37,49 @@ private void init(Context context) { mThumb.setIntrinsicHeight(65); setThumb(mThumb); } + + @Override + protected void onSizeChanged (int w, int h, int oldw, int oldh) { + super.onSizeChanged(w, h, oldw, oldh); + updateGradient(); + updateBackgroundGradient(); + } - public void setBackgroundGradientColors(int[] colors, int width) { - setGrad(getBackground(), colors, width); + public void setGradientColors(int[] colors) { + mGradientColors = colors; + updateGradient(); + } + + private void updateGradient() { + if (mGradientColors == null || mGradientColors.length == 0) + return; + setGrad(getProgressDrawable(), mGradientColors); + invalidate(); + } + + public void setBackgroundGradientColors(int[] colors) { + mBackgroundColors = colors; + updateBackgroundGradient(); + } + + private void updateBackgroundGradient() { + if (mBackgroundColors == null || mBackgroundColors.length == 0) + return; + setGrad(getBackground(), mBackgroundColors); invalidate(); } - private void setGrad(Drawable d, int[] colors, int width) { + private void setGrad(Drawable d, int[] colors) { if (d == null) return; - LinearGradient lg = new LinearGradient(0f, 0f, width, 0f, colors, null, Shader.TileMode.CLAMP); + LinearGradient lg = new LinearGradient(0f, 0f, getWidth(), 0f, colors, null, Shader.TileMode.CLAMP); if (d instanceof BorderedBox) ((BorderedBox)d).setShader(lg); else if (d instanceof ShapeDrawable) ((ShapeDrawable)d).getPaint().setShader(lg); } - public void setGradientColors(int[] colors, int width) { - setGrad(getProgressDrawable(), colors, width); - invalidate(); - } - public void setThumbColor(int color) { mThumb.setColor(color); } diff --git a/MyApplication2/src/main/java/com/example/myapplication2/Wheel.java b/MyApplication2/src/main/java/com/example/myapplication2/Wheel.java index e72740b..8c5f6d5 100644 --- a/MyApplication2/src/main/java/com/example/myapplication2/Wheel.java +++ b/MyApplication2/src/main/java/com/example/myapplication2/Wheel.java @@ -13,7 +13,7 @@ import android.widget.RemoteViews; /** - * View group that lays out items in a cirlce. Dragging on the view will rotate the items around the circle + * View group that lays out items in a circle. Dragging on the view will rotate the items around the circle */ @RemoteViews.RemoteView public class Wheel extends ViewGroup { From 332b7b4b5c7cc19a08b8f700cd008de32f188946 Mon Sep 17 00:00:00 2001 From: Arnaud Bienner Date: Sat, 17 May 2014 22:08:59 +0200 Subject: [PATCH 02/12] Don't use hard coded values for border colors, etc. Use style instead --- .../myapplication2/AdvancedColorPicker.java | 17 ++++++++--------- .../com/example/myapplication2/BorderedBox.java | 7 ++++--- .../com/example/myapplication2/SeekBar.java | 2 +- .../main/res/layout/advanced_color_picker.xml | 6 +++--- MyApplication2/src/main/res/values/styles.xml | 7 +++++++ 5 files changed, 23 insertions(+), 16 deletions(-) diff --git a/MyApplication2/src/main/java/com/example/myapplication2/AdvancedColorPicker.java b/MyApplication2/src/main/java/com/example/myapplication2/AdvancedColorPicker.java index 841954f..9154615 100644 --- a/MyApplication2/src/main/java/com/example/myapplication2/AdvancedColorPicker.java +++ b/MyApplication2/src/main/java/com/example/myapplication2/AdvancedColorPicker.java @@ -142,13 +142,13 @@ public void onChange(int index) { wheel.setCenter(0.5f, 1.02f); } - mHueBar = findBar(R.id.hueSeekbar, R.id.hueLabel, HUE); - mSatBar = findBar(R.id.satSeekbar, R.id.satLabel, SAT); - mValBar = findBar(R.id.valSeekbar, R.id.valLabel, VAL); + mHueBar = findBar(context, R.id.hueSeekbar, R.id.hueLabel, HUE); + mSatBar = findBar(context, R.id.satSeekbar, R.id.satLabel, SAT); + mValBar = findBar(context, R.id.valSeekbar, R.id.valLabel, VAL); - mRedBar = findBar(R.id.redSeekbar, R.id.redLabel, RED); - mGreenBar = findBar(R.id.greenSeekbar, R.id.greenLabel, GREEN); - mBlueBar = findBar(R.id.blueSeekbar, R.id.blueLabel, BLUE); + mRedBar = findBar(context, R.id.redSeekbar, R.id.redLabel, RED); + mGreenBar = findBar(context, R.id.greenSeekbar, R.id.greenLabel, GREEN); + mBlueBar = findBar(context, R.id.blueSeekbar, R.id.blueLabel, BLUE); TextView hsvLabel = (TextView) findViewById(R.id.hsvLabel); @@ -177,11 +177,10 @@ public void onClick(View v) { updateLabels(); } - private Bar findBar(final int seekbarId, final int labelId, final int type) { - BorderedBox border = new BorderedBox(); + private Bar findBar(Context context, final int seekbarId, final int labelId, final int type) { + BorderedBox border = new BorderedBox(context); border.setBorderRadius(1); border.setBorderThickness(3); - border.setBorderColor(Color.LTGRAY); border.setShadowInset(true); border.setShadowShader(new LinearGradient(0f, 0f, 0f, 50, new int[] { Color.argb(100,0,0,0), Color.argb(0,0,0,0) diff --git a/MyApplication2/src/main/java/com/example/myapplication2/BorderedBox.java b/MyApplication2/src/main/java/com/example/myapplication2/BorderedBox.java index 02d61cf..fc30182 100644 --- a/MyApplication2/src/main/java/com/example/myapplication2/BorderedBox.java +++ b/MyApplication2/src/main/java/com/example/myapplication2/BorderedBox.java @@ -1,5 +1,6 @@ package com.example.myapplication2; +import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.ColorFilter; @@ -24,15 +25,15 @@ public class BorderedBox extends Drawable { private int mHeight = -1; private boolean mShadowInset; - public BorderedBox() { + public BorderedBox(Context context) { mShadowPaint = new Paint(); mShadowPaint.setStyle(Paint.Style.FILL_AND_STROKE); - mShadowPaint.setColor(Color.GRAY); + mShadowPaint.setColor(context.getResources().getColor(android.R.color.background_dark)); mShadowPaint.setAntiAlias(true); mBorderPaint = new Paint(); mBorderPaint.setStyle(Paint.Style.STROKE); - mBorderPaint.setColor(Color.WHITE); + mBorderPaint.setColor(context.getResources().getColor(android.R.color.background_light)); mBorderPaint.setAntiAlias(true); mCenter = new Paint(); diff --git a/MyApplication2/src/main/java/com/example/myapplication2/SeekBar.java b/MyApplication2/src/main/java/com/example/myapplication2/SeekBar.java index e89cbd5..88dcd74 100644 --- a/MyApplication2/src/main/java/com/example/myapplication2/SeekBar.java +++ b/MyApplication2/src/main/java/com/example/myapplication2/SeekBar.java @@ -29,7 +29,7 @@ public SeekBar(Context context, AttributeSet attrs, int defStyle) { } private void init(Context context) { - mThumb = new BorderedBox(); + mThumb = new BorderedBox(context); mThumb.setBorderThickness(5); mThumb.setBorderRadius(1f); mThumb.setShadowOffset(new PointF(0, 2)); diff --git a/MyApplication2/src/main/res/layout/advanced_color_picker.xml b/MyApplication2/src/main/res/layout/advanced_color_picker.xml index 335703f..fa601fe 100644 --- a/MyApplication2/src/main/res/layout/advanced_color_picker.xml +++ b/MyApplication2/src/main/res/layout/advanced_color_picker.xml @@ -54,7 +54,7 @@ - - - \ No newline at end of file + diff --git a/MyApplication2/src/main/res/values/styles.xml b/MyApplication2/src/main/res/values/styles.xml index 303627d..ad6c358 100644 --- a/MyApplication2/src/main/res/values/styles.xml +++ b/MyApplication2/src/main/res/values/styles.xml @@ -46,6 +46,7 @@ 40sp horizontal ?android:attr/textAppearanceMedium + @android:color/primary_text_light + + From e57c44612407326eaf10ccafc0f426198035ca4c Mon Sep 17 00:00:00 2001 From: Arnaud Bienner Date: Sat, 17 May 2014 22:09:52 +0200 Subject: [PATCH 03/12] Looks like we're not catching the correct exception type which may happen here --- .../java/com/example/myapplication2/AdvancedColorPicker.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MyApplication2/src/main/java/com/example/myapplication2/AdvancedColorPicker.java b/MyApplication2/src/main/java/com/example/myapplication2/AdvancedColorPicker.java index 9154615..47f2722 100644 --- a/MyApplication2/src/main/java/com/example/myapplication2/AdvancedColorPicker.java +++ b/MyApplication2/src/main/java/com/example/myapplication2/AdvancedColorPicker.java @@ -216,7 +216,7 @@ public void onTextChanged(CharSequence s, int start, int before, int count) { try { CharSequence text = bar.label.getText(); val = Float.parseFloat(text.toString()); - } catch (NullPointerException ex) { + } catch (NumberFormatException ex) { Log.i(LOGTAG, "Invalid float", ex); } From e70d4355580897cd1feeffa5cc0bb879f76aecb1 Mon Sep 17 00:00:00 2001 From: Arnaud Bienner Date: Sat, 17 May 2014 22:19:33 +0200 Subject: [PATCH 04/12] Remove a few unneeded things --- .../src/main/java/com/example/myapplication2/BorderedBox.java | 1 - .../main/java/com/example/myapplication2/MainActivity.java | 2 -- .../src/main/java/com/example/myapplication2/Slice.java | 4 ---- .../src/main/java/com/example/myapplication2/Wheel.java | 1 - 4 files changed, 8 deletions(-) diff --git a/MyApplication2/src/main/java/com/example/myapplication2/BorderedBox.java b/MyApplication2/src/main/java/com/example/myapplication2/BorderedBox.java index fc30182..443c0d5 100644 --- a/MyApplication2/src/main/java/com/example/myapplication2/BorderedBox.java +++ b/MyApplication2/src/main/java/com/example/myapplication2/BorderedBox.java @@ -19,7 +19,6 @@ public class BorderedBox extends Drawable { private final Paint mBorderPaint; private final Paint mCenter; private float mRadius = 0; - private int mBorderWidth = 0; private PointF mShadowOffset = new PointF(0,0); private int mWidth = -1; private int mHeight = -1; diff --git a/MyApplication2/src/main/java/com/example/myapplication2/MainActivity.java b/MyApplication2/src/main/java/com/example/myapplication2/MainActivity.java index 93fa77c..783415a 100644 --- a/MyApplication2/src/main/java/com/example/myapplication2/MainActivity.java +++ b/MyApplication2/src/main/java/com/example/myapplication2/MainActivity.java @@ -4,11 +4,9 @@ import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; -import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; -import android.view.ViewGroup; import android.widget.Button; public class MainActivity extends Activity { diff --git a/MyApplication2/src/main/java/com/example/myapplication2/Slice.java b/MyApplication2/src/main/java/com/example/myapplication2/Slice.java index b0fd27b..534b3f3 100644 --- a/MyApplication2/src/main/java/com/example/myapplication2/Slice.java +++ b/MyApplication2/src/main/java/com/example/myapplication2/Slice.java @@ -22,11 +22,7 @@ public class Slice extends View { private static final String LOGTAG = "Slice"; - private static Path mPath; private static Paint mPaint = new Paint(); - private static int mStrokeWidth = 40; - private float mAngle; - private float mRadius; private float mOffset; public Slice(Context context) { diff --git a/MyApplication2/src/main/java/com/example/myapplication2/Wheel.java b/MyApplication2/src/main/java/com/example/myapplication2/Wheel.java index 8c5f6d5..1dfacda 100644 --- a/MyApplication2/src/main/java/com/example/myapplication2/Wheel.java +++ b/MyApplication2/src/main/java/com/example/myapplication2/Wheel.java @@ -226,7 +226,6 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto for (int i = 0; i < count; i++) { final View child = getChildAt(i); if (child.getVisibility() != GONE) { - final LayoutParams lp = (LayoutParams) child.getLayoutParams(); child.layout(cx, cy, cx + mRadius, cy + 2*mSliceHeight); } } From 267a1c84746d319b3b965e706ba0eac08de0435a Mon Sep 17 00:00:00 2001 From: Arnaud Bienner Date: Sat, 17 May 2014 22:26:09 +0200 Subject: [PATCH 05/12] and a few others --- .../src/main/java/com/example/myapplication2/Slice.java | 6 ------ 1 file changed, 6 deletions(-) diff --git a/MyApplication2/src/main/java/com/example/myapplication2/Slice.java b/MyApplication2/src/main/java/com/example/myapplication2/Slice.java index 534b3f3..3792d2a 100644 --- a/MyApplication2/src/main/java/com/example/myapplication2/Slice.java +++ b/MyApplication2/src/main/java/com/example/myapplication2/Slice.java @@ -79,13 +79,7 @@ private void init(Context context) { mPaint.setColor(color); } - protected void onConfigurationChanged (Configuration newConfig) { - mPath = null; - } - public void setParams(float angle, float r, float offset) { - mAngle = angle; - mRadius = r; if (mOffset != offset) { mOffset = offset; } From 8bb6ec40985f3f2efdb15ea47b48f03344c0ea5f Mon Sep 17 00:00:00 2001 From: Arnaud Bienner Date: Sat, 17 May 2014 22:45:21 +0200 Subject: [PATCH 06/12] Don't try to add a '%' sign to the left of the EditText. This breaks border highlight when the text field is selected, among other things. Looks like this is no other way to do something similar, so better to put the % sign apart. And this doesn't look bad IMHO. --- .../main/res/layout/advanced_color_picker.xml | 46 ++++++++----------- 1 file changed, 19 insertions(+), 27 deletions(-) diff --git a/MyApplication2/src/main/res/layout/advanced_color_picker.xml b/MyApplication2/src/main/res/layout/advanced_color_picker.xml index fa601fe..8070197 100644 --- a/MyApplication2/src/main/res/layout/advanced_color_picker.xml +++ b/MyApplication2/src/main/res/layout/advanced_color_picker.xml @@ -49,35 +49,27 @@ - - - - - - - - - - - + - + - + + + From 5d65542d1df9db50abfd3174157ddf3850bd32da Mon Sep 17 00:00:00 2001 From: Arnaud Bienner Date: Sat, 17 May 2014 23:36:35 +0200 Subject: [PATCH 07/12] Edit was almost similar. No need to replicate it --- MyApplication2/src/main/res/values-land/styles.xml | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/MyApplication2/src/main/res/values-land/styles.xml b/MyApplication2/src/main/res/values-land/styles.xml index f78da9b..fcb6fb4 100644 --- a/MyApplication2/src/main/res/values-land/styles.xml +++ b/MyApplication2/src/main/res/values-land/styles.xml @@ -25,16 +25,4 @@ match_parent - - - \ No newline at end of file + From 37e9392b9bae3ade0187038580f3fa50aa51d3d4 Mon Sep 17 00:00:00 2001 From: Arnaud Bienner Date: Sun, 1 Jun 2014 00:51:40 +0200 Subject: [PATCH 08/12] Make setColor public and add a getColor method --- .../com/example/myapplication2/AdvancedColorPicker.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/MyApplication2/src/main/java/com/example/myapplication2/AdvancedColorPicker.java b/MyApplication2/src/main/java/com/example/myapplication2/AdvancedColorPicker.java index 47f2722..7052ad6 100644 --- a/MyApplication2/src/main/java/com/example/myapplication2/AdvancedColorPicker.java +++ b/MyApplication2/src/main/java/com/example/myapplication2/AdvancedColorPicker.java @@ -392,12 +392,16 @@ private void setRGB(int r, int g, int b) { setColor(Color.rgb(r, g, b)); } - private void setColor(int color) { + public void setColor(int color) { mColor = color; updateGradients(mColor); setWheel(mColor); } + public int getColor() { + return mColor; + } + private void setHSV(float h, float s, float v) { mHSV[0] = h; mHSV[1] = s; From a1435d89aadea3535b455f05a1a1b45a23c0f303 Mon Sep 17 00:00:00 2001 From: Arnaud Bienner Date: Sun, 1 Jun 2014 16:54:02 +0200 Subject: [PATCH 09/12] Reactivate hardware acceleration in the project settings, and remove the setLayerType calls, which don't look necessary anymore --- MyApplication2/src/main/AndroidManifest.xml | 2 +- .../java/com/example/myapplication2/Wheel.java | 14 ++------------ 2 files changed, 3 insertions(+), 13 deletions(-) diff --git a/MyApplication2/src/main/AndroidManifest.xml b/MyApplication2/src/main/AndroidManifest.xml index 53fdfb9..a4e5a4e 100644 --- a/MyApplication2/src/main/AndroidManifest.xml +++ b/MyApplication2/src/main/AndroidManifest.xml @@ -11,7 +11,7 @@ diff --git a/MyApplication2/src/main/java/com/example/myapplication2/Wheel.java b/MyApplication2/src/main/java/com/example/myapplication2/Wheel.java index 1dfacda..4b618cd 100644 --- a/MyApplication2/src/main/java/com/example/myapplication2/Wheel.java +++ b/MyApplication2/src/main/java/com/example/myapplication2/Wheel.java @@ -258,11 +258,8 @@ private boolean endDrag(MotionEvent event) { public void setSelectedSegment(int i) { int count = getChildCount(); int angle = 360 / count; - if (mAnimator != null) + if (mAnimator != null) { mAnimator.endAnimation(); - - if (Build.VERSION.SDK_INT >= 11) { - setLayerType(View.LAYER_TYPE_HARDWARE, null); } mAnimator = new WheelAnimator((180 - angle * i + angle / 2)%360); } @@ -311,9 +308,6 @@ protected void endAnimation() { mRotation = mEnd; notifyListeners(); mAnimator = null; - if (Build.VERSION.SDK_INT >= 11) { - setLayerType(View.LAYER_TYPE_SOFTWARE, null); - } } @Override @@ -337,13 +331,9 @@ private boolean drag(MotionEvent event) { } private boolean startDrag(MotionEvent event) { - if (mAnimator != null) + if (mAnimator != null) { mAnimator.endAnimation(); - - if (Build.VERSION.SDK_INT >= 11) { - setLayerType(View.LAYER_TYPE_HARDWARE, null); } - mStartDragRotation = getAngle(event); return true; } From 24432e9c73137ace72fc81a3abff9328cd1095b9 Mon Sep 17 00:00:00 2001 From: Arnaud Bienner Date: Fri, 13 Jun 2014 00:41:38 +0200 Subject: [PATCH 10/12] Update mHSV when updating color and vice et versa --- .../com/example/myapplication2/AdvancedColorPicker.java | 5 +++++ .../main/java/com/example/myapplication2/MainActivity.java | 6 ++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/MyApplication2/src/main/java/com/example/myapplication2/AdvancedColorPicker.java b/MyApplication2/src/main/java/com/example/myapplication2/AdvancedColorPicker.java index 7052ad6..dfa772b 100644 --- a/MyApplication2/src/main/java/com/example/myapplication2/AdvancedColorPicker.java +++ b/MyApplication2/src/main/java/com/example/myapplication2/AdvancedColorPicker.java @@ -394,7 +394,10 @@ private void setRGB(int r, int g, int b) { public void setColor(int color) { mColor = color; + Color.colorToHSV(mColor, mHSV); updateGradients(mColor); + updateGradients(mHSV); + updateLabels(); setWheel(mColor); } @@ -408,5 +411,7 @@ private void setHSV(float h, float s, float v) { mHSV[2] = v; updateGradients(mHSV); setWheel(mHSV); + mColor = Color.HSVToColor(mHSV); + updateGradients(mColor); } } diff --git a/MyApplication2/src/main/java/com/example/myapplication2/MainActivity.java b/MyApplication2/src/main/java/com/example/myapplication2/MainActivity.java index 783415a..de281c2 100644 --- a/MyApplication2/src/main/java/com/example/myapplication2/MainActivity.java +++ b/MyApplication2/src/main/java/com/example/myapplication2/MainActivity.java @@ -20,16 +20,18 @@ protected void onCreate(Bundle savedInstanceState) { b.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { + final AdvancedColorPicker acp = new AdvancedColorPicker(MainActivity.this); (new AlertDialog.Builder(MainActivity.this)) .setTitle("Pick a color") - .setView(new AdvancedColorPicker(MainActivity.this)) + .setView(acp) .setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - dialog.dismiss();; + dialog.dismiss(); } }).create() .show(); + acp.setColor(0); } }); } From 09a6c312e4b3ec86b7b2875212dc5efe77f444fa Mon Sep 17 00:00:00 2001 From: Arnaud Bienner Date: Sun, 22 Jun 2014 13:14:43 +0200 Subject: [PATCH 11/12] Remove some trailing spaces and other useless things --- .../main/java/com/example/myapplication2/Animator.java | 2 +- .../main/java/com/example/myapplication2/BorderedBox.java | 2 +- .../src/main/java/com/example/myapplication2/SeekBar.java | 8 ++++---- .../src/main/java/com/example/myapplication2/Slice.java | 3 --- 4 files changed, 6 insertions(+), 9 deletions(-) diff --git a/MyApplication2/src/main/java/com/example/myapplication2/Animator.java b/MyApplication2/src/main/java/com/example/myapplication2/Animator.java index a6e4385..e52b87f 100644 --- a/MyApplication2/src/main/java/com/example/myapplication2/Animator.java +++ b/MyApplication2/src/main/java/com/example/myapplication2/Animator.java @@ -45,7 +45,7 @@ public void step() { return; } - float dt = (float)(now - startTime); + float dt = now - startTime; stepAnimation(dt/DURATION); if (Build.VERSION.SDK_INT >= 16) { diff --git a/MyApplication2/src/main/java/com/example/myapplication2/BorderedBox.java b/MyApplication2/src/main/java/com/example/myapplication2/BorderedBox.java index 443c0d5..99824aa 100644 --- a/MyApplication2/src/main/java/com/example/myapplication2/BorderedBox.java +++ b/MyApplication2/src/main/java/com/example/myapplication2/BorderedBox.java @@ -39,7 +39,7 @@ public BorderedBox(Context context) { mCenter.setColor(Color.RED); mCenter.setAntiAlias(true); } - + @Override public void draw(Canvas canvas) { Rect bounds = getBounds(); diff --git a/MyApplication2/src/main/java/com/example/myapplication2/SeekBar.java b/MyApplication2/src/main/java/com/example/myapplication2/SeekBar.java index 88dcd74..e28b85a 100644 --- a/MyApplication2/src/main/java/com/example/myapplication2/SeekBar.java +++ b/MyApplication2/src/main/java/com/example/myapplication2/SeekBar.java @@ -37,7 +37,7 @@ private void init(Context context) { mThumb.setIntrinsicHeight(65); setThumb(mThumb); } - + @Override protected void onSizeChanged (int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); @@ -49,19 +49,19 @@ public void setGradientColors(int[] colors) { mGradientColors = colors; updateGradient(); } - + private void updateGradient() { if (mGradientColors == null || mGradientColors.length == 0) return; setGrad(getProgressDrawable(), mGradientColors); invalidate(); } - + public void setBackgroundGradientColors(int[] colors) { mBackgroundColors = colors; updateBackgroundGradient(); } - + private void updateBackgroundGradient() { if (mBackgroundColors == null || mBackgroundColors.length == 0) return; diff --git a/MyApplication2/src/main/java/com/example/myapplication2/Slice.java b/MyApplication2/src/main/java/com/example/myapplication2/Slice.java index 3792d2a..2b1b00e 100644 --- a/MyApplication2/src/main/java/com/example/myapplication2/Slice.java +++ b/MyApplication2/src/main/java/com/example/myapplication2/Slice.java @@ -1,13 +1,10 @@ package com.example.myapplication2; -import android.annotation.SuppressLint; import android.content.Context; -import android.content.res.Configuration; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; -import android.graphics.Path; import android.graphics.Rect; import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; From fa8f29eedb5e88af9e4f86609ba994ee6aa3fd43 Mon Sep 17 00:00:00 2001 From: Arnaud Bienner Date: Sun, 22 Jun 2014 14:14:26 +0200 Subject: [PATCH 12/12] Continue to clean up --- .../com/example/myapplication2/SeekBar.java | 6 +-- .../com/example/myapplication2/Slice.java | 50 ++++++------------- 2 files changed, 18 insertions(+), 38 deletions(-) diff --git a/MyApplication2/src/main/java/com/example/myapplication2/SeekBar.java b/MyApplication2/src/main/java/com/example/myapplication2/SeekBar.java index e28b85a..0ec45b4 100644 --- a/MyApplication2/src/main/java/com/example/myapplication2/SeekBar.java +++ b/MyApplication2/src/main/java/com/example/myapplication2/SeekBar.java @@ -53,7 +53,7 @@ public void setGradientColors(int[] colors) { private void updateGradient() { if (mGradientColors == null || mGradientColors.length == 0) return; - setGrad(getProgressDrawable(), mGradientColors); + setGradient(getProgressDrawable(), mGradientColors); invalidate(); } @@ -65,11 +65,11 @@ public void setBackgroundGradientColors(int[] colors) { private void updateBackgroundGradient() { if (mBackgroundColors == null || mBackgroundColors.length == 0) return; - setGrad(getBackground(), mBackgroundColors); + setGradient(getBackground(), mBackgroundColors); invalidate(); } - private void setGrad(Drawable d, int[] colors) { + private void setGradient(Drawable d, int[] colors) { if (d == null) return; diff --git a/MyApplication2/src/main/java/com/example/myapplication2/Slice.java b/MyApplication2/src/main/java/com/example/myapplication2/Slice.java index 2b1b00e..e17c207 100644 --- a/MyApplication2/src/main/java/com/example/myapplication2/Slice.java +++ b/MyApplication2/src/main/java/com/example/myapplication2/Slice.java @@ -3,8 +3,6 @@ import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; -import android.graphics.Color; -import android.graphics.Paint; import android.graphics.Rect; import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; @@ -19,8 +17,6 @@ public class Slice extends View { private static final String LOGTAG = "Slice"; - private static Paint mPaint = new Paint(); - private float mOffset; public Slice(Context context) { super(context); @@ -40,45 +36,29 @@ public Slice(Context context, AttributeSet attrs, int defStyle) { @Override public void setBackgroundColor(int color) { Log.i(LOGTAG, "Set color " + color); - mPaint.setColor(color); super.setBackgroundColor(color); } private void init(Context context) { Drawable d = getBackground(); Log.i(LOGTAG, "Get background " + d); - int color = Color.BLACK; - if (d instanceof ColorDrawable) { - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { - // http://stackoverflow.com/questions/8089054/get-the-background-color-of-a-button-in-android - // If the ColorDrawable makes use of its bounds in the draw method, - // we may not be able to get the color we want. This is not the usual - // case before Ice Cream Sandwich (4.0.1 r1). - // Yet, we change the bounds temporarily, just to be sure that we are - // successful. - ColorDrawable colorDrawable = (ColorDrawable) d; + if (d instanceof ColorDrawable && Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { + // http://stackoverflow.com/questions/8089054/get-the-background-color-of-a-button-in-android + // If the ColorDrawable makes use of its bounds in the draw method, + // we may not be able to get the color we want. This is not the usual + // case before Ice Cream Sandwich (4.0.1 r1). + // Yet, we change the bounds temporarily, just to be sure that we are + // successful. + ColorDrawable colorDrawable = (ColorDrawable) d; - Rect bounds = new Rect(); - bounds.set(colorDrawable.getBounds()); // Save the original bounds. - colorDrawable.setBounds(0, 0, 1, 1); // Change the bounds. + Rect bounds = new Rect(); + bounds.set(colorDrawable.getBounds()); // Save the original bounds. + colorDrawable.setBounds(0, 0, 1, 1); // Change the bounds. - Bitmap bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); - Canvas canvas = new Canvas(bitmap); - colorDrawable.draw(canvas); - color = bitmap.getPixel(0, 0); - - colorDrawable.setBounds(bounds); // Restore the original bounds. - } else { - color = ((ColorDrawable) d).getColor(); - } - } - - mPaint.setColor(color); - } - - public void setParams(float angle, float r, float offset) { - if (mOffset != offset) { - mOffset = offset; + Bitmap bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); + Canvas canvas = new Canvas(bitmap); + colorDrawable.draw(canvas); + colorDrawable.setBounds(bounds); // Restore the original bounds. } } }