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

相关文章

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06

跨国公司撤出在华研发中心的启示:中国IT产业的挑战与机遇

近日,IBM中国宣布撤出在华的两大研发中心,这一决定在IT行业引发了广泛的讨论和关注。跨国公司在华研发中心的撤出,不仅对众多IT从业者的职业发展带来了直接的冲击,也引发了人们对全球化背景下中国IT产业竞争力和未来发展方向的深思。面对这一突如其来的变化,我们应如何看待跨国公司的决策?中国IT人才又该如何应对?中国IT产业将何去何从?本文将围绕这些问题展开探讨。 跨国公司撤出的背景与

Android实现任意版本设置默认的锁屏壁纸和桌面壁纸(两张壁纸可不一致)

客户有些需求需要设置默认壁纸和锁屏壁纸  在默认情况下 这两个壁纸是相同的  如果需要默认的锁屏壁纸和桌面壁纸不一样 需要额外修改 Android13实现 替换默认桌面壁纸: 将图片文件替换frameworks/base/core/res/res/drawable-nodpi/default_wallpaper.*  (注意不能是bmp格式) 替换默认锁屏壁纸: 将图片资源放入vendo

Android平台播放RTSP流的几种方案探究(VLC VS ExoPlayer VS SmartPlayer)

技术背景 好多开发者需要遴选Android平台RTSP直播播放器的时候,不知道如何选的好,本文针对常用的方案,做个大概的说明: 1. 使用VLC for Android VLC Media Player(VLC多媒体播放器),最初命名为VideoLAN客户端,是VideoLAN品牌产品,是VideoLAN计划的多媒体播放器。它支持众多音频与视频解码器及文件格式,并支持DVD影音光盘,VCD影

Flutter 进阶:绘制加载动画

绘制加载动画:由小圆组成的大圆 1. 定义 LoadingScreen 类2. 实现 _LoadingScreenState 类3. 定义 LoadingPainter 类4. 总结 实现加载动画 我们需要定义两个类:LoadingScreen 和 LoadingPainter。LoadingScreen 负责控制动画的状态,而 LoadingPainter 则负责绘制动画。

android-opencv-jni

//------------------start opencv--------------------@Override public void onResume(){ super.onResume(); //通过OpenCV引擎服务加载并初始化OpenCV类库,所谓OpenCV引擎服务即是 //OpenCV_2.4.3.2_Manager_2.4_*.apk程序包,存

从状态管理到性能优化:全面解析 Android Compose

文章目录 引言一、Android Compose基本概念1.1 什么是Android Compose?1.2 Compose的优势1.3 如何在项目中使用Compose 二、Compose中的状态管理2.1 状态管理的重要性2.2 Compose中的状态和数据流2.3 使用State和MutableState处理状态2.4 通过ViewModel进行状态管理 三、Compose中的列表和滚动

Android 10.0 mtk平板camera2横屏预览旋转90度横屏拍照图片旋转90度功能实现

1.前言 在10.0的系统rom定制化开发中,在进行一些平板等默认横屏的设备开发的过程中,需要在进入camera2的 时候,默认预览图像也是需要横屏显示的,在上一篇已经实现了横屏预览功能,然后发现横屏预览后,拍照保存的图片 依然是竖屏的,所以说同样需要将图片也保存为横屏图标了,所以就需要看下mtk的camera2的相关横屏保存图片功能, 如何实现实现横屏保存图片功能 如图所示: 2.mtk

android应用中res目录说明

Android应用的res目录是一个特殊的项目,该项目里存放了Android应用所用的全部资源,包括图片、字符串、颜色、尺寸、样式等,类似于web开发中的public目录,js、css、image、style。。。。 Android按照约定,将不同的资源放在不同的文件夹中,这样可以方便的让AAPT(即Android Asset Packaging Tool , 在SDK的build-tools目

Android fill_parent、match_parent、wrap_content三者的作用及区别

这三个属性都是用来适应视图的水平或者垂直大小,以视图的内容或尺寸为基础的布局,比精确的指定视图的范围更加方便。 1、fill_parent 设置一个视图的布局为fill_parent将强制性的使视图扩展至它父元素的大小 2、match_parent 和fill_parent一样,从字面上的意思match_parent更贴切一些,于是从2.2开始,两个属性都可以使用,但2.3版本以后的建议使