当前位置:网站首页>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
EDLines: A real-time line segment detector with a false detection control翻译
How to achieve 0 error (s) and 0 warning (s) in keil5
服务器渲染技术jsp
Cookie&Session
The value of the second servo encoder is linked to the NC virtual axis of Beifu PLC for display
Latest interface automation interview questions
完全背包问题
Feign remote call and getaway gateway
C # realize solving the shortest path of unauthorized graph based on breadth first BFS -- complete program display
随机推荐
pytest-fixture
XXL job User Guide
Leetcode 1482 guess, how about this question?
Best used trust automation script (shell)
Force buckle - sum of two numbers
CX5120控制汇川IS620N伺服报错E15解决方案
后台系统右边内容如何出现滚动条和解决双滚动条的问题
FCN全卷积网络理解及代码实现(来自pytorch官方实现)
The 'mental (tiring) process' of building kubernetes/kubesphere environment with kubekey
Ultimate dolls 2.0 | encapsulation of cloud native delivery
数据库中COMMENT关键字的使用
服务器渲染技术jsp
Finally in promise
力扣-两数之和
How to verify whether the contents of two files are the same
Overview of EtherCAT principle
Listener listener
终极套娃 2.0 | 云原生交付的封装
手把手带你了解一块电路板,从设计到制作(干货)
C language EXECL function