Skip to content

Commit

Permalink
Added customizable Start and End line to the Bar.
Browse files Browse the repository at this point in the history
  • Loading branch information
zeferino jorge authored and zeferino jorge committed Jul 11, 2016
1 parent 4d3a7c5 commit 3f0bbba
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package at.grabner.circleprogress;

/**
* Created by jzeferino on 07.11.2016.
*/
public enum BarStartEndLine {
/**
* No lines
*/
NONE, /**
* Show Start line in Bar
*/
START,
/**
* Show End line in Bar
*/
END,
/**
* Show both lines, start and end
*/
BOTH
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ public class CircleProgressView extends View {
private int mStartAngle = 270;
private float mOuterContourSize = 1;
private float mInnerContourSize = 1;

// Bar Line width and type
private int mBarStartEndLineWidth = 0;
private BarStartEndLine mBarStartEndLine = BarStartEndLine.NONE;
private int mBarStartEndLineColor = 0xAA000000;
//Default text sizes
private int mUnitTextSize = 10;
private int mTextSize = 10;
Expand All @@ -132,6 +137,7 @@ public class CircleProgressView extends View {
//Paints
private Paint mBarPaint = new Paint();
private Paint mBarSpinnerPaint = new Paint();
private Paint mBarStartEndLinePaint = new Paint();
private Paint mBackgroundCirclePaint = new Paint();
private Paint mRimPaint = new Paint();
private Paint mTextPaint = new Paint();
Expand Down Expand Up @@ -165,7 +171,6 @@ public class CircleProgressView extends View {
private float mBlockDegree = 360 / mBlockCount;
private float mBlockScaleDegree = mBlockDegree * mBlockScale;


private int mTouchEventCount;
private OnProgressChangedListener onProgressChangedListener;
private float previousProgressChangedValue;
Expand Down Expand Up @@ -274,6 +279,20 @@ public static double calcRotationAngleInDegrees(PointF centerPt, PointF targetPt

//----------------------------------
//region getter/setter
public BarStartEndLine getBarStartEndLine() {
return mBarStartEndLine;
}

/**
* @param barWidth The width of the progress bar in pixel.
* @param _barStartEndLine The type of the line in the Bar.
*/
public void setBarStartEndLine(int barWidth, BarStartEndLine _barStartEndLine, @ColorInt int _lineColor) {
mBarStartEndLineWidth = barWidth;
mBarStartEndLine = _barStartEndLine;
mBarStartEndLineColor = _lineColor;
}

public int[] getBarColors() {
return mBarColors;
}
Expand Down Expand Up @@ -903,6 +922,12 @@ private void parseAttributes(TypedArray a) {
setBarStrokeCap(StrokeCap.values()[a.getInt(R.styleable.CircleProgressView_cpv_barStrokeCap, 0)].paintCap);
}

if (a.hasValue(R.styleable.CircleProgressView_cpv_barStartEndLineWidth) && a.hasValue(R.styleable.CircleProgressView_cpv_barStartEndLine)) {
setBarStartEndLine((int) a.getDimension(R.styleable.CircleProgressView_cpv_barStartEndLineWidth, 0),
BarStartEndLine.values()[a.getInt(R.styleable.CircleProgressView_cpv_barStartEndLine, 3)],
a.getColor(R.styleable.CircleProgressView_cpv_barStartEndLineColor, mBarStartEndLineColor));
}

setSpinBarColor(a.getColor(R.styleable.CircleProgressView_cpv_spinColor, mSpinnerColor));
setSpinningBarLength(a.getFloat(R.styleable.CircleProgressView_cpv_spinBarLength,
mSpinningBarLengthOrig));
Expand Down Expand Up @@ -969,7 +994,6 @@ private void parseAttributes(TypedArray a) {
setBlockScale(a.getFloat(R.styleable.CircleProgressView_cpv_blockScale, 0.9f));
}


if (a.hasValue(R.styleable.CircleProgressView_cpv_textTypeface)) {
try {
textTypeface = Typeface.createFromAsset(getContext().getAssets(), a.getString(R.styleable.CircleProgressView_cpv_textTypeface));
Expand Down Expand Up @@ -997,7 +1021,6 @@ private void parseAttributes(TypedArray a) {
}
}


// Recycle
a.recycle();
}
Expand Down Expand Up @@ -1381,6 +1404,14 @@ public void setupPaints() {
setupTextPaint();
setupBackgroundCirclePaint();
setupRimPaint();
setupBarStartEndLinePaint();
}

private void setupBarStartEndLinePaint() {
mBarStartEndLinePaint.setColor(mBarStartEndLineColor);
mBarStartEndLinePaint.setAntiAlias(true);
mBarStartEndLinePaint.setStyle(Style.STROKE);
mBarStartEndLinePaint.setStrokeWidth(mBarStartEndLineWidth);
}

private void setupOuterContourPaint() {
Expand Down Expand Up @@ -1503,18 +1534,51 @@ protected void onDraw(Canvas canvas) {
drawTextWithUnit(canvas);
}


if (mClippingBitmap != null) {
canvas.drawBitmap(mClippingBitmap, 0, 0, mMaskPaint);
}

if (mBarStartEndLineWidth > 0 && mBarStartEndLine != BarStartEndLine.NONE) {
drawStartEndLine(canvas, degrees);
}

}

private void drawStartEndLine(Canvas _canvas, float _degrees) {

// Signal to adjust ccw or cw
int signal = mDirection == Direction.CW ? 1 : -1;

int radius = (mLayoutWidth / 2);

float yFactor = (float) Math.cos(Math.toRadians(_degrees));
float xFactor = (float) (signal * Math.sin(Math.toRadians(_degrees)));

float yInitial = radius - yFactor * (radius - mBarWidth);
float xInitial = radius + xFactor * (radius - mBarWidth);

float yFinal = radius - yFactor * radius;
float xFinal = radius + xFactor * radius;

switch (mBarStartEndLine) {
case START:
// NOTE: We are drawing the start line always, could be optimized.
_canvas.drawLine(radius, 0, radius, mBarWidth, mBarStartEndLinePaint);
break;
case END:
_canvas.drawLine(xInitial, yInitial, xFinal, yFinal, mBarStartEndLinePaint);
break;
case BOTH:
_canvas.drawLine(radius, 0, radius, mBarWidth, mBarStartEndLinePaint);
_canvas.drawLine(xInitial, yInitial, xFinal, yFinal, mBarStartEndLinePaint);
break;
}
}

private void drawDebug(Canvas canvas) {
Paint innerRectPaint = new Paint();
innerRectPaint.setColor(Color.YELLOW);
canvas.drawRect(mCircleBounds, innerRectPaint);

}

private void drawBlocks(Canvas _canvas, RectF circleBounds, float startAngle, float _degrees, boolean userCenter, Paint paint) {
Expand Down Expand Up @@ -1616,7 +1680,6 @@ private void drawTextWithUnit(Canvas canvas) {
}
}


if (DEBUG) {
Paint rectPaint = new Paint();
rectPaint.setColor(Color.MAGENTA);
Expand All @@ -1630,7 +1693,6 @@ private void drawTextWithUnit(Canvas canvas) {

if (mShowUnit) {


if (mIsAutoColorEnabled) {
mUnitTextPaint.setColor(calcTextColor(mCurrentValue));
}
Expand All @@ -1642,8 +1704,6 @@ private void drawTextWithUnit(Canvas canvas) {
} else {
setUnitTextBoundsAndSizeWithFixedTextSize(unitGapWidthHalf * 2f, unitGapHeightHalf * 2f);
}


}

if (DEBUG) {
Expand All @@ -1663,7 +1723,6 @@ private void drawBar(Canvas _canvas, float _degrees) {
} else {
drawBlocks(_canvas, mCircleBounds, startAngle, _degrees, false, mBarPaint);
}

}

//endregion draw
Expand Down
8 changes: 8 additions & 0 deletions CircleProgressView/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@
<enum name="Round" value="1" />
<enum name="Square" value="2" />
</attr>
<attr name="cpv_barStartEndLineWidth" format="dimension" />
<attr name="cpv_barStartEndLineColor" format="color" />
<attr name="cpv_barStartEndLine" format="enum">
<enum name="None" value="0" />
<enum name="Start" value="1" />
<enum name="End" value="2" />
<enum name="Both" value="3" />
</attr>

<!-- Spinner-->
<attr name="cpv_spinColor" format="color" />
Expand Down

0 comments on commit 3f0bbba

Please sign in to comment.