Skip to content
This repository was archived by the owner on Apr 20, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions library/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
<attr name="progress_background_color" format="color" />
<attr name="thumb_visible" format="boolean" />
<attr name="marker_visible" format="boolean" />
<attr name="thumb_shape" format="enum">
<enum name="square" value="0" />
<enum name="circle" value="1" />
</attr>
<attr name="android:gravity" />
</declare-styleable>

Expand Down
2 changes: 2 additions & 0 deletions library/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<item name="progress_background_color">#ffffffff</item>
<item name="marker_visible">true</item>
<item name="thumb_visible">true</item>
<item name="thumb_shape">square</item>
</style>

<style name="CircularProgressBarLight">
Expand All @@ -20,5 +21,6 @@
<item name="progress_background_color">#aaa</item>
<item name="marker_visible">true</item>
<item name="thumb_visible">true</item>
<item name="thumb_shape">square</item>
</style>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@
*/
public class HoloCircularProgressBar extends View {

/**
* Types of thumb shape
*/
public enum ThumbShape {
Square, Circle
}

/**
* TAG constant for logging
*/
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}

Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -657,8 +711,6 @@ private void updateProgressColor() {
mThumbColorPaint.setColor(mProgressColor);
mThumbColorPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mThumbColorPaint.setStrokeWidth(mCircleStrokeWidth);

invalidate();
}

}