Skip to content

Instantly share code, notes, and snippets.

@pollux-
Last active December 21, 2016 11:00
Show Gist options
  • Save pollux-/346cbf827168a612a992ebb4ff851177 to your computer and use it in GitHub Desktop.
Save pollux-/346cbf827168a612a992ebb4ff851177 to your computer and use it in GitHub Desktop.
public class DualProgressView extends View {
private int mSize;
private Paint mPaint;
private RectF mRect;
private float mIndeterminateSweep;
private float mstartAngle;
public DualProgressView(Context context) {
super(context);
init();
}
public DualProgressView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public DualProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(Color.RED);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(8f);
mPaint.setStrokeCap(Paint.Cap.BUTT);
mRect = new RectF(200, 200, 800, 800);
mIndeterminateSweep = 10f;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawArc(mRect, mstartAngle, mIndeterminateSweep, false, mPaint);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int xPad = getPaddingLeft() + getPaddingRight();
int yPad = getPaddingTop() + getPaddingBottom();
int width = getMeasuredWidth() - xPad;
int height = getMeasuredHeight() - yPad;
mSize = (width < height) ? width : height;
setMeasuredDimension(mSize + xPad, mSize + yPad);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
animateArch();
}
private void animateArch() {
final ValueAnimator frontEndExtend = ValueAnimator.ofFloat(0, 360);
frontEndExtend.setDuration(5000);
frontEndExtend.setInterpolator(new LinearInterpolator());
frontEndExtend.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
mstartAngle = (Float) animation.getAnimatedValue();
mIndeterminateSweep += 2;
if (mIndeterminateSweep > 360)
mIndeterminateSweep = 15f;
invalidate();
}
});
frontEndExtend.start();
frontEndExtend.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {
}
@Override
public void onAnimationEnd(Animator animator) {
animateArch();
}
@Override
public void onAnimationCancel(Animator animator) {
}
@Override
public void onAnimationRepeat(Animator animator) {
}
});
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment