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/AdvancedColorPicker.java b/MyApplication2/src/main/java/com/example/myapplication2/AdvancedColorPicker.java index 74fa055..dfa772b 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) { @@ -141,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); @@ -172,13 +173,14 @@ public void onClick(View v) { }); updateGradients(mHSV); + setColor(mColor); + 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) @@ -214,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); } @@ -276,15 +278,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 +307,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); @@ -390,17 +392,26 @@ 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; + Color.colorToHSV(mColor, mHSV); updateGradients(mColor); + updateGradients(mHSV); + updateLabels(); setWheel(mColor); } + public int getColor() { + return mColor; + } + private void setHSV(float h, float s, float v) { mHSV[0] = h; mHSV[1] = s; mHSV[2] = v; updateGradients(mHSV); setWheel(mHSV); + mColor = Color.HSVToColor(mHSV); + updateGradients(mColor); } } 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 02d61cf..99824aa 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; @@ -18,28 +19,27 @@ 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; 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(); 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/MainActivity.java b/MyApplication2/src/main/java/com/example/myapplication2/MainActivity.java index 93fa77c..de281c2 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 { @@ -22,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); } }); } diff --git a/MyApplication2/src/main/java/com/example/myapplication2/SeekBar.java b/MyApplication2/src/main/java/com/example/myapplication2/SeekBar.java index 10cc3ab..0ec45b4 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); @@ -27,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)); @@ -36,27 +38,48 @@ private void init(Context context) { setThumb(mThumb); } - public void setBackgroundGradientColors(int[] colors, int width) { - setGrad(getBackground(), colors, width); + @Override + protected void onSizeChanged (int w, int h, int oldw, int oldh) { + super.onSizeChanged(w, h, oldw, oldh); + updateGradient(); + updateBackgroundGradient(); + } + + public void setGradientColors(int[] colors) { + mGradientColors = colors; + updateGradient(); + } + + private void updateGradient() { + if (mGradientColors == null || mGradientColors.length == 0) + return; + setGradient(getProgressDrawable(), mGradientColors); + invalidate(); + } + + public void setBackgroundGradientColors(int[] colors) { + mBackgroundColors = colors; + updateBackgroundGradient(); + } + + private void updateBackgroundGradient() { + if (mBackgroundColors == null || mBackgroundColors.length == 0) + return; + setGradient(getBackground(), mBackgroundColors); invalidate(); } - private void setGrad(Drawable d, int[] colors, int width) { + private void setGradient(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/Slice.java b/MyApplication2/src/main/java/com/example/myapplication2/Slice.java index b0fd27b..e17c207 100644 --- a/MyApplication2/src/main/java/com/example/myapplication2/Slice.java +++ b/MyApplication2/src/main/java/com/example/myapplication2/Slice.java @@ -1,13 +1,8 @@ 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; @@ -22,12 +17,6 @@ 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) { super(context); @@ -47,51 +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); - } - - 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; + 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. } } } diff --git a/MyApplication2/src/main/java/com/example/myapplication2/Wheel.java b/MyApplication2/src/main/java/com/example/myapplication2/Wheel.java index e72740b..4b618cd 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 { @@ -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); } } @@ -259,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); } @@ -312,9 +308,6 @@ protected void endAnimation() { mRotation = mEnd; notifyListeners(); mAnimator = null; - if (Build.VERSION.SDK_INT >= 11) { - setLayerType(View.LAYER_TYPE_SOFTWARE, null); - } } @Override @@ -338,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; } diff --git a/MyApplication2/src/main/res/layout/advanced_color_picker.xml b/MyApplication2/src/main/res/layout/advanced_color_picker.xml index 335703f..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 @@ - - - - - - - - - + - + - + - + @@ -127,4 +119,4 @@ - \ No newline at end of file + 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 + 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 + +