Skip to content

Commit

Permalink
Add cpv_direction attribute for specifying the direction of circular …
Browse files Browse the repository at this point in the history
…rotation.
  • Loading branch information
6bangs committed Jul 8, 2016
1 parent 82f70f4 commit 712c5cb
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public class CircleProgressView extends View {
//region members

//value animation
Direction mDirection = Direction.CW;
float mCurrentValue = 42;
float mValueTo = 0;
float mValueFrom = 0;
Expand Down Expand Up @@ -656,6 +657,13 @@ public void setUnitToTextScale(@FloatRange(from = 0.0) float _relativeUniteSize)
triggerReCalcTextSizesAndPositions();
}

/**
* Sets the direction of circular motion (clockwise or counter-clockwise).
*/
public void setDirection(Direction direction) {
mDirection = direction;
}

/**
* Set the value of the circle view without an animation.
* Stops any currently active animations.
Expand Down Expand Up @@ -779,6 +787,8 @@ private void parseAttributes(TypedArray a) {
setSpinSpeed((int) a.getFloat(R.styleable.CircleProgressView_cpv_spinSpeed,
mSpinSpeed));

setDirection(Direction.values()[a.getInt(R.styleable.CircleProgressView_cpv_direction, 0)]);

float value = a.getFloat(R.styleable.CircleProgressView_cpv_value, mCurrentValue);
setValue(value);
mCurrentValue = value;
Expand Down Expand Up @@ -1484,14 +1494,19 @@ private void drawBlocks(Canvas _canvas, RectF circleBounds, float startAngle, fl
}

private void drawSpinner(Canvas canvas) {

if (mSpinningBarLengthCurrent < 0) {
mSpinningBarLengthCurrent = 1;
}
float startAngle = (mStartAngle + mCurrentSpinnerDegreeValue - mSpinningBarLengthCurrent);

float startAngle;
if (mDirection == Direction.CW) {
startAngle = mStartAngle + mCurrentSpinnerDegreeValue - mSpinningBarLengthCurrent;
} else {
startAngle = mStartAngle - mCurrentSpinnerDegreeValue;
}

canvas.drawArc(mCircleBounds, startAngle, mSpinningBarLengthCurrent, false,
mBarSpinnerPaint);

}

private void drawTextWithUnit(Canvas canvas) {
Expand Down Expand Up @@ -1610,10 +1625,11 @@ private void drawTextWithUnit(Canvas canvas) {
}

private void drawBar(Canvas _canvas, float _degrees) {
float startAngle = mDirection == Direction.CW ? mStartAngle : mStartAngle - _degrees;
if (!mShowBlock) {
_canvas.drawArc(mCircleBounds, mStartAngle, _degrees, false, mBarPaint);
_canvas.drawArc(mCircleBounds, startAngle, _degrees, false, mBarPaint);
} else {
drawBlocks(_canvas, mCircleBounds, mStartAngle, _degrees, false, mBarPaint);
drawBlocks(_canvas, mCircleBounds, startAngle, _degrees, false, mBarPaint);
}

}
Expand Down Expand Up @@ -1657,15 +1673,15 @@ public boolean onTouchEvent(@NonNull MotionEvent event) {
case MotionEvent.ACTION_UP: {
mTouchEventCount = 0;
PointF point = new PointF(event.getX(), event.getY());
float angle = normalizeAngle(Math.round(calcRotationAngleInDegrees(mCenter, point) - mStartAngle));
float angle = getRotationAngleForPointFromStart(point);
setValueAnimated(mMaxValue / 360f * angle, 800);
return true;
}
case MotionEvent.ACTION_MOVE: {
mTouchEventCount++;
if (mTouchEventCount > 5) { //touch/move guard
PointF point = new PointF(event.getX(), event.getY());
float angle = normalizeAngle(Math.round(calcRotationAngleInDegrees(mCenter, point) - mStartAngle));
float angle = getRotationAngleForPointFromStart(point);
setValue(mMaxValue / 360f * angle);
return true;
} else {
Expand All @@ -1682,6 +1698,12 @@ public boolean onTouchEvent(@NonNull MotionEvent event) {
return super.onTouchEvent(event);
}

private float getRotationAngleForPointFromStart(PointF point) {
long angle = Math.round(calcRotationAngleInDegrees(mCenter, point));
float fromStart = mDirection == Direction.CW ? angle - mStartAngle : mStartAngle - angle;
return normalizeAngle(fromStart);
}


//endregion touch input
//----------------------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package at.grabner.circleprogress;

public enum Direction {
/**
* The bar grows clockwise from the start angle, and the spinner rotates clockwise.
*/
CW,

/**
* The bar grows counter-clockwise from the start angle, and the spinner rotates
* counter-clockwise.
*/
CCW
}
4 changes: 4 additions & 0 deletions CircleProgressView/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@


<!-- Progress Bar -->
<attr name="cpv_direction" format="enum">
<enum name="CW" value="0"/>
<enum name="CCW" value="1"/>
</attr>
<attr name="cpv_value" format="float"/>
<attr name="cpv_maxValue" format="float"/>
<attr name="cpv_barWidth" format="dimension"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public void onProgressChanged(float value) {
// mCircleView.setValue(0);
// mCircleView.setValueAnimated(24);

//growing/rotating counter-clockwise
// mCircleView.setDirection(Direction.CCW)

// //show unit
// mCircleView.setUnit("%");
// mCircleView.setUnitVisible(mShowUnit);
Expand Down

0 comments on commit 712c5cb

Please sign in to comment.