Skip to content

Instantly share code, notes, and snippets.

@SouravKumarPandit
Last active September 25, 2018 07:02
Show Gist options
  • Save SouravKumarPandit/bd329a205e6d8831d2e5619a91618a67 to your computer and use it in GitHub Desktop.
Save SouravKumarPandit/bd329a205e6d8831d2e5619a91618a67 to your computer and use it in GitHub Desktop.
package com.focus.sourav.canvasdrawpractice.crmprogress;
import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ComposePathEffect;
import android.graphics.CornerPathEffect;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathDashPathEffect;
import android.graphics.PathEffect;
import android.graphics.PathMeasure;
import android.graphics.Point;
import android.graphics.PointF;
import android.graphics.RectF;
import android.support.v4.view.animation.FastOutLinearInInterpolator;
import android.support.v4.view.animation.FastOutSlowInInterpolator;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AnticipateInterpolator;
import android.view.animation.BounceInterpolator;
import android.view.animation.CycleInterpolator;
import android.view.animation.LinearInterpolator;
public class CLProgressBar extends View {
private static final int DEFAULT_WIDTH = 6;
private static PathMeasure pathMeasure = new PathMeasure();
private PathEffect cornerEffect;
RectF outterCircle;
RectF innerCircle;
ObjectAnimator progress;
public void setTopDegree(int topDegree) {
this.topDegree = topDegree;
invalidate();
}
private int topDegree = 0;
private int arcWidth;
private boolean isStart = false;
private int color;
private CirclePath[] circlePaths;
private float cy;
private float cx;
private Paint linePaint;
private Path arcPath;
public CLProgressBar(Context context) {
super(context);
initView(context, null);
}
public CLProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context, attrs);
}
public CLProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context, attrs);
}
private void initView(Context context, AttributeSet attrs) {
color = Color.RED;
this.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
arcWidth = dpToPx(context, DEFAULT_WIDTH);
linePaint = new Paint();
linePaint.setColor(Color.RED);
linePaint.setAntiAlias(true);
linePaint.setStyle(Paint.Style.FILL);
linePaint.setStrokeWidth(dpToPx(getContext(), 1));
linePaint.setDither(true);
linePaint.setStrokeCap(Paint.Cap.ROUND);
// cornerEffect = new CornerPathEffect(8f);
// linePaint.setPathEffect(cornerEffect);
arcPath = new Path();
outterCircle = new RectF();
innerCircle = new RectF();
}
public Path drawDonut(Path path, float start, float sweep) {
path.reset();
path.arcTo(outterCircle, start, sweep, false);
path.arcTo(innerCircle, start + sweep, -sweep, false);
path.close();
return arcPath;
// canvas.drawPath(arcPath, paint);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (!isStart) {
return;
}
if (circlePaths == null) {
cx = canvas.getWidth() / 2;
cy = canvas.getHeight() / 2;
circlePaths = new CirclePath[]{
new CirclePath(0xffdedede, canvas.getWidth() * 0.25f, 3 + (int) (Math.random() * ((6 - 2) + 1)), -1),
new CirclePath(0xffdedede, canvas.getWidth() * 0.35f, 2 + (int) (Math.random() * ((6 - 2) + 1)), 1),
new CirclePath(0xffdedede, canvas.getWidth() * 0.45f, 2 + (int) (Math.random() * ((6 - 2) + 1)), -1)
};
}
for (CirclePath circlePath : circlePaths) {
outterCircle.set(cx - circlePath.radius, cy - circlePath.radius, cx + circlePath.radius, cy + circlePath.radius);
innerCircle.set(cx - circlePath.radius + arcWidth, cy - circlePath.radius + arcWidth, cx + circlePath.radius - arcWidth, cy + circlePath.radius - arcWidth);
canvas.rotate(topDegree,cx,cy);
canvas.drawPath(circlePath.path, linePaint);
}
}
private int rotation1(int delta) {
topDegree = (topDegree + delta) % 360;
return topDegree;
}
public void setLoadingColor(int color) {
this.color = color;
}
public int getLoadingColor() {
return color;
}
public void start() {
startAnimator();
isStart = true;
invalidate();
}
public void stop() {
stopAnimator();
invalidate();
}
public boolean isStart() {
return isStart;
}
private void startAnimator() {
ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(this, "scaleX", 0.0f, 1);
ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(this, "scaleY", 0.0f, 1);
scaleXAnimator.setDuration(200);
scaleXAnimator.setInterpolator(new LinearInterpolator());
scaleYAnimator.setDuration(200);
scaleYAnimator.setInterpolator(new LinearInterpolator());
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(scaleXAnimator, scaleYAnimator);
animatorSet.start();
ObjectAnimator progress = ObjectAnimator.ofInt(this, "topDegree", 0, 360);
progress.setDuration(2500L);
progress.setStartDelay(100);
progress.setInterpolator(new LinearInterpolator());
progress.setRepeatCount(ValueAnimator.INFINITE);
progress.setRepeatMode(ValueAnimator.RESTART);
progress.start();
}
private void stopAnimator() {
ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(this, "scaleX", 1, 0);
ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(this, "scaleY", 1, 0);
scaleXAnimator.setDuration(200);
scaleXAnimator.setInterpolator(new LinearInterpolator());
scaleYAnimator.setDuration(200);
scaleYAnimator.setInterpolator(new LinearInterpolator());
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(scaleXAnimator, scaleYAnimator);
animatorSet.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
isStart = false;
if (progress!=null)
progress.cancel();
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
animatorSet.start();
}
public int dpToPx(Context context, float dpVal) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpVal, context.getResources().getDisplayMetrics());
}
private class CirclePath {
int rotation;
float radius;
int laps;
float length;
int color;
float initialPhase;
final Path path;
public CirclePath(int iColor, float radius, int laps, int rotation) {
this.color = iColor;
this.radius = radius;
this.laps = laps;
this.rotation = rotation;
path = createPath(radius);
pathMeasure.setPath(path, true);
length = pathMeasure.getLength();
}
public Path getPath() {
return path;
}
private Path createPath(float radius) {
Path path = new Path();
path.reset();
// path.addArc(0,0,cx*2, cy*2, 45,275);
outterCircle.set(cx - radius, cy - radius, cx + radius, cy + radius);
innerCircle.set(cx - radius + arcWidth, cy - radius + arcWidth, cx + radius - arcWidth, cy + radius - arcWidth);
path.addCircle(cx, cy, radius, Path.Direction.CCW);
drawDonut(path, 30, 290);
path.close();
return path;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment