Android 动画系列二之补间动画

article/2025/8/21 20:59:19

1. 前言

        Android三种动画中的第二种——补间动画(Tween),和帧动画不同,帧动画 是通过连续播放图片来模拟动画效果,而补间动画开发者只需指定动画开始,以及动画结束"关键帧", 而动画变化的"中间帧"则由系统计算并补齐。如下为示意图:

         对于补间动画而言,开发者无需“逐一”定义的过程中的每一帧,他只需要定义动画的开始和结束的关键帧,并指定动画的持续时间即可。

2. 补间动画的分类

        AlphaAnimation:透明度渐变效果,创建时许指定开始以及结束透明度,还有动画的持续 时间,透明度的变化范围(0,1),0是完全透明,1是完全不透明;对应<alpha/>标签!

        ScaleAnimation:缩放渐变效果,创建时需指定开始以及结束的缩放比,以及缩放参考点, 还有动画的持续时间;对应<scale/>标签!

        TranslateAnimation:位移渐变效果,创建时指定起始以及结束位置,并指定动画的持续 时间即可;对应<translate/>标签!

        RotateAnimation:旋转渐变效果,创建时指定动画起始以及结束的旋转角度,以及动画 持续时间和旋转的轴心;对应<rotate/>标签

       AnimationSet:组合渐变,就是前面多种渐变的组合,对应<set/>标签

3. Interpolator

        在开始讲解各种动画的用法之前,我们先要来讲解一个东西:Interpolator

        用来控制动画的变化速度,可以理解成动画渲染器,当然我们也可以自己实现Interpolator 接口,自行来控制动画的变化速度,而Android中已经为我们提供了五个可供选择的实现类:

        LinearInterpolator:动画以均匀的速度改变

        AccelerateInterpolator:在动画开始的地方改变速度较慢,然后开始加速

        AccelerateDecelerateInterpolator:在动画开始、结束的地方改变速度较慢,中间时加速

        CycleInterpolator:动画循环播放特定次数,变化速度按正弦曲线改变: Math.sin(2 * mCycles * Math.PI * input)

        DecelerateInterpolator:在动画开始的地方改变速度较快,然后开始减速

        AnticipateInterpolator:反向,先向相反方向改变一段再加速播放

        AnticipateOvershootInterpolator:开始的时候向后然后向前甩一定值后返回最后的值

        BounceInterpolator: 跳跃,快到目的值时值会跳跃,如目的值100,后面的值可能依次为85,77,70,80,90,100

        OvershottInterpolator:回弹,最后超出目的值然后缓慢改变到目的值

而这个东东,我们一般是在写动画xml文件时会用到,属性是:android:interpolator, 而上面对应的值是:@android:anim/linear_interpolator,其实就是驼峰命名法变下划线而已 AccelerateDecelerateInterpolator对应:@android:anim/accelerate_decelerate_interpolator!

4. 动画详解

4.1 AlphaAnimation 透明度渐变

        anim_alpha.xml:

<alpha xmlns:android="http://schemas.android.com/apk/res/android"  android:interpolator="@android:anim/accelerate_decelerate_interpolator"  android:fromAlpha="1.0"  android:toAlpha="0.1"  android:duration="2000"/>

属性解释:

fromAlpha :起始透明度
toAlpha:结束透明度
透明度的范围为:0-1,完全透明-完全不透明

4.2 ScaleAnimation(缩放渐变)

        anim_scale.xml:

<scale xmlns:android="http://schemas.android.com/apk/res/android"  android:interpolator="@android:anim/accelerate_interpolator"  android:fromXScale="0.2"  android:toXScale="1.5"  android:fromYScale="0.2"  android:toYScale="1.5"  android:pivotX="50%"  android:pivotY="50%"  android:duration="2000"/>

属性解释:

        fromXScale/fromYScale:沿着X轴/Y轴缩放的起始比例

        toXScale/toYScale:沿着X轴/Y轴缩放的结束比例

        pivotX/pivotY:缩放的中轴点X/Y坐标,即距离自身左边缘的位置,比如50%就是以图像的 中心为中轴点

4.3 TranslateAnimation(位移渐变)

        anim_translate.xml:

<translate xmlns:android="http://schemas.android.com/apk/res/android"  android:interpolator="@android:anim/accelerate_decelerate_interpolator"  android:fromXDelta="0"  android:toXDelta="320"  android:fromYDelta="0"  android:toYDelta="0"  android:duration="2000"/>

属性解释:

fromXDelta/fromYDelta:动画起始位置的X/Y坐标

toXDelta/toYDelta:动画结束位置的X/Y坐标

4.4 RotateAnimation(旋转渐变)

        anim_rotate.xml:

<rotate xmlns:android="http://schemas.android.com/apk/res/android"  android:interpolator="@android:anim/accelerate_decelerate_interpolator"  android:fromDegrees="0"  android:toDegrees="360"  android:duration="1000"  android:repeatCount="1"  android:repeatMode="reverse"/> 

属性解释:

        fromDegrees/toDegrees:旋转的起始/结束角度

        repeatCount:旋转的次数,默认值为0,代表一次,假如是其他值,比如3,则旋转4次 另外,值为-1或者infinite时,表示动画永不停止

        repeatMode:设置重复模式,默认restart,但只有当repeatCount大于0或者infinite或-1时 才有效。还可以设置成reverse,表示偶数次显示动画时会做方向相反的运动!

