Android实现仿微软系统加载动画效果
短信预约 -IT技能 免费直播动态提醒
效果图:
实现步骤:
- 初始化五个圆球分别设置中心点,方便画圆
- 利用ValueAnimator的值变化来获取旋转角度
- onDraw来分别画每个圆
具体代码实现:
1、创建Circle对象
package com.sjl.keeplive.track;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PointF;
public class Circle {
private PointF center;
private float radius;
public Circle() {
center = new PointF();
}
public void setRadius(float radius) {
this.radius = radius;
}
public void setCenter(float x, float y) {
center.set(x, y);
}
public void draw(Canvas canvas, Paint paint) {
canvas.drawCircle(center.x,center.y,radius,paint);
}
}
2、自定义MinSoftLoadingView实现代码
package com.sjl.keeplive.track;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
public class MinSoftLoadingView extends View {
private int circleCount = 5;
private Circle[] circles;
private Paint paint;
private int width;
private int height;
private PointF center;
private float circleRadius;
private float[] rotates;
private float roateRadius;
public MinSoftLoadingView(Context context) {
this(context, null);
}
public MinSoftLoadingView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public MinSoftLoadingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
//每个点旋转的角度
rotates = new float[circleCount];
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
width = getWidth();
height = getHeight();
center = new PointF(width / 2.0f, height / 2.0f);
//旋转扫描半径
roateRadius = Math.min(width, height);
//圆球最大的那个半径
circleRadius = roateRadius / 10.0f;
initCircle();
}
private void initCircle() {
circles = new Circle[circleCount];
for (int i = 0; i < circleCount; i++) {
circles[i] = new Circle();
circles[i].setCenter(center.x, center.y - roateRadius / 2 + circleRadius);
circles[i].setRadius(circleRadius - circleRadius * i / 5);
}
//开始执行动画
startAnimation();
}
private void startAnimation() {
for (int i = 0; i < circleCount; i++) {
final int index = i;
ValueAnimator animator = ValueAnimator.ofFloat(0, 360);
animator.setRepeatCount(ValueAnimator.INFINITE);//重复
animator.setDuration(2000);
animator.setStartDelay(index * 100); //每一个随后的延迟时间
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
rotates[index] = (float) animation.getAnimatedValue();
invalidate();
}
});
animator.start();
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (int i = 0; i < circleCount; i++) {
canvas.save();
canvas.rotate(rotates[i], center.x, center.y);
circles[i].draw(canvas, paint);
canvas.restore();
}
}
}
3、布局文件中使用
<com.sjl.keeplive.track.MinSoftLoadingView
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center"/>
核心思想就是让每个球出发的时间不同,这样每个球锁旋转的位置也不同,就形成了一种视觉差!如果把* animator.setStartDelay(index * 100)*的时间改的大一些就可以看得更明显了!
以上就是Android实现仿微软系统加载动画效果的详细内容,更多关于Android 微软系统加载动画的资料请关注编程网其它相关文章!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341