Android仿QQ邮箱下拉刷新动画(三个小球围绕中心转动)

2024-08-22 18:18

本文主要是介绍Android仿QQ邮箱下拉刷新动画(三个小球围绕中心转动),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

仿QQ邮箱下拉刷新动画(三个小球围绕中心转动)

该动画的实现主要借鉴了海龙的博客- 两个小球不停的绕中心旋转的进度条 ,在此感谢下。

1 首先上图(折腾了好久才把gif给搞了上去

这里写图片描述

2 分析

2.1 当我们看到一个动画,首先需要对动画的效果进行分析,而不是盲目的进行开发

2.2 动画的分解(先需要关注一个小球的效果,避免其他干扰)
2.2.1 平移动画:把中心点的横坐标当作坐标的原点
第一个小球的x轴变化为:-1f>0f>1f>0f>-1f;
第二个小球的x轴变化为:0f>1f>0f>-1f>0f;
第三个小球的x轴变化为:1f>0f>-1f>0f>1f;
2.2.2 缩放动画:(三种大小:minRadius,centerRadius,maxRadius)
第一个小球的缩放变化为:center>max>center>min>center;
第二个小球的缩放变化为:max>center>min>center>max;
第三个小球的缩放变化为:center>min>center>max>center;
2.2.3 重要的一点:简单理解为半径大的覆盖在半径小的上方

3 分析完毕,直接上代码

public class ThreeBallRotationProgressBar extends View {private final static int DEFAULT_MAX_RADIUS = 16;private final static int DEFAULT_MIN_RADIUS = 5;private final static int DEFAULT_DISTANCE = 35;private final static int DEFAULT_ONE_BALL_COLOR = Color.parseColor("#40df73");private final static int DEFAULT_TWO_BALL_COLOR = Color.parseColor("#ffdf3e");private final static int DEFAULT_THREE_BALL_COLOR = Color.parseColor("#ff733e");private final static int DEFAULT_ANIMATOR_DURATION = 1400;private Paint mOnePaint;private Paint mTwoPaint;private Paint mThreePaint;private float maxRadius = DEFAULT_MAX_RADIUS;private float minRadius = DEFAULT_MIN_RADIUS;private int distance = DEFAULT_DISTANCE;private long duration = DEFAULT_ANIMATOR_DURATION;private Ball mOneBall;private Ball mTwoBall;private Ball mThreeBall;private float mCenterX;private float mCenterY;private AnimatorSet animatorSet;public ThreeBallRotationProgressBar(Context context) {this(context, null);}public ThreeBallRotationProgressBar(Context context, AttributeSet attrs) {this(context, attrs, 0);}public ThreeBallRotationProgressBar(Context context, AttributeSet attrs,int defStyleAttr) {super(context, attrs, defStyleAttr);init(context);}private void init(Context context) {mOneBall = new Ball();mTwoBall = new Ball();mThreeBall = new Ball();mOneBall.setColor(DEFAULT_ONE_BALL_COLOR);mTwoBall.setColor(DEFAULT_TWO_BALL_COLOR);mThreeBall.setColor(DEFAULT_THREE_BALL_COLOR);mOnePaint = new Paint(Paint.ANTI_ALIAS_FLAG);mOnePaint.setColor(DEFAULT_ONE_BALL_COLOR);mTwoPaint = new Paint(Paint.ANTI_ALIAS_FLAG);mTwoPaint.setColor(DEFAULT_TWO_BALL_COLOR);mThreePaint = new Paint(Paint.ANTI_ALIAS_FLAG);mThreePaint.setColor(DEFAULT_THREE_BALL_COLOR);configAnimator();}@Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh) {super.onSizeChanged(w, h, oldw, oldh);mCenterX = w / 2;mCenterY = h / 2;}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);mCenterX = getWidth() / 2;mCenterY = getHeight() / 2;}@Overrideprotected void onDraw(Canvas canvas) {if (mOneBall.getRadius() >= mTwoBall.getRadius()) {if (mThreeBall.getRadius() >= mOneBall.getRadius()) {canvas.drawCircle(mTwoBall.getCenterX(), mCenterY,mTwoBall.getRadius(), mTwoPaint);canvas.drawCircle(mOneBall.getCenterX(), mCenterY,mOneBall.getRadius(), mOnePaint);canvas.drawCircle(mThreeBall.getCenterX(), mCenterY,mThreeBall.getRadius(), mThreePaint);} else {if (mTwoBall.getRadius() <= mThreeBall.getRadius()) {canvas.drawCircle(mTwoBall.getCenterX(), mCenterY,mTwoBall.getRadius(), mTwoPaint);canvas.drawCircle(mThreeBall.getCenterX(), mCenterY,mThreeBall.getRadius(), mThreePaint);canvas.drawCircle(mOneBall.getCenterX(), mCenterY,mOneBall.getRadius(), mOnePaint);} else {canvas.drawCircle(mThreeBall.getCenterX(), mCenterY,mThreeBall.getRadius(), mThreePaint);canvas.drawCircle(mTwoBall.getCenterX(), mCenterY,mTwoBall.getRadius(), mTwoPaint);canvas.drawCircle(mOneBall.getCenterX(), mCenterY,mOneBall.getRadius(), mOnePaint);}}} else {if (mThreeBall.getRadius() >= mTwoBall.getRadius()) {canvas.drawCircle(mOneBall.getCenterX(), mCenterY,mOneBall.getRadius(), mOnePaint);canvas.drawCircle(mTwoBall.getCenterX(), mCenterY,mTwoBall.getRadius(), mTwoPaint);canvas.drawCircle(mThreeBall.getCenterX(), mCenterY,mThreeBall.getRadius(), mThreePaint);} else {if (mOneBall.getRadius() <= mThreeBall.getRadius()) {canvas.drawCircle(mOneBall.getCenterX(), mCenterY,mOneBall.getRadius(), mOnePaint);canvas.drawCircle(mThreeBall.getCenterX(), mCenterY,mThreeBall.getRadius(), mThreePaint);canvas.drawCircle(mTwoBall.getCenterX(), mCenterY,mTwoBall.getRadius(), mTwoPaint);} else {canvas.drawCircle(mThreeBall.getCenterX(), mCenterY,mThreeBall.getRadius(), mThreePaint);canvas.drawCircle(mOneBall.getCenterX(), mCenterY,mOneBall.getRadius(), mOnePaint);canvas.drawCircle(mTwoBall.getCenterX(), mCenterY,mTwoBall.getRadius(), mTwoPaint);}}}}private void configAnimator() {float centerRadius = (maxRadius + minRadius) * 0.5f;ObjectAnimator oneScaleAnimator = ObjectAnimator.ofFloat(mOneBall,"radius", centerRadius, maxRadius, centerRadius, minRadius,centerRadius);oneScaleAnimator.setRepeatCount(ValueAnimator.INFINITE);ValueAnimator oneCenterAnimator = ValueAnimator.ofFloat(-1, 0, 1, 0, -1);oneCenterAnimator.setRepeatCount(ValueAnimator.INFINITE);oneCenterAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {@Overridepublic void onAnimationUpdate(ValueAnimator animation) {float value = (Float) animation.getAnimatedValue();float x = mCenterX + (distance) * value;mOneBall.setCenterX(x);invalidate();}});ValueAnimator oneAlphaAnimator = ValueAnimator.ofFloat(0.8f, 1, 0.8f,0, 0.8f);oneAlphaAnimator.setRepeatCount(ValueAnimator.INFINITE);oneAlphaAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {@Overridepublic void onAnimationUpdate(ValueAnimator animation) {float value = (Float) animation.getAnimatedValue();int alpha = (int) (255 * value);mOnePaint.setAlpha(alpha);}});ObjectAnimator twoScaleAnimator = ObjectAnimator.ofFloat(mTwoBall,"radius", maxRadius, centerRadius, minRadius, centerRadius,maxRadius);twoScaleAnimator.setRepeatCount(ValueAnimator.INFINITE);ValueAnimator twoCenterAnimator = ValueAnimator.ofFloat(0, 1, 0, -1, 0);twoCenterAnimator.setRepeatCount(ValueAnimator.INFINITE);twoCenterAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {@Overridepublic void onAnimationUpdate(ValueAnimator animation) {float value = (Float) animation.getAnimatedValue();float x = mCenterX + (distance) * value;mTwoBall.setCenterX(x);}});ValueAnimator twoAlphaAnimator = ValueAnimator.ofFloat(1, 0.8f, 0,0.8f, 1);twoAlphaAnimator.setRepeatCount(ValueAnimator.INFINITE);twoAlphaAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {@Overridepublic void onAnimationUpdate(ValueAnimator animation) {float value = (Float) animation.getAnimatedValue();int alpha = (int) (255 * value);mTwoPaint.setAlpha(alpha);}});ObjectAnimator threeScaleAnimator = ObjectAnimator.ofFloat(mThreeBall,"radius", centerRadius, minRadius, centerRadius, maxRadius,centerRadius);threeScaleAnimator.setRepeatCount(ValueAnimator.INFINITE);ValueAnimator threeCenterAnimator = ValueAnimator.ofFloat(1, 0, -1, 0,1);threeCenterAnimator.setRepeatCount(ValueAnimator.INFINITE);threeCenterAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {@Overridepublic void onAnimationUpdate(ValueAnimator animation) {float value = (Float) animation.getAnimatedValue();float x = mCenterX + (distance) * value;mThreeBall.setCenterX(x);}});ValueAnimator threeAlphaAnimator = ValueAnimator.ofFloat(0.8f, 0, 0.8f,1, 0.8f);threeAlphaAnimator.setRepeatCount(ValueAnimator.INFINITE);threeAlphaAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {@Overridepublic void onAnimationUpdate(ValueAnimator animation) {float value = (Float) animation.getAnimatedValue();int alpha = (int) (255 * value);mThreePaint.setAlpha(alpha);}});animatorSet = new AnimatorSet();animatorSet.playTogether(oneScaleAnimator, oneCenterAnimator,twoScaleAnimator, twoCenterAnimator, threeScaleAnimator,threeCenterAnimator);animatorSet.setDuration(DEFAULT_ANIMATOR_DURATION);animatorSet.setInterpolator(new LinearInterpolator());}public class Ball {private float radius;private float centerX;private int color;public float getRadius() {return radius;}public void setRadius(float radius) {this.radius = radius;}public float getCenterX() {return centerX;}public void setCenterX(float centerX) {this.centerX = centerX;}public int getColor() {return color;}public void setColor(int color) {this.color = color;}}@Overridepublic void setVisibility(int v) {if (getVisibility() != v) {super.setVisibility(v);if (v == GONE || v == INVISIBLE) {stopAnimator();} else {startAnimator();}}}@Overrideprotected void onVisibilityChanged(View changedView, int v) {super.onVisibilityChanged(changedView, v);if (v == GONE || v == INVISIBLE) {stopAnimator();} else {startAnimator();}}@Overrideprotected void onAttachedToWindow() {super.onAttachedToWindow();startAnimator();}@Overrideprotected void onDetachedFromWindow() {super.onDetachedFromWindow();stopAnimator();}public void setOneBallColor(int color) {mOneBall.setColor(color);}public void setmTwoBallColor(int color) {mTwoBall.setColor(color);}public void setMaxRadius(float maxRadius) {this.maxRadius = maxRadius;configAnimator();}public void setMinRadius(float minRadius) {this.minRadius = minRadius;configAnimator();}public void setDistance(int distance) {this.distance = distance;}public void setDuration(long duration) {this.duration = duration;if (animatorSet != null) {animatorSet.setDuration(duration);}}public void startAnimator() {if (getVisibility() != VISIBLE)return;if (animatorSet.isRunning())return;if (animatorSet != null) {animatorSet.start();}}public void stopAnimator() {if (animatorSet != null) {animatorSet.end();}}
}

3.1 configAnimator()方法主要就是实现2中分析的动画效果

3.2 为了解决2中提到的重要一点,半径大的小球覆盖在半径小的小球上方,主要在onDraw()采用比较的方式实现

4 总结

4.1 掌阅iReader的下拉刷新也采用了类似的动画效果(三个方形围绕中心转动),大家可以参考本文章,试着实现里边的动画效果。相信自己写过,总能有不少收获的~

4.2 对于android动画,还是需要耐心的分析,当然熟悉的掌握动画实现还是必要的!

5 源码下载

下载地址

这篇关于Android仿QQ邮箱下拉刷新动画(三个小球围绕中心转动)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1097024

相关文章

springboot加载不到nacos配置中心的配置问题处理

《springboot加载不到nacos配置中心的配置问题处理》:本文主要介绍springboot加载不到nacos配置中心的配置问题处理,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录springboot加载不到nacos配置中心的配置两种可能Spring Boot 版本Nacos

Nacos注册中心和配置中心的底层原理全面解读

《Nacos注册中心和配置中心的底层原理全面解读》:本文主要介绍Nacos注册中心和配置中心的底层原理的全面解读,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录临时实例和永久实例为什么 Nacos 要将服务实例分为临时实例和永久实例?1.x 版本和2.x版本的区别

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

