diff --git a/library/res/values/attrs.xml b/library/res/values/attrs.xml index e786569..52868c3 100644 --- a/library/res/values/attrs.xml +++ b/library/res/values/attrs.xml @@ -9,6 +9,10 @@ + + + + diff --git a/library/res/values/styles.xml b/library/res/values/styles.xml index 24f2c2d..3722d83 100644 --- a/library/res/values/styles.xml +++ b/library/res/values/styles.xml @@ -9,6 +9,7 @@ #ffffffff true true + square \ No newline at end of file diff --git a/library/src/com/pascalwelsch/holocircularprogressbar/HoloCircularProgressBar.java b/library/src/com/pascalwelsch/holocircularprogressbar/HoloCircularProgressBar.java index 0961b57..8abb30b 100644 --- a/library/src/com/pascalwelsch/holocircularprogressbar/HoloCircularProgressBar.java +++ b/library/src/com/pascalwelsch/holocircularprogressbar/HoloCircularProgressBar.java @@ -26,6 +26,13 @@ */ public class HoloCircularProgressBar extends View { + /** + * Types of thumb shape + */ + public enum ThumbShape { + Square, Circle + } + /** * TAG constant for logging */ @@ -67,13 +74,18 @@ public class HoloCircularProgressBar extends View { */ private static final String INSTANCE_STATE_MARKER_VISIBLE = "marker_visible"; + /** + * used to save and restore the type of the thumb in this instance + */ + private static final String INSTANCE_STATE_THUMB_TYPE = "thumb_type"; + /** * The rectangle enclosing the circle. */ private final RectF mCircleBounds = new RectF(); /** - * the rect for the thumb square + * the rect for the thumb shape */ private final RectF mSquareRect = new RectF(); @@ -184,6 +196,11 @@ public class HoloCircularProgressBar extends View { */ private int mThumbRadius = 20; + /** + * The type of Thumb shape to draw. + */ + private ThumbShape mThumbShape = ThumbShape.Square; + /** * The Translation offset x which gives us the ability to use our own coordinates system. */ @@ -252,6 +269,9 @@ public HoloCircularProgressBar(final Context context, final AttributeSet attrs, .getBoolean(R.styleable.HoloCircularProgressBar_thumb_visible, true)); setMarkerEnabled(attributes .getBoolean(R.styleable.HoloCircularProgressBar_marker_visible, true)); + setThumbShape(ThumbShape.values()[ + attributes.getInt(R.styleable.HoloCircularProgressBar_thumb_shape, 0) + ]); mGravity = attributes .getInt(R.styleable.HoloCircularProgressBar_android_gravity, @@ -310,13 +330,21 @@ protected void onDraw(final Canvas canvas) { // draw the thumb square at the correct rotated position canvas.save(); canvas.rotate(progressRotation - 90); - // rotate the square by 45 degrees - canvas.rotate(45, mThumbPosX, mThumbPosY); - mSquareRect.left = mThumbPosX - mThumbRadius / 3; - mSquareRect.right = mThumbPosX + mThumbRadius / 3; - mSquareRect.top = mThumbPosY - mThumbRadius / 3; - mSquareRect.bottom = mThumbPosY + mThumbRadius / 3; - canvas.drawRect(mSquareRect, mThumbColorPaint); + + + if (mThumbShape == ThumbShape.Square) { + // rotate the square by 45 degrees + canvas.rotate(45, mThumbPosX, mThumbPosY); + mSquareRect.left = mThumbPosX - mThumbRadius / 3; + mSquareRect.right = mThumbPosX + mThumbRadius / 3; + mSquareRect.top = mThumbPosY - mThumbRadius / 3; + mSquareRect.bottom = mThumbPosY + mThumbRadius / 3; + canvas.drawRect(mSquareRect, mThumbColorPaint); + } else { + canvas.drawCircle(mThumbPosX, mThumbPosY, + mThumbRadius/2, mThumbColorPaint); + } + canvas.restore(); } } @@ -396,6 +424,11 @@ protected void onRestoreInstanceState(final Parcelable state) { mIsMarkerEnabled = bundle.getBoolean(INSTANCE_STATE_MARKER_VISIBLE); + final ThumbShape thumbShape = ThumbShape.values()[bundle.getInt(INSTANCE_STATE_THUMB_TYPE)]; + if (thumbShape != mThumbShape) { + setThumbShape(thumbShape); + } + super.onRestoreInstanceState(bundle.getParcelable(INSTANCE_STATE_SAVEDSTATE)); return; } @@ -413,6 +446,7 @@ protected Parcelable onSaveInstanceState() { bundle.putInt(INSTANCE_STATE_PROGRESS_BACKGROUND_COLOR, mProgressBackgroundColor); bundle.putBoolean(INSTANCE_STATE_THUMB_VISIBLE, mIsThumbEnabled); bundle.putBoolean(INSTANCE_STATE_MARKER_VISIBLE, mIsMarkerEnabled); + bundle.putInt(INSTANCE_STATE_THUMB_TYPE, mThumbShape.ordinal()); return bundle; } @@ -460,6 +494,15 @@ public boolean isThumbEnabled() { return mIsThumbEnabled; } + /** + * Gets the thumb shape. + * + * @return the shape of the thumb + */ + public ThumbShape getThumbShape() { + return mThumbShape; + } + /** * Sets the marker enabled. * @@ -540,6 +583,17 @@ public void setThumbEnabled(final boolean enabled) { mIsThumbEnabled = enabled; } + /** + * sets the shape of the thumb + * + * @param shape shape of the thumb + */ + public void setThumbShape(ThumbShape shape) { + mThumbShape = shape; + if (mIsThumbEnabled) + invalidate(); + } + /** * Sets the wheel size. * @@ -657,8 +711,6 @@ private void updateProgressColor() { mThumbColorPaint.setColor(mProgressColor); mThumbColorPaint.setStyle(Paint.Style.FILL_AND_STROKE); mThumbColorPaint.setStrokeWidth(mCircleStrokeWidth); - invalidate(); } - }