4.5 AnimationSet(组合渐变)

就是前面几个动画组合到一起而已, anim_set.xml:

<set xmlns:android="http://schemas.android.com/apk/res/android"  android:interpolator="@android:anim/decelerate_interpolator"  android:shareInterpolator="true" >  <scale  android:duration="2000"  android:fromXScale="0.2"  android:fromYScale="0.2"  android:pivotX="50%"  android:pivotY="50%"  android:toXScale="1.5"  android:toYScale="1.5" />  <rotate  android:duration="1000"  android:fromDegrees="0"  android:repeatCount="1"  android:repeatMode="reverse"  android:toDegrees="360" />  <translate  android:duration="2000"  android:fromXDelta="0"  android:fromYDelta="0"  android:toXDelta="320"  android:toYDelta="0" />  <alpha  android:duration="2000"  android:fromAlpha="1.0"  android:toAlpha="0.1" />  </set>  

5. Demo

        布局文件 activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><Buttonandroid:id="@+id/btn_alpha"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="透明度渐变" /><Buttonandroid:id="@+id/btn_scale"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="缩放渐变" /><Buttonandroid:id="@+id/btn_tran"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="位移渐变" /><Buttonandroid:id="@+id/btn_rotate"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="旋转渐变" /><Buttonandroid:id="@+id/btn_set"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="组合渐变" /><ImageViewandroid:id="@+id/img_show"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:layout_marginTop="48dp"android:src="@mipmap/ic_launcher" /></LinearLayout>

接着到我们的MainActivity.java,同样非常简单,只需调用AnimationUtils.loadAnimation() 加载动画,然后我们的View控件调用startAnimation开启动画即可

public class MainActivity extends AppCompatActivity implements View.OnClickListener{private Button btn_alpha;private Button btn_scale;private Button btn_tran;private Button btn_rotate;private Button btn_set;private ImageView img_show;private Animation animation = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);bindViews();}private void bindViews() {btn_alpha = (Button) findViewById(R.id.btn_alpha);btn_scale = (Button) findViewById(R.id.btn_scale);btn_tran = (Button) findViewById(R.id.btn_tran);btn_rotate = (Button) findViewById(R.id.btn_rotate);btn_set = (Button) findViewById(R.id.btn_set);img_show = (ImageView) findViewById(R.id.img_show);btn_alpha.setOnClickListener(this);btn_scale.setOnClickListener(this);btn_tran.setOnClickListener(this);btn_rotate.setOnClickListener(this);btn_set.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()){case R.id.btn_alpha:animation = AnimationUtils.loadAnimation(this,R.anim.anim_alpha);img_show.startAnimation(animation);break;case R.id.btn_scale:animation = AnimationUtils.loadAnimation(this,R.anim.anim_scale);img_show.startAnimation(animation);break;case R.id.btn_tran:animation = AnimationUtils.loadAnimation(this,R.anim.anim_translate);img_show.startAnimation(animation);break;case R.id.btn_rotate:animation = AnimationUtils.loadAnimation(this,R.anim.anim_rotate);img_show.startAnimation(animation);break;case R.id.btn_set:animation = AnimationUtils.loadAnimation(this,R.anim.anim_set);img_show.startAnimation(animation);break;}}
}

 两个比较重要的方法:

1. AnimationUtils.loadAnimation(this,R.anim.anim_scale);

/*** Loads an {@link Animation} object from a resource** @param context Application context used to access resources* @param id The resource id of the animation to load* @return The animation object reference by the specified id* @throws NotFoundException when the animation cannot be loaded*/public static Animation loadAnimation(Context context, @AnimRes int id)throws NotFoundException {XmlResourceParser parser = null;try {parser = context.getResources().getAnimation(id);return createAnimationFromXml(context, parser);} catch (XmlPullParserException ex) {NotFoundException rnf = new NotFoundException("Can't load animation resource ID #0x" +Integer.toHexString(id));rnf.initCause(ex);throw rnf;} catch (IOException ex) {NotFoundException rnf = new NotFoundException("Can't load animation resource ID #0x" +Integer.toHexString(id));rnf.initCause(ex);throw rnf;} finally {if (parser != null) parser.close();}}

2.  View.startAnimation(animation)

    /*** Start the specified animation now.** @param animation the animation to start now*/public void startAnimation(Animation animation) {animation.setStartTime(Animation.START_ON_FIRST_FRAME);setAnimation(animation);invalidateParentCaches();invalidate(true);}

6. 动画状态的监听

我们可以对动画的执行状态进行监听,调用动画对象的:

        setAnimationListener(new AnimationListener())方法,重写下面的三个方法:

        onAnimationStart():动画开始

        onAnimtaionRepeat():动画重复

        onAnimationEnd():动画结束

 即可完成动画执行状态的监听~

7. 为View动态设置动画效果

       如本文中的静态加载方式:先调用AnimationUtils.loadAnimation(动画xml文件),然后View控件调用startAnimation(anim) 开始动画。


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

相关文章

Android动画大合集

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

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)&…