当前位置:网站首页>Pathmeasure implements loading animation
Pathmeasure implements loading animation
2022-07-01 03:26:00 【Xiao Er Li】
design sketch

PathLoadingView.java
public class PathLoadingView extends View {
private Path mPath;
private Path dest;
private Paint mPaint;
private PathMeasure mPathMeasure;
private float mLength;
private float mAnimatedValue;
public PathLoadingView(Context context) {
this(context, null);
}
public PathLoadingView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public PathLoadingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
// Initialize brush
mPaint = new Paint();
mPaint.setColor(Color.parseColor("#FF4081"));
mPaint.setStrokeWidth(2f);
mPaint.setStyle(Paint.Style.STROKE);
mPath = new Path();
mPath.addCircle(300f, 300f, 20f, Path.Direction.CW);
// initialization PathMeasure
mPathMeasure = new PathMeasure(mPath, true);
// Path length
mLength = mPathMeasure.getLength();
dest = new Path();
// Attribute animation , Monitor animation value changes , For dynamic calculation path length
ValueAnimator animator = ValueAnimator.ofFloat(0, 1);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
// Get the change of animation value
mAnimatedValue = (float) animation.getAnimatedValue();
// Trigger onDraw
invalidate();
}
});
animator.setDuration(2000);
animator.setRepeatCount(ValueAnimator.INFINITE); // Infinite loop
animator.start();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// Reset before each painting dest
dest.reset();
float distance = mLength * mAnimatedValue;
// distance - 0.5*mLength < start < distance
float start = (float) (distance - (0.5 - Math.abs(mAnimatedValue - 0.5f)) * mLength);
// Intercept path Length to dest
mPathMeasure.getSegment(start,distance,dest,true);
canvas.drawPath(dest,mPaint);
}
}
边栏推荐
猜你喜欢
随机推荐
[linear DP] shortest editing distance
岭回归和lasso回归
BluePrism注册下载并安装-RPA第一章
[linear DP] longest common subsequence
leetcode 1482 猜猜看啊,这道题目怎么二分?
Thread data sharing and security -threadlocal
Redis 教程
Mysql知识点
Server rendering technology JSP
pytest-fixture
Filter
手把手带你了解一块电路板,从设计到制作(干货)
Go tool cli for command line implementation
不用加减乘除实现加法
md5sum操作
C # realize solving the shortest path of unauthorized graph based on breadth first BFS -- complete program display
ctfshow爆破wp
别再说不会解决 “跨域“ 问题啦
实战 ELK 优雅管理服务器日志
Introduction to webrtc concept -- an article on understanding source, track, sink and mediastream









