Skip to content

Instantly share code, notes, and snippets.

@alexandrius
Created January 9, 2017 09:38
Show Gist options
  • Save alexandrius/c1be855795a720d65f024a52468841a6 to your computer and use it in GitHub Desktop.
Save alexandrius/c1be855795a720d65f024a52468841a6 to your computer and use it in GitHub Desktop.
public class CircleProgressView extends View {
private static final int START_ANGLE_POINT = 270;
private Paint paint;
private RectF rect;
private float angle;
private RectF shadowRect;
private Paint shadowPaint;
private Paint circlePaint;
private boolean isItem;
private int ratVal;
private int left;
private int width;
private int STROKE_WIDTH = 46;
private int SHADOW_STROKE_WIDTH = 48;
private int currentProgress = 0;
public CircleProgressView(Context c) {
super(c);
STROKE_WIDTH = c.getResources().getDimensionPixelSize(R.dimen.progress_stroke_width);
SHADOW_STROKE_WIDTH = c.getResources().getDimensionPixelSize(R.dimen.progress_stroke_shadow_width);
init(SHADOW_STROKE_WIDTH, STROKE_WIDTH, UiTools.getScreenDimens().x / 3);
}
public CircleProgressView(Context context, AttributeSet attrs) {
super(context, attrs);
if (attrs != null) {
TypedArray ar = context.obtainStyledAttributes(attrs, R.styleable.ProgressCircle);
this.isItem = ar.getBoolean(R.styleable.ProgressCircle_shouldResize, false);
ar.recycle();
}
init(SHADOW_STROKE_WIDTH, STROKE_WIDTH, UiTools.getScreenDimens().x / 3);
}
public void init(int shadowStrokeWidth, int strokeWidth, int width) {
setLayerType(LAYER_TYPE_SOFTWARE, null); //FIXME: on older devices
invalidate();
angle = 0;
paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(strokeWidth);
int color = Tools.getColor(getContext(), R.color.app_main_color);
if (isItem) {
color = Tools.getColor(getContext(), R.color.green_default_color);
}
paint.setColor(color);
if (!isItem) {
left = (ratVal - width) / 2;
rect = new RectF(left, strokeWidth, ratVal - left, width + strokeWidth);
shadowRect = new RectF(left + 2, shadowStrokeWidth, ratVal - left + 2, width + shadowStrokeWidth);
} else {
rect = new RectF(strokeWidth, strokeWidth, width + strokeWidth, width + strokeWidth);
shadowRect = new RectF(shadowStrokeWidth, shadowStrokeWidth, width + shadowStrokeWidth, width + shadowStrokeWidth);
}
circlePaint = new Paint();
circlePaint.setARGB(255, 236, 240, 241);
circlePaint.setStyle(Paint.Style.FILL);
circlePaint.setAntiAlias(true);
shadowPaint = new Paint();
shadowPaint.set(paint);
shadowPaint.setStyle(Paint.Style.STROKE);
shadowPaint.setStrokeWidth(shadowStrokeWidth);
shadowPaint.setMaskFilter(new BlurMaskFilter(15, BlurMaskFilter.Blur.NORMAL));
shadowPaint.setARGB(180, 146, 146, 146);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
this.ratVal = w;
this.width = h;
if (isItem)
init(0, 7, w - 14);
else {
init(SHADOW_STROKE_WIDTH, STROKE_WIDTH, h - 80);
}
}
public void animateProgress(int currentProgress) {
animateProgress(currentProgress, 1000);
}
public void animateProgress(int currentProgress, long duration) {
if (isItem) {
int color = Tools.getColor(getContext(), R.color.green_default_color);
this.currentProgress = currentProgress;
if (currentProgress == 0) {
color = Tools.getColor(getContext(), R.color.default_divider_color);
currentProgress = 100;
}
paint.setColor(color);
}
CircleAngleAnimation angleAnimation = new CircleAngleAnimation(360 * currentProgress / 100);
angleAnimation.setDuration(duration);
startAnimation(angleAnimation);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (!isItem) {
canvas.drawCircle(ratVal / 2, width / 2, width / 2 - 40, circlePaint);
}
canvas.drawArc(shadowRect, START_ANGLE_POINT, -angle, false, shadowPaint);
if(isItem && currentProgress == 0){
paint.setColor(Tools.getColor(getContext(), R.color.default_divider_color));
}
canvas.drawArc(rect, START_ANGLE_POINT, -angle, false, paint);
}
public float getAngle() {
return angle;
}
public void setAngle(float angle) {
this.angle = angle;
}
private class CircleAngleAnimation extends Animation {
private float oldAngle;
private float newAngle;
public CircleAngleAnimation(int newAngle) {
this.oldAngle = CircleProgressView.this.getAngle();
this.newAngle = newAngle;
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation transformation) {
float angle = oldAngle + ((newAngle - oldAngle) * interpolatedTime);
CircleProgressView.this.setAngle(angle);
CircleProgressView.this.requestLayout();
CircleProgressView.this.invalidate();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment