Last active
April 27, 2016 12:17
-
-
Save gokselpirnal/1f3a84ae30cd8a37277f64c6ee5a9faa to your computer and use it in GitHub Desktop.
CircularPercentView
This file contains hidden or 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
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<declare-styleable name="CircularPercentView"> | |
<attr name="backgroundColor" format="color"/> | |
<attr name="percentColor" format="color"/> | |
<attr name="emptyColor" format="color"/> | |
<attr name="percent" format = "integer"/> | |
</declare-styleable> | |
</resources> |
This file contains hidden or 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.Paint; | |
import android.graphics.RectF; | |
import android.util.AttributeSet; | |
import android.view.View; | |
public class CircularPercentView extends View { | |
private Paint backgroundPaint; | |
private Paint percentPaint; | |
private Paint emptyPaint; | |
private int circleX; | |
private int circleY; | |
private float angle; | |
private RectF barRectF; | |
private float startAngle; | |
private float percent; | |
private int backgroundColor; | |
private int percentColor; | |
private int emptyColor; | |
public CircularPercentView(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
init(context); | |
} | |
public CircularPercentView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CircularPercentView); | |
backgroundColor = typedArray.getColor(R.styleable.CircularPercentView_backgroundColor, 0xffafb4db); | |
percentColor = typedArray.getColor(R.styleable.CircularPercentView_percentColor, 0xff6950a1); | |
emptyColor = typedArray.getColor(R.styleable.CircularPercentView_emptyColor, 0xff6950a1); | |
percent = typedArray.getInt(R.styleable.CircularPercentView_percent, 0); | |
typedArray.recycle(); | |
init(context); | |
} | |
public CircularPercentView(Context context) { | |
super(context); | |
init(context); | |
} | |
private void init(Context context) { | |
startAngle = -90; | |
angle = percent * 3.6f; | |
backgroundPaint = new Paint(); | |
backgroundPaint.setAntiAlias(true); | |
backgroundPaint.setColor(backgroundColor); | |
backgroundPaint.setStyle(Paint.Style.FILL); | |
percentPaint = new Paint(); | |
percentPaint.setAntiAlias(true); | |
percentPaint.setColor(percentColor); | |
percentPaint.setStyle(Paint.Style.STROKE); | |
percentPaint.setStrokeWidth((float) (0.15 * getMeasuredWidth())); | |
emptyPaint = new Paint(); | |
emptyPaint.setAntiAlias(true); | |
emptyPaint.setColor(emptyColor); | |
emptyPaint.setStyle(Paint.Style.STROKE); | |
emptyPaint.setStrokeWidth((float) (0.05 * getMeasuredWidth())); | |
} | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
setMeasuredDimension((widthMeasureSpec), (widthMeasureSpec)); | |
circleX = getMeasuredWidth() / 2; | |
circleY = getMeasuredHeight() / 2; | |
percentPaint.setStrokeWidth((float) (0.15 * circleX)); | |
emptyPaint.setStrokeWidth((float) (0.05 * circleX)); | |
barRectF = new RectF((0.15f * circleX), (0.15f * circleX), getMeasuredWidth() - (0.15f * circleX), getMeasuredWidth() - (0.15f * circleX)); | |
} | |
@Override | |
protected void onDraw(Canvas canvas) { | |
super.onDraw(canvas); | |
canvas.drawCircle(circleX, circleY, getMeasuredWidth(), backgroundPaint); | |
canvas.drawArc(barRectF, startAngle, 360, false, emptyPaint); | |
canvas.drawArc(barRectF, startAngle, angle, false, percentPaint); | |
} | |
public void setPercent(int percent) { | |
this.percent = percent; | |
this.angle = percent * 3.6f; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Mr. Goksel, could you paste screenshots of this gist please?