Created
October 4, 2017 16:18
-
-
Save igalata/8b93c96a33a31ef96635c7e067ebc1be to your computer and use it in GitHub Desktop.
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.animation.ValueAnimator; | |
import android.content.Context; | |
import android.graphics.Canvas; | |
import android.graphics.Color; | |
import android.graphics.Paint; | |
import android.graphics.Path; | |
import android.support.annotation.Nullable; | |
import android.util.AttributeSet; | |
import android.view.View; | |
import android.view.animation.Interpolator; | |
/** | |
* Created by irinagalata on 10/4/17. | |
*/ | |
public class CustomView extends View { | |
Paint paint = new Paint(); | |
Path path = new Path(); | |
private float centerX = 0f; | |
private float centerY = 0f; | |
public CustomView(Context context) { | |
this(context, null); | |
} | |
public CustomView(Context context, @Nullable AttributeSet attrs) { | |
this(context, attrs, 0); | |
} | |
public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
setWillNotDraw(false); | |
startAnimation(); | |
} | |
@Override | |
public void onDraw(Canvas canvas) { | |
super.onDraw(canvas); | |
paint.setColor(Color.RED); | |
paint.setStyle(Paint.Style.FILL); | |
path.addCircle(centerX, centerY, 400, Path.Direction.CCW); | |
path.lineTo(400f, 400f); | |
canvas.drawPath(path, paint); | |
path.rewind(); | |
} | |
public void startAnimation() { | |
ValueAnimator animator = ValueAnimator.ofFloat(0f, 600f); | |
animator.setDuration(4000); | |
animator.setInterpolator(new CustomInterpolator()); | |
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { | |
@Override | |
public void onAnimationUpdate(ValueAnimator valueAnimator) { | |
centerX = (float) valueAnimator.getAnimatedValue(); | |
centerY = (float) valueAnimator.getAnimatedValue(); | |
invalidate(); | |
} | |
}); | |
animator.start(); | |
} | |
} | |
class CustomInterpolator implements Interpolator { | |
@Override | |
public float getInterpolation(float v) { | |
float factor = 0.4f; | |
return (float) (Math.pow(2, -10 * v) | |
* Math.sin((v - factor / 4) * (2 * Math.PI) / factor) + 1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment