当前位置:网站首页>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);
}
}
边栏推荐
猜你喜欢

数据交换 JSON

C#实现图的深度优先遍历--非递归代码

EDLines: A real-time line segment detector with a false detection control翻译

# 使用 KubeKey 搭建 Kubernetes/KubeSphere 环境的'心路(累)历程'

Example of Huawei operator level router configuration | example of configuring optionc mode cross domain LDP VPLS

Latest interface automation interview questions

世界上最好的学习法:费曼学习法
Common interview questions for performance test

Introduction to EtherCAT

终极套娃 2.0 | 云原生交付的封装
随机推荐
Redis 教程
HTB-Lame
MySQL index --01--- design principle of index
第03章_用户与权限管理
pytest-fixture
JUC学习
The value of the second servo encoder is linked to the NC virtual axis of Beifu PLC for display
力扣-两数之和
Nacos
【读书笔记】《文案变现》——写出有效文案的四个黄金步骤
go实现命令行的工具cli
[QT] add knowledge supplement of third-party database
How to achieve 0 error (s) and 0 warning (s) in keil5
限流组件设计实战
Introduction and installation of Solr
The 'mental (tiring) process' of building kubernetes/kubesphere environment with kubekey
ctfshow爆破wp
岭回归和lasso回归
Redis efficient like and cancel function
10、Scanner.next() 无法读取空格/indexOf -1