From 1eac75b7ceda985841cf50d0fc7e1413a0b76a85 Mon Sep 17 00:00:00 2001 From: laaptu Date: Wed, 4 Mar 2015 12:56:08 +0545 Subject: [PATCH] Animating Stroke Changes * Added mAnimateStroke, mStrokeRectF for animating stroke * Modified onDraw() to incorporate stroke animation --- .../ProgressPieView/ProgressPieView.java | 97 ++++++++++++++++--- library/src/main/res/values/attrs.xml | 41 ++++---- .../ProgressPieView/sample/MainActivity.java | 21 ++++ sample/src/main/res/layout/main_activity.xml | 25 +++-- 4 files changed, 144 insertions(+), 40 deletions(-) diff --git a/library/src/main/java/com/filippudak/ProgressPieView/ProgressPieView.java b/library/src/main/java/com/filippudak/ProgressPieView/ProgressPieView.java index 55f7e31..cea9ab3 100644 --- a/library/src/main/java/com/filippudak/ProgressPieView/ProgressPieView.java +++ b/library/src/main/java/com/filippudak/ProgressPieView/ProgressPieView.java @@ -18,10 +18,31 @@ import android.support.v4.util.LruCache; import android.view.View; +/** + * Customization includes + *
    + *
  1. Adding a boolean value mAnimateStroke to animate stroke or not
  2. + *
  3. If mShowStroke=false, mAnimateStroke=false
  4. + *
  5. mStrokeRectF is the main RectF responsible for drawing and animating stroke
  6. + *
  7. mStrokePaint is the main color for mStrokeRectF
  8. + *
  9. mStrokeRectF drawing is only possible for FILL_TYPE_RADIAL
  10. + *
  11. onDraw() method mStrokeRectF have same dimension as that of mInnerRectF
  12. + *
+ * Customization lines + *
    + *
  1. 100-101
  2. + *
  3. 151-153
  4. + *
  5. 179-180
  6. + *
  7. 206-210
  8. + *
  9. 230-231
  10. + *
  11. 270-271
  12. + *
+ */ public class ProgressPieView extends View { public interface OnProgressListener { public void onProgressChanged(int progress, int max); + public void onProgressCompleted(); } @@ -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; @@ -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); } @@ -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); @@ -144,6 +174,12 @@ private void init(Context context, AttributeSet attrs) { mInnerRectF = new RectF(); mImageRect = new Rect(); + + //Customization + if (mAnimateStroke) { + mStrokeRectF = new RectF(); + } + } @Override @@ -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(); @@ -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); @@ -218,7 +266,8 @@ protected void onDraw(Canvas canvas) { mImage.draw(canvas); } - if (mShowStroke) { + //Customization + if (mShowStroke && !mAnimateStroke) { canvas.drawOval(mInnerRectF, mStrokePaint); } @@ -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; } @@ -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) { @@ -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) { @@ -341,6 +392,7 @@ public boolean isInverted() { /** * Sets the inverted state. + * * @param inverted draw the progress inverted or not */ public void setInverted(boolean inverted) { @@ -356,6 +408,7 @@ public boolean isCounterclockwise() { /** * Sets the counterclockwise state. + * * @param counterclockwise draw the progress counterclockwise or not */ public void setCounterclockwise(boolean counterclockwise) { @@ -370,8 +423,9 @@ 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); @@ -379,7 +433,7 @@ public void setProgressColor(int color) { } /** - * 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(); @@ -387,6 +441,7 @@ public int getBackgroundColor() { /** * 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) { @@ -395,7 +450,7 @@ 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(); @@ -403,6 +458,7 @@ public int getTextColor() { /** * 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) { @@ -419,6 +475,7 @@ public float getTextSize() { /** * Sets the text size. + * * @param sizeSp in sp for the text */ public void setTextSize(int sizeSp) { @@ -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) { @@ -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) { @@ -469,6 +528,7 @@ public boolean isTextShowing() { /** * Sets the show text state. + * * @param showText show or hide text */ public void setShowText(boolean showText) { @@ -477,7 +537,7 @@ 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(); @@ -485,6 +545,7 @@ public int getStrokeColor() { /** * 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) { @@ -501,6 +562,7 @@ public float getStrokeWidth() { /** * Set the stroke width. + * * @param widthDp in dp for the pie border */ public void setStrokeWidth(int widthDp) { @@ -518,6 +580,7 @@ public boolean isStrokeShowing() { /** * Sets the show stroke state. + * * @param showStroke show or hide stroke */ public void setShowStroke(boolean showStroke) { @@ -534,6 +597,7 @@ public Drawable getImageDrawable() { /** * Sets the drawable of the view. + * * @param image drawable of the view */ public void setImageDrawable(Drawable image) { @@ -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) { @@ -561,6 +626,7 @@ public boolean isImageShowing() { /** * Sets the show image state. + * * @param showImage show or hide image */ public void setShowImage(boolean showImage) { @@ -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) { @@ -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) { diff --git a/library/src/main/res/values/attrs.xml b/library/src/main/res/values/attrs.xml index bb34fae..9f8e4f6 100644 --- a/library/src/main/res/values/attrs.xml +++ b/library/src/main/res/values/attrs.xml @@ -1,25 +1,28 @@ - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/sample/src/main/java/com/filippudak/ProgressPieView/sample/MainActivity.java b/sample/src/main/java/com/filippudak/ProgressPieView/sample/MainActivity.java index 520bca1..e5876e8 100644 --- a/sample/src/main/java/com/filippudak/ProgressPieView/sample/MainActivity.java +++ b/sample/src/main/java/com/filippudak/ProgressPieView/sample/MainActivity.java @@ -11,6 +11,16 @@ import com.filippudak.ProgressPieView.ProgressPieView; + +/** + * Customization lines + *
    + *
  1. 34
  2. + *
  3. 90
  4. + *
  5. 102
  6. + *
  7. 137-138
  8. + *
+ */ public class MainActivity extends ActionBarActivity { private static final int SIZE = 96; @@ -21,6 +31,8 @@ public class MainActivity extends ActionBarActivity { private ProgressPieView mProgressPieViewInverted; private ProgressPieView mProgressPieViewXml; private ProgressPieView mProgressPieViewCode; + //Customization + private ProgressPieView mProgressPieViewAnimateStroke; @Override protected void onCreate(Bundle savedInstanceState) { @@ -75,6 +87,10 @@ public void onProgressCompleted() { // XML version mProgressPieViewXml = (ProgressPieView) findViewById(R.id.progressPieViewXml); + //Customization + //Animate Stroke + mProgressPieViewAnimateStroke = (ProgressPieView) findViewById(R.id.progressPieViewStrokeAnimate); + // SeekBar mSeekBar = (SeekBar) findViewById(R.id.seekbar); mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @@ -85,6 +101,8 @@ public void onProgressChanged(SeekBar seekBar, int i, boolean b) { mProgressPieViewInverted.setProgress(i); mProgressPieViewXml.setProgress(i); mProgressPieViewCode.setProgress(i); + //Customization + mProgressPieViewAnimateStroke.setProgress(i); } @Override @@ -116,6 +134,9 @@ public boolean onOptionsItemSelected(MenuItem item) { mProgressPieViewXml.animateProgressFill(); mProgressPieViewCode.setProgress(0); mProgressPieViewCode.animateProgressFill(); + //Customization + mProgressPieViewAnimateStroke.setProgress(0); + mProgressPieViewAnimateStroke.animateProgressFill(); } return super.onOptionsItemSelected(item); } diff --git a/sample/src/main/res/layout/main_activity.xml b/sample/src/main/res/layout/main_activity.xml index e65b457..e28bd8a 100644 --- a/sample/src/main/res/layout/main_activity.xml +++ b/sample/src/main/res/layout/main_activity.xml @@ -1,7 +1,6 @@ - + android:max="100" /> + android:id="@+id/progressPieView" /> + ppv:ppvCounterclockwise="true" /> + ppv:ppvProgressColor="@color/holo_red_light" /> + + + + \ No newline at end of file