Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,31 @@
import android.support.v4.util.LruCache;
import android.view.View;

/**
* Customization includes
* <ol>
* <li>Adding a boolean value mAnimateStroke to animate stroke or not</li>
* <li>If mShowStroke=false, mAnimateStroke=false</li>
* <li>mStrokeRectF is the main RectF responsible for drawing and animating stroke</li>
* <li>mStrokePaint is the main color for mStrokeRectF</li>
* <li>mStrokeRectF drawing is only possible for FILL_TYPE_RADIAL</li>
* <li>onDraw() method mStrokeRectF have same dimension as that of mInnerRectF</li>
* </ol>
* Customization lines
* <ol>
* <li>100-101</li>
* <li>151-153</li>
* <li>179-180</li>
* <li>206-210</li>
* <li>230-231</li>
* <li>270-271</li>
* </ol>
*/
public class ProgressPieView extends View {

public interface OnProgressListener {
public void onProgressChanged(int progress, int max);

public void onProgressCompleted();
}

Expand All @@ -34,7 +55,7 @@ public interface OnProgressListener {
*/
public static final int FILL_TYPE_CENTER = 1;

public static final int SLOW_ANIMATION_SPEED = 50;
public static final int SLOW_ANIMATION_SPEED = 50;
public static final int MEDIUM_ANIMATION_SPEED = 25;
public static final int FAST_ANIMATION_SPEED = 1;

Expand Down Expand Up @@ -69,12 +90,16 @@ public interface OnProgressListener {
private Paint mBackgroundPaint;
private RectF mInnerRectF;
private int mProgressFillType = FILL_TYPE_RADIAL;
private int mAnimationSpeed = MEDIUM_ANIMATION_SPEED;

private int mAnimationSpeed = MEDIUM_ANIMATION_SPEED;
private AnimationHandler mAnimationHandler = new AnimationHandler();

private int mViewSize;

//Customization Values
private RectF mStrokeRectF;
private boolean mAnimateStroke = false;

public ProgressPieView(Context context) {
this(context, null);
}
Expand Down Expand Up @@ -122,6 +147,11 @@ private void init(Context context, AttributeSet attrs) {

mProgressFillType = a.getInteger(R.styleable.ProgressPieView_ppvProgressFillType, mProgressFillType);

//Customization
mAnimateStroke = a.getBoolean(R.styleable.ProgressPieView_ppvAnimateStroke, mAnimateStroke);
if (!mShowStroke)
mAnimateStroke = false;

a.recycle();

mBackgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
Expand All @@ -144,6 +174,12 @@ private void init(Context context, AttributeSet attrs) {

mInnerRectF = new RectF();
mImageRect = new Rect();

//Customization
if (mAnimateStroke) {
mStrokeRectF = new RectF();
}

}

@Override
Expand All @@ -162,10 +198,19 @@ protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mInnerRectF.set(0, 0, mViewSize, mViewSize);
mInnerRectF.offset((getWidth() - mViewSize) / 2, (getHeight() - mViewSize) / 2);

if (mShowStroke) {
final int halfBorder = (int) (mStrokePaint.getStrokeWidth() / 2f + 0.5f);
mInnerRectF.inset(halfBorder, halfBorder);
//Customization
if (mAnimateStroke) {
mStrokeRectF.set(0, 0, mViewSize, mViewSize);
mStrokeRectF.offset((getWidth() - mViewSize) / 2, (getHeight() - mViewSize) / 2);
mStrokeRectF.inset(halfBorder, halfBorder);
}
}


float centerX = mInnerRectF.centerX();
float centerY = mInnerRectF.centerY();

Expand All @@ -181,6 +226,9 @@ protected void onDraw(Canvas canvas) {
sweepAngle = -sweepAngle;
}
canvas.drawArc(mInnerRectF, mStartAngle, sweepAngle, true, mProgressPaint);
//Customization: Drawing/animating stroke
if (mAnimateStroke)
canvas.drawArc(mStrokeRectF, mStartAngle, sweepAngle, false, mStrokePaint);
break;
case FILL_TYPE_CENTER:
float radius = (mViewSize / 2) * ((float) mProgress / mMax);
Expand Down Expand Up @@ -218,7 +266,8 @@ protected void onDraw(Canvas canvas) {
mImage.draw(canvas);
}

if (mShowStroke) {
//Customization
if (mShowStroke && !mAnimateStroke) {
canvas.drawOval(mInnerRectF, mStrokePaint);
}

Expand All @@ -242,18 +291,18 @@ public void setMax(int max) {
mMax = max;
invalidate();
}
/**

/**
* Sets the animation speed used in the animateProgressFill method.
*/
public void setAnimationSpeed(int animationSpeed){
public void setAnimationSpeed(int animationSpeed) {
this.mAnimationSpeed = animationSpeed;
}

/**
* Returns the current animation speed used in animateProgressFill method.
*/
public int getAnimationSpeed(){
public int getAnimationSpeed() {
return this.mAnimationSpeed;
}

Expand All @@ -269,6 +318,7 @@ public void animateProgressFill() {

/**
* Animates a progress fill of the view, using a Handler.
*
* @param animateTo - the progress value the animation should stop at (0 - MAX)
*/
public void animateProgressFill(int animateTo) {
Expand Down Expand Up @@ -326,6 +376,7 @@ public int getStartAngle() {

/**
* Sets the start angle the {@link #FILL_TYPE_RADIAL} uses.
*
* @param startAngle start angle in degrees
*/
public void setStartAngle(int startAngle) {
Expand All @@ -341,6 +392,7 @@ public boolean isInverted() {

/**
* Sets the inverted state.
*
* @param inverted draw the progress inverted or not
*/
public void setInverted(boolean inverted) {
Expand All @@ -356,6 +408,7 @@ public boolean isCounterclockwise() {

/**
* Sets the counterclockwise state.
*
* @param counterclockwise draw the progress counterclockwise or not
*/
public void setCounterclockwise(boolean counterclockwise) {
Expand All @@ -370,23 +423,25 @@ public int getProgressColor() {
}

/**
* Sets the color used to display the progress of the view.
* @param color - color of the progress part of the view
* Sets the color used to display the progress of the view.
*
* @param color - color of the progress part of the view
*/
public void setProgressColor(int color) {
mProgressPaint.setColor(color);
invalidate();
}

/**
* Gets the color used to display the background of the view.
* Gets the color used to display the background of the view.
*/
public int getBackgroundColor() {
return mBackgroundPaint.getColor();
}

/**
* Sets the color used to display the background of the view.
*
* @param color - color of the background part of the view
*/
public void setBackgroundColor(int color) {
Expand All @@ -395,14 +450,15 @@ public void setBackgroundColor(int color) {
}

/**
* Gets the color used to display the text of the view.
* Gets the color used to display the text of the view.
*/
public int getTextColor() {
return mTextPaint.getColor();
}

/**
* Sets the color used to display the text of the view.
*
* @param color - color of the text part of the view
*/
public void setTextColor(int color) {
Expand All @@ -419,6 +475,7 @@ public float getTextSize() {

/**
* Sets the text size.
*
* @param sizeSp in sp for the text
*/
public void setTextSize(int sizeSp) {
Expand All @@ -436,6 +493,7 @@ public String getText() {

/**
* Sets the text of the view.
*
* @param text to be displayed in the view
*/
public void setText(String text) {
Expand All @@ -452,7 +510,8 @@ public String getTypeface() {

/**
* Sets the text typeface.
* - i.e. fonts/Roboto/Roboto-Regular.ttf
* - i.e. fonts/Roboto/Roboto-Regular.ttf
*
* @param typeface that the text is displayed in
*/
public void setTypeface(String typeface) {
Expand All @@ -469,6 +528,7 @@ public boolean isTextShowing() {

/**
* Sets the show text state.
*
* @param showText show or hide text
*/
public void setShowText(boolean showText) {
Expand All @@ -477,14 +537,15 @@ public void setShowText(boolean showText) {
}

/**
* Get the color used to display the stroke of the view.
* Get the color used to display the stroke of the view.
*/
public int getStrokeColor() {
return mStrokePaint.getColor();
}

/**
* Sets the color used to display the stroke of the view.
*
* @param color - color of the stroke part of the view
*/
public void setStrokeColor(int color) {
Expand All @@ -501,6 +562,7 @@ public float getStrokeWidth() {

/**
* Set the stroke width.
*
* @param widthDp in dp for the pie border
*/
public void setStrokeWidth(int widthDp) {
Expand All @@ -518,6 +580,7 @@ public boolean isStrokeShowing() {

/**
* Sets the show stroke state.
*
* @param showStroke show or hide stroke
*/
public void setShowStroke(boolean showStroke) {
Expand All @@ -534,6 +597,7 @@ public Drawable getImageDrawable() {

/**
* Sets the drawable of the view.
*
* @param image drawable of the view
*/
public void setImageDrawable(Drawable image) {
Expand All @@ -543,6 +607,7 @@ public void setImageDrawable(Drawable image) {

/**
* Sets the drawable of the view.
*
* @param resId resource id of the view's drawable
*/
public void setImageResource(int resId) {
Expand All @@ -561,6 +626,7 @@ public boolean isImageShowing() {

/**
* Sets the show image state.
*
* @param showImage show or hide image
*/
public void setShowImage(boolean showImage) {
Expand All @@ -577,6 +643,7 @@ public int getProgressFillType() {

/**
* Sets the progress fill type.
*
* @param fillType one of {@link #FILL_TYPE_CENTER}, {@link #FILL_TYPE_RADIAL}
*/
public void setProgressFillType(int fillType) {
Expand All @@ -585,8 +652,8 @@ public void setProgressFillType(int fillType) {

/**
* Sets the progress listner.
* @param listener progress listener
*
* @param listener progress listener
* @see com.filippudak.ProgressPieView.ProgressPieView.OnProgressListener
*/
public void setOnProgressListener(OnProgressListener listener) {
Expand Down
41 changes: 22 additions & 19 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ProgressPieView">
<attr name="android:text"/>
<attr name="android:textSize"/>
<attr name="android:textColor"/>
<attr name="ppvProgress" format="integer"/>
<attr name="ppvMax" format="integer"/>
<attr name="ppvStartAngle" format="integer"/>
<attr name="ppvInverted" format="boolean"/>
<attr name="ppvCounterclockwise" format="boolean"/>
<attr name="ppvStrokeWidth" format="dimension"/>
<attr name="ppvBackgroundColor" format="reference|color"/>
<attr name="ppvProgressColor" format="reference|color"/>
<attr name="ppvStrokeColor" format="reference|color"/>
<attr name="ppvShowStroke" format="boolean"/>
<attr name="ppvShowText" format="boolean"/>
<attr name="ppvTypeface" format="string"/>
<attr name="ppvImage" format="reference"/>
<attr name="ppvProgressFillType" format="enum" >
<enum name="radial" value="0"/>
<enum name="center" value="1"/>
<attr name="android:text" />
<attr name="android:textSize" />
<attr name="android:textColor" />
<attr name="ppvProgress" format="integer" />
<attr name="ppvMax" format="integer" />
<attr name="ppvStartAngle" format="integer" />
<attr name="ppvInverted" format="boolean" />
<attr name="ppvCounterclockwise" format="boolean" />
<attr name="ppvStrokeWidth" format="dimension" />
<attr name="ppvBackgroundColor" format="reference|color" />
<attr name="ppvProgressColor" format="reference|color" />
<attr name="ppvStrokeColor" format="reference|color" />
<attr name="ppvShowStroke" format="boolean" />
<attr name="ppvShowText" format="boolean" />
<attr name="ppvTypeface" format="string" />
<attr name="ppvImage" format="reference" />
<attr name="ppvProgressFillType" format="enum">
<enum name="radial" value="0" />
<enum name="center" value="1" />
</attr>

<!--Animate Stroke -->
<attr name="ppvAnimateStroke" format="boolean" />
</declare-styleable>
</resources>
Loading