SpringCloud使用Nacos 配置中心实现配置自动刷新功能使用

《SpringCloud使用Nacos配置中心实现配置自动刷新功能使用》SpringCloud项目中使用Nacos作为配置中心可以方便开发及运维人员随时查看配置信息,及配置共享,并且Nacos支持配... 目录前言一、Nacos中集中配置方式?二、使用步骤1.使用$Value 注解2.使用@Configur

Kotlin Compose Button 实现长按监听并实现动画效果(完整代码)

《KotlinComposeButton实现长按监听并实现动画效果(完整代码)》想要实现长按按钮开始录音,松开发送的功能,因此为了实现这些功能就需要自己写一个Button来解决问题,下面小编给大... 目录Button 实现原理1. Surface 的作用(关键)2. InteractionSource3.

使用WPF实现窗口抖动动画效果

《使用WPF实现窗口抖动动画效果》在用户界面设计中,适当的动画反馈可以提升用户体验,尤其是在错误提示、操作失败等场景下,窗口抖动作为一种常见且直观的视觉反馈方式,常用于提醒用户注意当前状态,本文将详细... 目录前言实现思路概述核心代码实现1、 获取目标窗口2、初始化基础位置值3、创建抖动动画4、动画完成后

使用animation.css库快速实现CSS3旋转动画效果

