Android动画大合集

article/2025/8/21 21:00:57

  android中的动画实现方式繁多,在项目中也经常用到动画,网上有很多人也都进行了一些总结,但是感觉还是零零散散,自己总结一下才能加深印象,以后有时间了,也可以从各个分类里进一步去补充完善。

如果喜欢看Google官方英文文档的,可以直接移步Animations and Transitions

文章目录

  • 一、View Animation
    • 1、Tween Animation 补间动画
    • 2、Frame Animation(Drawable Animation)逐帧动画
  • 二、Property Animation 属性动画
    • 1、ValueAnimator
    • 2、ValueAnimator
  • 三、Spring Animation 弹性动画
  • 四、Fling Animation
  • 五、 Layout Animation 布局动画
  • 六、 Layout Transition 布局转场动画
  • 七、 Activity transition Activity转场动画
    • 1、使用transition启动一个Activity
    • 2、shared element共享元素动画
  • 八、用ConstraintLayout和ConstraintSet实现动画
  • 九、用MotionLayout实现动画

一、View Animation

1、Tween Animation 补间动画

  这类动画比较简单,一般就是平移、缩放、旋转、透明度,或者其组合,可以用代码或者xml文件的形式,推荐使用xml文件形式,因为可以复用。

  四个动画效果实现类:TranslateAnimation、ScaleAnimation、RotateAnimation、AlphaAnimation、AnimationSet,对应的的XML标签为translate、 scale、 rotate、alpha、set,其中set里还可以放set,然后放在放置在res/anim/目录下,关于详细使用这里不再做介绍。

<set xmlns:android="http://schemas.android.com/apk/res/android"android:interpolator="@[package:]anim/interpolator_resource"android:shareInterpolator=["true" | "false"] ><alphaandroid:fromAlpha="float"android:toAlpha="float" /><scaleandroid:fromXScale="float"android:toXScale="float"android:fromYScale="float"android:toYScale="float"android:pivotX="float"android:pivotY="float" /><translateandroid:fromXDelta="float"android:toXDelta="float"android:fromYDelta="float"android:toYDelta="float" /><rotateandroid:fromDegrees="float"android:toDegrees="float"android:pivotX="float"android:pivotY="float" /><set>...</set>
</set>

代码中使用:

ImageView image = (ImageView) findViewById(R.id.image);
Animation hyperspaceJump = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump);
image.startAnimation(hyperspaceJump);
AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(myContext,R.animator.property_animator);
set.setTarget(myObject);
set.start();

参考网上的一个停止补间动画的正确姿势:

public void stopAnimation(View v) {v.clearAnimation();if (canCancelAnimation()) {v.animate().cancel();    }    animation.setAnimationListener(null);    v.setAnimation(null);
}/*** Returns true if the API level supports canceling existing animations via the * ViewPropertyAnimator, and false if it does not * @return true if the API level supports canceling existing animations via the * ViewPropertyAnimator, and false if it does not */
public static boolean canCancelAnimation() {return Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH;
}

2、Frame Animation(Drawable Animation)逐帧动画

  如果要做很炫酷的下来刷新或者loading动画,可以考虑使用Airbnb开源的动画框架Lottie
  简单讲就是把几个静态的图片快速播放形成动画,可以使用AnimationDrawable,官方推荐使用XML文件,放在res/drawable/路径下,
代码如下所示:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"android:oneshot="true"><item android:drawable="@drawable/thrust1" android:duration="200" /><item android:drawable="@drawable/thrust2" android:duration="200" /><item android:drawable="@drawable/thrust3" android:duration="200" />
</animation-list>

  android:oneshot="true"表示只执行一次动画,false表示无限循环,

ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
rocketImage.setBackgroundResource(R.drawable.rocket_thrust);
rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
rocketImage.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {rocketAnimation.start();}});

  注意调用AnimationDrawable的start方法时,不能在Activity的onCreat里,因为这时AnimationDrawable还没有完全依附到window上,如果一开始就想执行动画,可以放在onStart方法里

  还有一种矢量动画AnimatedVectorDrawable, 感兴趣的可以查看这里
https://developer.android.google.cn/guide/topics/graphics/drawable-animation

