|
| 1 | +/* |
| 2 | + * Copyright 2013 Leon Cheng |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.lylc.widget.circularprogressbar; |
| 18 | + |
| 19 | +import android.animation.Animator; |
| 20 | +import android.animation.Animator.AnimatorListener; |
| 21 | +import android.animation.ObjectAnimator; |
| 22 | +import android.animation.ValueAnimator; |
| 23 | +import android.animation.ValueAnimator.AnimatorUpdateListener; |
| 24 | +import android.content.Context; |
| 25 | +import android.content.res.Resources; |
| 26 | +import android.content.res.TypedArray; |
| 27 | +import android.graphics.Canvas; |
| 28 | +import android.graphics.Color; |
| 29 | +import android.graphics.Paint; |
| 30 | +import android.graphics.Paint.Style; |
| 31 | +import android.graphics.RectF; |
| 32 | +import android.graphics.Typeface; |
| 33 | +import android.text.TextUtils; |
| 34 | +import android.util.AttributeSet; |
| 35 | +import android.view.View; |
| 36 | +import android.view.animation.LinearInterpolator; |
| 37 | +import android.widget.ProgressBar; |
| 38 | +import net.gnu.explorer.R; |
| 39 | + |
| 40 | +public class CircularProgressBar extends ProgressBar{ |
| 41 | + private static final String TAG = "CircularProgressBar"; |
| 42 | + |
| 43 | + private static final int STROKE_WIDTH = 20; |
| 44 | + |
| 45 | + private String mTitle = ""; |
| 46 | + private String mSubTitle = ""; |
| 47 | + |
| 48 | + private int mStrokeWidth = STROKE_WIDTH; |
| 49 | + |
| 50 | + private final RectF mCircleBounds = new RectF(); |
| 51 | + |
| 52 | + private final Paint mProgressColorPaint = new Paint(); |
| 53 | + private final Paint mBackgroundColorPaint = new Paint(); |
| 54 | + private final Paint mTitlePaint = new Paint(); |
| 55 | + private final Paint mSubtitlePaint = new Paint(); |
| 56 | + |
| 57 | + private boolean mHasShadow = true; |
| 58 | + private int mShadowColor = Color.BLACK; |
| 59 | + |
| 60 | + public interface ProgressAnimationListener{ |
| 61 | + public void onAnimationStart(); |
| 62 | + public void onAnimationFinish(); |
| 63 | + public void onAnimationProgress(int progress); |
| 64 | + } |
| 65 | + |
| 66 | + public CircularProgressBar(Context context) { |
| 67 | + super(context); |
| 68 | + init(null, 0); |
| 69 | + } |
| 70 | + |
| 71 | + public CircularProgressBar(Context context, AttributeSet attrs) { |
| 72 | + super(context, attrs); |
| 73 | + init(attrs, 0); |
| 74 | + } |
| 75 | + |
| 76 | + public CircularProgressBar(Context context, AttributeSet attrs, int defStyle) { |
| 77 | + super(context, attrs, defStyle); |
| 78 | + init(attrs, defStyle); |
| 79 | + } |
| 80 | + |
| 81 | + public void init(AttributeSet attrs, int style){ |
| 82 | + //so that shadow shows up properly for lines and arcs |
| 83 | + setLayerType(View.LAYER_TYPE_SOFTWARE, null); |
| 84 | + |
| 85 | + TypedArray a = getContext().obtainStyledAttributes(attrs, |
| 86 | + R.styleable.CircularProgressBar, style, 0); |
| 87 | + |
| 88 | + String color; |
| 89 | + Resources res = getResources(); |
| 90 | + |
| 91 | + this.mHasShadow = a.getBoolean(R.styleable.CircularProgressBar_cpb_hasShadow, true); |
| 92 | + |
| 93 | + color = a.getString(R.styleable.CircularProgressBar_cpb_progressColor); |
| 94 | + if(color==null) |
| 95 | + mProgressColorPaint.setColor(res.getColor(R.color.circular_progress_default_progress)); |
| 96 | + else |
| 97 | + mProgressColorPaint.setColor(Color.parseColor(color)); |
| 98 | + |
| 99 | + color = a.getString(R.styleable.CircularProgressBar_cpb_backgroundColor); |
| 100 | + if(color==null) |
| 101 | + mBackgroundColorPaint.setColor(res.getColor(R.color.circular_progress_default_background)); |
| 102 | + else |
| 103 | + mBackgroundColorPaint.setColor(Color.parseColor(color)); |
| 104 | + |
| 105 | + color = a.getString(R.styleable.CircularProgressBar_cpb_titleColor); |
| 106 | + if(color==null) |
| 107 | + mTitlePaint.setColor(res.getColor(R.color.circular_progress_default_title)); |
| 108 | + else |
| 109 | + mTitlePaint.setColor(Color.parseColor(color)); |
| 110 | + |
| 111 | + color = a.getString(R.styleable.CircularProgressBar_cpb_subtitleColor); |
| 112 | + if(color==null) |
| 113 | + mSubtitlePaint.setColor(res.getColor(R.color.circular_progress_default_subtitle)); |
| 114 | + else |
| 115 | + mSubtitlePaint.setColor(Color.parseColor(color)); |
| 116 | + |
| 117 | + |
| 118 | + String t = a.getString(R.styleable.CircularProgressBar_cpb_title); |
| 119 | + if(t!=null) |
| 120 | + mTitle = t; |
| 121 | + |
| 122 | + t = a.getString(R.styleable.CircularProgressBar_cpb_subtitle); |
| 123 | + if(t!=null) |
| 124 | + mSubTitle = t; |
| 125 | + |
| 126 | + mStrokeWidth = a.getInt(R.styleable.CircularProgressBar_cpb_strokeWidth, STROKE_WIDTH); |
| 127 | + |
| 128 | + a.recycle(); |
| 129 | + |
| 130 | + |
| 131 | + mProgressColorPaint.setAntiAlias(true); |
| 132 | + mProgressColorPaint.setStyle(Paint.Style.STROKE); |
| 133 | + mProgressColorPaint.setStrokeWidth(mStrokeWidth); |
| 134 | + |
| 135 | + mBackgroundColorPaint.setAntiAlias(true); |
| 136 | + mBackgroundColorPaint.setStyle(Paint.Style.STROKE); |
| 137 | + mBackgroundColorPaint.setStrokeWidth(mStrokeWidth); |
| 138 | + |
| 139 | + mTitlePaint.setTextSize(60); |
| 140 | + mTitlePaint.setStyle(Style.FILL); |
| 141 | + mTitlePaint.setAntiAlias(true); |
| 142 | + mTitlePaint.setTypeface(Typeface.create("Roboto-Thin", Typeface.NORMAL)); |
| 143 | + mTitlePaint.setShadowLayer(0.1f, 0, 1, Color.GRAY); |
| 144 | + |
| 145 | + mSubtitlePaint.setTextSize(30); |
| 146 | + mSubtitlePaint.setStyle(Style.FILL); |
| 147 | + mSubtitlePaint.setAntiAlias(true); |
| 148 | + mSubtitlePaint.setTypeface(Typeface.create("Roboto-Thin", Typeface.BOLD)); |
| 149 | + // mSubtitlePaint.setShadowLayer(0.1f, 0, 1, Color.GRAY); |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + protected synchronized void onDraw(Canvas canvas) { |
| 154 | + canvas.drawArc(mCircleBounds, 0, 360 , false, mBackgroundColorPaint); |
| 155 | + |
| 156 | + int prog = getProgress(); |
| 157 | + float scale = getMax() > 0 ? (float)prog/getMax() *360: 0; |
| 158 | + |
| 159 | + if(mHasShadow) |
| 160 | + mProgressColorPaint.setShadowLayer( 3, 0, 1, mShadowColor); |
| 161 | + canvas.drawArc(mCircleBounds, 270, scale , false, mProgressColorPaint); |
| 162 | + |
| 163 | + |
| 164 | + if(!TextUtils.isEmpty(mTitle)){ |
| 165 | + int xPos = (int)(getMeasuredWidth()/2 - mTitlePaint.measureText(mTitle) / 2); |
| 166 | + int yPos = (int) (getMeasuredHeight()/2); |
| 167 | + |
| 168 | + float titleHeight = Math.abs(mTitlePaint.descent() + mTitlePaint.ascent()); |
| 169 | + if(TextUtils.isEmpty(mSubTitle)){ |
| 170 | + yPos += titleHeight/2; |
| 171 | + } |
| 172 | + canvas.drawText(mTitle, xPos, yPos, mTitlePaint); |
| 173 | + |
| 174 | + yPos += titleHeight; |
| 175 | + xPos = (int)(getMeasuredWidth()/2 - mSubtitlePaint.measureText(mSubTitle) / 2); |
| 176 | + |
| 177 | + canvas.drawText(mSubTitle, xPos, yPos, mSubtitlePaint); |
| 178 | + } |
| 179 | + |
| 180 | + super.onDraw(canvas); |
| 181 | + } |
| 182 | + |
| 183 | + @Override |
| 184 | + protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) { |
| 185 | + final int height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec); |
| 186 | + final int width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec); |
| 187 | + final int min = Math.min(width, height); |
| 188 | + setMeasuredDimension(min+2*STROKE_WIDTH, min+2*STROKE_WIDTH); |
| 189 | + |
| 190 | + mCircleBounds.set(STROKE_WIDTH, STROKE_WIDTH, min+STROKE_WIDTH, min+STROKE_WIDTH); |
| 191 | + } |
| 192 | + |
| 193 | + @Override |
| 194 | + public synchronized void setProgress(int progress) { |
| 195 | + super.setProgress(progress); |
| 196 | + |
| 197 | + // the setProgress super will not change the details of the progress bar |
| 198 | + // anymore so we need to force an update to redraw the progress bar |
| 199 | + invalidate(); |
| 200 | + } |
| 201 | + |
| 202 | + public void animateProgressTo(final int start, final int end, final ProgressAnimationListener listener){ |
| 203 | + if(start!=0) |
| 204 | + setProgress(start); |
| 205 | + |
| 206 | + final ObjectAnimator progressBarAnimator = ObjectAnimator.ofFloat(this, "animateProgress", start, end); |
| 207 | + progressBarAnimator.setDuration(1500); |
| 208 | + // progressBarAnimator.setInterpolator(new AnticipateOvershootInterpolator(2f, 1.5f)); |
| 209 | + progressBarAnimator.setInterpolator(new LinearInterpolator()); |
| 210 | + |
| 211 | + progressBarAnimator.addListener(new AnimatorListener() { |
| 212 | + @Override |
| 213 | + public void onAnimationCancel(final Animator animation) { |
| 214 | + } |
| 215 | + |
| 216 | + @Override |
| 217 | + public void onAnimationEnd(final Animator animation) { |
| 218 | + CircularProgressBar.this.setProgress(end); |
| 219 | + if(listener!=null) |
| 220 | + listener.onAnimationFinish(); |
| 221 | + } |
| 222 | + |
| 223 | + @Override |
| 224 | + public void onAnimationRepeat(final Animator animation) { |
| 225 | + } |
| 226 | + |
| 227 | + @Override |
| 228 | + public void onAnimationStart(final Animator animation) { |
| 229 | + if(listener!=null) |
| 230 | + listener.onAnimationStart(); |
| 231 | + } |
| 232 | + }); |
| 233 | + |
| 234 | + progressBarAnimator.addUpdateListener(new AnimatorUpdateListener() { |
| 235 | + @Override |
| 236 | + public void onAnimationUpdate(final ValueAnimator animation) { |
| 237 | + int progress = ((Float) animation.getAnimatedValue()).intValue(); |
| 238 | + if(progress!=CircularProgressBar.this.getProgress()){ |
| 239 | + CircularProgressBar.this.setProgress(progress); |
| 240 | + if(listener!=null) |
| 241 | + listener.onAnimationProgress(progress); |
| 242 | + } |
| 243 | + } |
| 244 | + }); |
| 245 | + progressBarAnimator.start(); |
| 246 | + } |
| 247 | + |
| 248 | + public synchronized void setTitle(String title){ |
| 249 | + this.mTitle = title; |
| 250 | + invalidate(); |
| 251 | + } |
| 252 | + |
| 253 | + public synchronized void setSubTitle(String subtitle){ |
| 254 | + this.mSubTitle = subtitle; |
| 255 | + invalidate(); |
| 256 | + } |
| 257 | + |
| 258 | + public synchronized void setSubTitleColor(int color){ |
| 259 | + mSubtitlePaint.setColor(color); |
| 260 | + invalidate(); |
| 261 | + } |
| 262 | + |
| 263 | + public synchronized void setTitleColor(int color){ |
| 264 | + mTitlePaint.setColor(color); |
| 265 | + invalidate(); |
| 266 | + } |
| 267 | + |
| 268 | + public synchronized void setHasShadow(boolean flag){ |
| 269 | + this.mHasShadow = flag; |
| 270 | + invalidate(); |
| 271 | + } |
| 272 | + |
| 273 | + public synchronized void setShadow(int color){ |
| 274 | + this.mShadowColor = color; |
| 275 | + invalidate(); |
| 276 | + } |
| 277 | + |
| 278 | + public String getTitle(){ |
| 279 | + return mTitle; |
| 280 | + } |
| 281 | + |
| 282 | + public boolean getHasShadow(){ |
| 283 | + return mHasShadow; |
| 284 | + } |
| 285 | +} |
0 commit comments