《使用animation.css库快速实现CSS3旋转动画效果》随着Web技术的不断发展,动画效果已经成为了网页设计中不可或缺的一部分,本文将深入探讨animation.css的工作原理,如何使用以及... 目录1. css3动画技术简介2. animation.css库介绍2.1 animation.cs

Android NDK版本迭代与FFmpeg交叉编译完全指南

《AndroidNDK版本迭代与FFmpeg交叉编译完全指南》在Android开发中,使用NDK进行原生代码开发是一项常见需求,特别是当我们需要集成FFmpeg这样的多媒体处理库时,本文将深入分析A... 目录一、android NDK版本迭代分界线二、FFmpeg交叉编译关键注意事项三、完整编译脚本示例四

Android与iOS设备MAC地址生成原理及Java实现详解

《Android与iOS设备MAC地址生成原理及Java实现详解》在无线网络通信中,MAC(MediaAccessControl)地址是设备的唯一网络标识符,本文主要介绍了Android与iOS设备M... 目录引言1. MAC地址基础1.1 MAC地址的组成1.2 MAC地址的分类2. android与I

Android 实现一个隐私弹窗功能

《Android实现一个隐私弹窗功能》:本文主要介绍Android实现一个隐私弹窗功能,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 效果图如下:1. 设置同意、退出、点击用户协议、点击隐私协议的函数参数2. 《用户协议》、《隐私政策》设置成可点击的,且颜色要区分出来res/l