二、Property Animation 属性动画

  属性动画,顾名思义,只要对象里有属性的set和get方法,就可以利用这个属性去做动画。
  上面介绍的View Animation都是对View进行的操作,准确的说,是改变View的绘制,并没有改变View对象本身的属性值,且只有一些基本的动画效果,不能改变background color等,属性动画可以对任何对象进行改变(我好像还没遇到这种场景)。官网介绍,相比Property Animation,View Animation启动所需的时间少,要写的代码也少,所以,如果能用视图动画搞定的动画,就没有必要使用属性动画了。

  在android.view.animation包里定义的差值器interpolators可以用在属性动画中。

  属性动画使用Animator的子类,通常是ValueAnimator和ObjectAnimator ,ValueAnimator只能计算属性值的变化,可以设置监听然后自己处理相关逻辑;ObjectAnimator可以对对象的属性值计算后,直接应用于对象的相应属性。

1、ValueAnimator

ValueAnimator animation = ValueAnimator.ofFloat(0f, 100f);
animation.setDuration(1000);
animation.start();

  或者对自定义的类型进行动画:

ValueAnimator fadeAnim = ObjectAnimator.ofFloat(newBall, "alpha", 1f, 0f);
fadeAnim.setDuration(250);
fadeAnim.addListener(new AnimatorListenerAdapter() {
public void onAnimationEnd(Animator animation) {balls.remove(((ObjectAnimator)animation).getTarget());
}
fadeAnim.start();animation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {@Overridepublic void onAnimationUpdate(ValueAnimator updatedAnimation) {// You can use the animated value in a property that uses the// same type as the animation. In this case, you can use the// float value in the translationX property.float animatedValue = (float)updatedAnimation.getAnimatedValue();textView.setTranslationX(animatedValue);}
});

2、ValueAnimator

  ObjectAnimator和AnimatorSet的使用:

ObjectAnimator.ofFloat(targetObject, "propName", 1f)AnimatorSet bouncer = new AnimatorSet();
bouncer.play(bounceAnim).before(squashAnim1);
bouncer.play(squashAnim1).with(squashAnim2);
bouncer.play(squashAnim1).with(stretchAnim1);
bouncer.play(squashAnim1).with(stretchAnim2);
bouncer.play(bounceBackAnim).after(stretchAnim2);
ValueAnimator fadeAnim = ObjectAnimator.ofFloat(newBall, "alpha", 1f, 0f);
fadeAnim.setDuration(250);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(bouncer).before(fadeAnim);
animatorSet.start();

  在一次需要修改多个属性值时,除了可以使用AnimatorSet, 还可以使用语法更简洁的ViewPropertyAnimator,同时ViewPropertyAnimator也更高效,代码对比如下:

   Multiple ObjectAnimator objects

ObjectAnimator animX = ObjectAnimator.ofFloat(myView, "x", 50f);
ObjectAnimator animY = ObjectAnimator.ofFloat(myView, "y", 100f);
AnimatorSet animSetXY = new AnimatorSet();
animSetXY.playTogether(animX, animY);
animSetXY.start();

   One ObjectAnimator

PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("x", 50f);
PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("y", 100f);
ObjectAnimator.ofPropertyValuesHolder(myView, pvhX, pvyY).start();

  ViewPropertyAnimator

myView.animate().x(50f).y(100f);

  关于ViewPropertyAnimator的更多介绍,可以查看官网介绍 blog post.

三、Spring Animation 弹性动画

  这类动画是模仿现实中的弹力效果,每一帧都是通过计算相应的弹力=得到,可以设置弹性动画的阻尼率和刚度来达到不同的弹性效果。

添加依赖

dependencies {implementation 'com.android.support:support-dynamic-animation:27.1.1'}

以下是一个应用了链式弹性动画Chained spring的效果图,
这里写图片描述

  弹性动画使用SpringAnimation类,具体可参考官网介绍:Animate movement using spring physics 或者 Android中弹簧动画的那些事 - SpringAnimation

四、Fling Animation

  Fling Animation类似于列表ListView在快速滚动时到最后列表停止滚动的效果。

这里写图片描述

  Fling Animation使用FlingAnimation类,具体可参考官网介绍:Move views using a fling animation

五、 Layout Animation 布局动画

  当改变一个布局的元素时,android提供了一种预加载动画,使用默认的布局动画,只需要对一个layout设置一个属性,如下所示:

<LinearLayout android:id="@+id/container"android:animateLayoutChanges="true"...
/>

当对这个LinearLayout中添加、删除或者更新子View时,这些子View就会自动添加上默认的动画效果。

private ViewGroup mContainerView;
...
private void addItem() {View newView;...mContainerView.addView(newView, 0);
}

除了默认的布局动画,也可以自定义动画,并使用setAnimator方法设置给ViewGroup,以后有时间可以仔细研究一下,

public void setAnimator (int transitionType, Animator animator)

六、 Layout Transition 布局转场动画

  转场动画可以在两个布局之间,产生动画效果,步骤如下:

  1.根据其实布局和结束布局,创建Scene对象,
  2.创建一个Transition对象,用来定义所需的动画类型
  3.调用TransitionManager.go(),然后系统就在两个布局之间产生动画

这里写图片描述

  更多内容请参考 Animate layout changes using a transition 和
Create a custom transition animation

七、 Activity transition Activity转场动画

  从Android5.0(API21)开始,可以使用 Activity transition,所以要注意低版本的效果适配。

1、使用transition启动一个Activity

  当给一个Activity指定了退出的转场动画,在启动一个新的Activity时,将调用动画效果,如果新启动的Activity也设置了进入的转场动画,那么在启动时,也会产生相应的动画,要禁用动画效果时,传一个空的bundle

  使用Activity转场动画,要在使用的theme里,将android:windowActivityTransitions设置为true

<style name="BaseAppTheme" parent="android:Theme.Material"><!-- enable window content transitions --><item name="android:windowActivityTransitions">true</item><!-- specify enter and exit transitions --><item name="android:windowEnterTransition">@transition/explode</item><item name="android:windowExitTransition">@transition/explode</item><!-- specify shared element transitions --><item name="android:windowSharedElementEnterTransition">@transition/change_image_transform</item><item name="android:windowSharedElementExitTransition">@transition/change_image_transform</item>
</style>

  然后在代码中

// inside your activity (if you did not enable transitions in your theme)
getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);// set an exit transition
getWindow().setExitTransition(new Explode());startActivity(intent,ActivityOptions.makeSceneTransitionAnimation(this).toBundle());

2、shared element共享元素动画

  可以共享一个或者多个元素,简要代码如下,

// get the element that receives the click event
final View imgContainerView = findViewById(R.id.img_container);// get the common element for the transition in this activity
final View androidRobotView = findViewById(R.id.image_small);// define a click listener
imgContainerView.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Intent intent = new Intent(this, Activity2.class);// create the transition animation - the images in the layouts// of both activities are defined with android:transitionName="robot"ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(this, androidRobotView, "robot");// start the new activitystartActivity(intent, options.toBundle());}
});

  多个共享元素时:

ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(this,Pair.create(view1, "agreedName1"),Pair.create(view2, "agreedName2"));

推荐博文:
【Transition】Android炫酷的Activity切换效果,共享元素

八、用ConstraintLayout和ConstraintSet实现动画

  这个放在最后写,因为感觉最牛逼啊,其实和上面介绍到的Layout Transition 布局转场动画很相似,先是用ConstraintLayout写两个布局文件,然后利用TransitionManager调用beginDel ayedTransition()启动一个延时转场动画就行,且ConstraintLayout支持到API 9,TransitionManager支持到API 14,所以多使用ConstraintSet来做动画,兼容性不用担心,除了在两个布局里转场,在一个布局内改变某个View的属性(如边距),其余的View也会自动产生平滑的动画效果,其他什么动画相关代码都不用写,,,,,惊不惊喜!

使用ConstraintLayout制作漂亮的动画

ConstraintLayout 炫酷的动画

  利用给Transition设置相应的差值器Interpolator,就能产生更加fashion的效果,感兴趣的同学可以观看以下视频教程:

  用四行代码来实现复杂动画 | 中文教学视频

//2018.12.23 更新

九、用MotionLayout实现动画

MotionLayout 因何而生?
Android 框架已经提供了好几种在 App 里添加动画的方式:

Animated Vector Drawable
Property Animation framework
LayoutTransition animations
Layout transitions with TransitionManager
CoordinatorLayout

下面将揭示 MotionLayout 和上面这些已有方案的不同。

暂时没事时间研究,先把网址贴一下:

『译』ConstrainLayout 2.0 之 MotionLayout(Part1.1)

【翻译】MotionLayout实现折叠工具栏(Part 1)
【翻译】MotionLayout实现折叠工具栏(Part 2)


http://chatgpt.dhexx.cn/article/xarTBqon.shtml

相关文章

Android中动画详细讲解

一、前言 Android动画经常会在切换activity、数据加载时会用到动画效果&#xff0c;以前接触的比较少&#xff0c;用的都是封装好的动画效果&#xff0c;自己写的比较少&#xff0c;今天心血来潮想写一个自己设计的动画效果&#xff0c;发现学习动画代码比较简单&#xff0c;但…

android 动画中插值器Interpolator详解

1、插值器简介–Interpolator 通俗易懂的说&#xff0c;Interpolator负责控制动画变化的速率&#xff0c;即确定了 动画效果变化的模式&#xff0c;使得基本的动画效果能够以匀速、加速、减速、抛物线速率等各种速率变化 动画是开发者给定开始和结束的“关键帧”&#xff0c;…

Android中的属性动画

1.属性动画简介 接下来我们学习Android动画中的第三种动画——属性动画(Property Animation) Animation一般动画就是我们前面学的帧动画和补间动画&#xff01;Animator则是本节要讲的属性动画&#xff01; 1.1为什么要用属性动画 补间动画功能比较单调,只有四种动画(透明度…

android 动画库

1. Spruce(安卓动画库)&#xff08;是一个轻量级的动画库&#xff0c;可以帮助排版屏幕上的动画。使用有很多不同的动画库时&#xff0c;开发人员需要确保每个视图都能够在适当的时间活动。&#xff09; 2. Litho&#xff08;是一个非常强大的框架&#xff0c;以声明的方式构建…

android动画类型有哪几种,Android动画概念大揭秘

前言 说起Android里面的动画,我可能会立马想起平移、旋转、渐变、缩放等动画效果,但是对于他们的属性就记的不太清了,知道的都是皮毛而且很容易忘记,每次需要用到的时候总要去baidu或者google,完全无法做到灵活应用,信手拈来。所以抽时间重新温故了一下动画相关的知识,把…

Android 动画分类

前言 动画的使用 是 Android 开发中常用的知识可是动画的种类繁多、使用复杂&#xff0c;每当需要 采用自定义动画 实现 复杂的动画效果时&#xff0c;很多开发者就显得束手无策本文将献上一份Android动画的全面介绍攻略&#xff0c;包括动画的种类、使用、原理等&#xff0c;能…

Android动画

这篇博客主要总结一下自己在项目中对动画的一些使用。我写博客&#xff0c;其实更多的是总结自己在工作中用到的一些知识。比如某一段时间一个知识点相关的技术用的比较多&#xff0c;那我会总结一下。其实&#xff0c;对于Android动画的总结&#xff0c;这是几个月前就应该写完…

Android动画之帧动画

在Android开发时&#xff0c;为了实现一些动态的炫酷的效果&#xff0c;我们常用到帧动画&#xff0c;View动画&#xff08;补间动画&#xff09;和属性动画&#xff0c;今天就来总结下我在使用帧动画的实现方式。 1、什么是帧动画&#xff1f; 帧动画就是顺序播放一组预先定…

【Android】动画

概念 动画实际上就是在指定的时间段内持续的修改某个属性的值&#xff0c;使得该值在指定取值范围之内平滑的过渡 android中的动画分为&#xff1a;View动画、帧动画和属性动画 帧动画 Frame动画是一系列图片按照一定的顺序展示的过程&#xff0c;它的原理是在一定的时间段内切…

STM32 LWIP SNTP实现毫秒级的时间校准

1、首先配置LWIP支持SNTP 然后在opt.h中增加一个timeout->LWIP_SNTP 防止出现类似 Assertion "sys_timeout: timeout ! NULL, pool MEMP_SYS_TIMEOUT is empty" failed at line 190 in ../Middlewares/Third_Party/LwIP/src/core/timeouts.c 这样的错误。 /*…

NTP/SNTP协议介绍和校时服务器搭建

文|Seraph 本文主要简单介绍用于校时的NTP/SNTP协议 同时&#xff0c;以windows 2008 R2为例&#xff0c;搭建NTP/SNTP服务器 NTP可参考文献RFC1305&#xff0c;SNTP可参考文献RFC1796 1. 应用场景 一般应用&#xff0c;连上公网即可通过NTP/SNTP协议进行校时&#xff0c;例如…

linux sntp 代码,C语言window(linux)平台的SNTP实现

C语言实现window(linux)平台的SNTP&#xff0c;本程序功能主要是实现电脑(或者设备)时间同步。摘录部分代码&#xff1a; unsigned char liVnMode; /* LeapSecond(2bits:0), VersionNumber(3bits: 3), Mode(3bits: Client3, Server4) */ unsigned char stratum; /* 时间层级 (0…

学习日记——ESP8266SNTP

SNTP基本知识 1、定义 SNTP是简单网络时间协议&#xff0c;而NTP网络时间协议就是网络计算机上同步计算时间的协议&#xff0c;具有高度的精确性&#xff0c;实际上也用不到这么高精度的算法。所以就在NTP上简化了以下变成SNTP&#xff0c;SNTP协议主要被用来同步因特网上计算…

WiFi开发|ESP8266模组SDK开发之SNTP协议

ESP8266模组SDK开发之SNTP协议 1. NTP和SNTP NTP 是网络时间协议&#xff08;Network Time Protocol&#xff09;&#xff0c;是用来同步网络设备&#xff08;如计算机、手机&#xff09;的时间的协议 SNTP由NTP改编而来&#xff08;简单网络时间协议&#xff0c;Simple Net…

ESP8266学习笔记(11)——SNTP接口使用

一、SNTP简介 简单网络时间协议&#xff08;Simple Network Time Protocol&#xff09;&#xff0c;由 NTP 改编而来&#xff0c;主要用来同步因特网中的计算机时钟 二、SNTP接口 SNTP 接口位于 ESP8266_NONOS_SDK/include/sntp.h。 三、初始化SNTP 设置三个时间服务器…

ESP32 SNTP配置

SNTP&#xff0c;Simple Network Time Protocol&#xff08;简单网络时间协议&#xff09;&#xff0c;用来同步时钟。 百度百科&#xff1a;SNTP SNTP协议采用客户端/服务器的工作方式&#xff0c;可以采用单播&#xff08;点对点&#xff09;或者广播&#xff08;一点对多点&…

通过sntp同步系统时间

通过sntp同步系统时间 小型物联网设备&#xff0c;很少有接口提供给用户进行数据交互&#xff0c;那么我们设备的系统时间只能够通过获取网络时间后&#xff0c;再更新到本地。那么&#xff0c;就少不了使用sntp协议。 ntp协议&#xff1a;NTP(Network Time Protocol&#xff…

2012系统sntp服务器,如何设置SNTP服务器,实现同步PLC时钟

在过程/生产自动化系统构架和运行时&#xff0c;有时需要实现对时间的精确控制&#xff0c;即系统中所有子系统(包括设备等)的时间必须保持同步。 作为自动化系统组成部分的 以Windows 7操作系统为例&#xff0c;将PC机配置为SNTP服务器端的操作步骤&#xff1a; [注意]操作前前…

ESP32学习笔记(41)——SNTP接口使用

一、SNTP简介 简单网络时间协议&#xff08;Simple Network Time Protocol&#xff09;&#xff0c;由 NTP 改编而来&#xff0c;主要用来同步因特网中的计算机时钟。 SNTP 协议是用来同步本地的时间到 unix 时间戳。通常嵌入式设备上电&#xff0c;连接 AP(access point)&…

物联网专题27:SNTP

什么是SNTP&#xff08;Simple Network Time Protocol&#xff09;&#xff1f;简单时钟控制协议&#xff0c;主要用来同步因特网中的计算机时钟。 SNTP&#xff0c;使用的默认端口号是 UDP123。 ESP8266中&#xff0c;SNTP相关的API&#xff1a; 1 设置SNTP服务器&#xff08…