Created
September 15, 2015 09:03
-
-
Save Folyd/7f37ee964668f8b6b31e to your computer and use it in GitHub Desktop.
Circle ProgressBar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.content.res.TypedArray; | |
import android.graphics.Canvas; | |
import android.graphics.Color; | |
import android.graphics.Paint; | |
import android.graphics.RectF; | |
import android.support.annotation.NonNull; | |
import android.util.AttributeSet; | |
import android.view.View; | |
import com.theonepiano.smartpiano.R; | |
public class CircleProgressBar extends View { | |
private int mProgress; | |
private float mStrokeWidth; | |
private int mTextRatio; | |
private Paint mPaint; | |
private RectF mOval; | |
private int mStrokeColor, mProgressColor, mTextColor; | |
public CircleProgressBar(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
/* | |
<declare-styleable name="CircleProgressBar"> | |
<attr name="strokeColor"/> | |
<attr name="strokeWidth"/> | |
<attr name="progressColor" format="reference|color"/> | |
<attr name="textColor" format="reference|color"/> | |
<attr name="textRatio" format="reference|integer"/> | |
</declare-styleable> | |
*/ | |
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CircleProgressBar); | |
mStrokeWidth = ta.getDimension(R.styleable.CircleProgressBar_strokeWidth, 4); | |
mTextRatio = ta.getInt(R.styleable.CircleProgressBar_textRatio, 4); | |
mStrokeColor = ta.getColor(R.styleable.CircleProgressBar_strokeColor, | |
android.R.color.black); | |
mProgressColor = ta.getColor(R.styleable.CircleProgressBar_progressColor, | |
android.R.color.white); | |
mTextColor = ta.getColor(R.styleable.CircleProgressBar_textColor, android.R.color.white); | |
ta.recycle(); | |
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); | |
mOval = new RectF(); | |
} | |
@Override | |
public void draw(@NonNull Canvas canvas) { | |
super.draw(canvas); | |
canvas.drawColor(Color.TRANSPARENT); | |
int diameter = Math.min(getWidth(), getHeight()); | |
mOval.left = mStrokeWidth / 2; | |
mOval.top = mStrokeWidth / 2; | |
mOval.right = diameter - mStrokeWidth / 2; | |
mOval.bottom = diameter - mStrokeWidth / 2; | |
mPaint.setStrokeWidth(mStrokeWidth); | |
mPaint.setStyle(Paint.Style.STROKE); | |
mPaint.setColor(mStrokeColor); | |
canvas.drawArc(mOval, -90, 360, false, mPaint); | |
mPaint.setColor(mProgressColor); | |
float sweepAngel = mProgress / 100.0f * 360.0f; | |
canvas.drawArc(mOval, -90, sweepAngel, false, mPaint); | |
mPaint.setColor(mTextColor); | |
String text = mProgress + "%"; | |
int textHeight = diameter / mTextRatio; | |
mPaint.setTextSize(textHeight); | |
float textWidth = mPaint.measureText(text); | |
mPaint.setStyle(Paint.Style.FILL); | |
canvas.drawText(text, diameter / 2 - textWidth / 2, diameter / 2 + textHeight / 2, mPaint); | |
} | |
public void setProgress(int progress) { | |
this.mProgress = progress; | |
invalidate(); | |
} | |
public void setProgressNotInUiThread(int progress) { | |
this.mProgress = progress; | |
this.postInvalidate(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment