原文首发于微信公众号:jzman-blog,欢迎关注交流!

Android 提供三种动画:帧动画、补间动画和属性动画,本篇文章介绍帧动画以及补间动画的使用,属性动画的使用将在后面的文章中分享,那就来复习一下这两种动画的使用吧。

FrameAnimation

FrameAnimation 即逐帧动画,通俗来说就是按照图片动作顺序依次播放来形成动画,创建 FrameAnimation 可用 xml 定义也可直接使用代码创建。

xml创建帧动画

在 res/drawable 文件夹下创建一个 drawable 文件,使用 animation-list 标签,具体内容如下:

<?xml version="1.0" encoding="utf-8"?>
<!--FrameAnimator-->
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item
android:drawable="@drawable/zzlx1"
android:duration="100" />
<item
android:drawable="@drawable/zzlx2"
android:duration="100" />
<item
android:drawable="@drawable/zzlx3"
android:duration="100" />
<!--...-->
</animation-list>

属性 oneshot 为 true 表示动画只能播放一次,false 表示动画循环播放,drawable 是当前动作对应的图片,duration 是其持续时间,duration 长度影响动画播放的快慢,然后在 Activity 中使用获取该 drawable 文件对应的 AnimationDrawable,然后使用 AnimationDrawable 对象来控制动画的状态,参考如下:

//获取Frame动画文件对应的AnimationDrawable
mAnimationDrawable = (AnimationDrawable) getResources().getDrawable(R.drawable.frame_animator);
//设置AnimationDrawable为图片的背景
imageView.setBackground(mAnimationDrawable); //开启动画
mAnimationDrawable.start();
//停止动画
mAnimationDrawable.stop();
代码创建帧动画

使用代码创建帧动画就是创建 AnimationDrawable 对象,然后在 AnimationDrawable 中添加对应的 Frame 即可,代码参考如下:

//代码创建Frame动画
mAnimationDrawable = new AnimationDrawable();
//设置动画循环播放,true为动画只播放一次
mAnimationDrawable.setOneShot(false);
mAnimationDrawable.addFrame(getResources().getDrawable(R.drawable.zzlx1),100);
mAnimationDrawable.addFrame(getResources().getDrawable(R.drawable.zzlx2),100);
mAnimationDrawable.addFrame(getResources().getDrawable(R.drawable.zzlx3),100);
//...
imageView.setBackground(mAnimationDrawable); //开启动画
mAnimationDrawable.start();
//停止动画
mAnimationDrawable.stop();

FrameAnimation 效果如下:

TweenAnimation

TweenAnimation 即常说的补间动画,主要有以下几种:

  1. 位移动画(Translation)
  2. 缩放动画(Scale)
  3. 旋转动画(Rotate)
  4. 透明度动画(Alpha)
  5. 组合动画

上述动画都有自己特有的一下属性,下面来看一看这些动画通用的一些属性,具体如下:

<!--设置动画持续时间-->
android:duration="1200"
<!--动画开始的延时-->
android:startOffset ="1000"
<!--动画播放完是否回到动画开始的位置,默认true,如果fillBefore设置为false,动画不会停留在结束位置,不知道是不是bug-->
android:fillBefore = "true"
<!--动画播放完之后是否回到动画结束的位置,默认false,如果fillAfter设置为true,动画则会停留在结束位置-->
android:fillAfter = "false"
<!--设置fill...属性是否启用,对fillAfter无效-->
android:fillEnabled= "true"
<!--设置动画重复模式,restart为重新播放,reverse为倒序回放,和repeatCount搭配使用-->
android:repeatMode = "restart"
<!--设置动画重复次数-->
android:repeatCount = "0"
<!--设置动画插值器,这里的插值器是动画开始速度较慢,后面加速-->
android:interpolator = "@android:anim/accelerate_interpolator"

如果在代码中进行对应动画实现,这些属性也有对应的属性设置,直接设置即可。

位移动画(Translate)

位移动画对 View 进行水平方向或垂直方向位置的平移,可指定起始位置和结束位置,可使用 xml 定义位移动画也可以使用代码创建位移动画,位移动画对应的 Animation 的子类是 TranslateAnimation。

xml定义位移动画:在 res/anim 下创建一个xml文件 translation_anim.xml,在该文件中定义位移动画如下:

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1200"
android:startOffset ="0"
android:fillBefore = "true"
android:fillAfter = "false"
android:fillEnabled= "false"
android:repeatMode = "reverse"
android:repeatCount = "5"
android:interpolator = "@android:anim/accelerate_interpolator" android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="100"
android:toYDelta="100">

上述 xml 文件定义了一个位移动画文件,其中位移动画自有的属性含义如下:

<!--水平方向动画开始的位置-->
android:fromXDelta="0"
<!--垂直方向动画开始的位置-->
android:fromYDelta="0"
<!--水平方向动画结束的位置-->
android:toXDelta="100"
<!--垂直方向动画结束的位置-->
android:toYDelta="100"

然后在 Activity 中获取该 xml 文件对应的 TranslateAnimation,将其设置到想要设置位移动画的 View 上即可,具体如下:

private void translation(){
//获取在anim下定义的动画文件
TranslateAnimation translateAnimation = (TranslateAnimation) AnimationUtils.loadAnimation(this, R.anim.translation_anim);、
//设置并开启动画
ivImage.startAnimation(translateAnimation);
}

代码中创建位移动画:代码创建位移动画使用 Animation 的子类 TranslateAnimation,使用时直接创建 TranslateAnimation 对象即可,具体如下:

//代码创建位移动画
private void translation(){
//表示相对View自身原点(View左上角)像素偏移量
TranslateAnimation translateAnimation = new TranslateAnimation(0,100,0,100);
//设置动画持续时间
translateAnimation.setDuration(1200);
//设置动画重复模式
translateAnimation.setRepeatMode(Animation.REVERSE);
//设置动画重复次数
translateAnimation.setRepeatCount(3);
translateAnimation.setFillAfter(true);
//设置动画插值器
translateAnimation.setInterpolator(this,android.R.anim.accelerate_interpolator);
// translateAnimation.setInterpolator(new AccelerateInterpolator());
//...
ivImage.startAnimation(translateAnimation);
}

上面参数中使用的时像素的偏移量,API 还提供了针对 View 自身一个父 View 的百分比的设置方式,下面这种创建 TranslateAnimation 对象的方式和上面实现的效果是一样的。具体如下:

/**
* ABSOLUTE:表示相对View自身原点(View左上角)像素偏移量
* 此时和TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)一样
* RELATIVE_TO_SELF:表示相对View自身的百分比,如0.5f表示View自身大小的50%,1.0f表示View自身大小
* RELATIVE_TO_PARENT:表示相对父View的百分比,如0.5f表示View自身大小的50%,1.0f表示View自身大小
*/
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0.46f,
Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0.46f);

使用时可根据需要选择合适的构造方式创建 TranslateAnimation,测试效果如下:

缩放动画(Scale)

缩放动画对 View 就是对视图进行一定程度的放大和缩小,可使用 xml 定义位移动画也可以使用代码创建位移动画,缩放动画对应的 Animation 的子类是 ScaleAnimation。

xml定义缩放动画:在 res/anim 下创建一个 xml 文件 scale_anim.xml,在里面定义缩放动画,具体如下:

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1200"
android:startOffset ="0"
android:fillBefore = "true"
android:fillAfter = "false"
android:fillEnabled= "false"
android:repeatMode = "reverse"
android:repeatCount = "3"
android:interpolator = "@android:anim/accelerate_interpolator" android:fromXScale="1"
android:fromYScale="1"
android:toXScale="3"
android:toYScale="3"
android:pivotX="50%"
android:pivotY="50%">
</scale>

上述 xml 文件定义了一个缩放动画文件,其中缩放动画自有的属性含义如下:

<!--设置水平方向上的起始缩放倍数-->
android:fromXScale="1"
<!--设置垂直方向上的起始缩放倍数-->
android:fromYScale="1"
<!--设置水平方向上的结束缩放倍数-->
android:toXScale="3"
<!--设置垂直方向上的结束缩放倍数-->
android:toYScale="3"
<!--设置缩放中心水平方向上的坐标-->
android:pivotX="50%"
<!--设置缩放中心垂直方向上的坐标-->
android:pivotY="50%">

其中 pivotX 和 pivotY 有三种设置方式:

  • 数字:如50表示缩放中心相较 View 原点偏移 50px
  • 百分比:如 50% 表示缩放中心相较 View 原点偏移 View 自身大小的 50%
  • 百分比p:如 50%p 表示缩放中心相较 View 原点偏移父 View 自身大小的 50%

然后在 Activity 中获取该 xml 文件对应的 ScaleAnimation,将其设置到想要设置位移动画的 View 上即可,具体如下:

private void scale(){
ScaleAnimation scaleAnimation = (ScaleAnimation) AnimationUtils.loadAnimation(this,R.anim.scale_anim);
ivImage.startAnimation(scaleAnimation);
}

代码创建缩放动画:代码创建缩放动画使用 Animation 的子类 ScaleAnimation,使用时直接创建 ScaleAnimation 对象即可,具体如下:

//代码创建缩放动画
private void scale(){
ScaleAnimation scaleAnimation = new ScaleAnimation(1,3,1,3,
Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
scaleAnimation.setRepeatMode(Animation.REVERSE);
scaleAnimation.setDuration(500);
scaleAnimation.setRepeatCount(5);
scaleAnimation.setInterpolator(this,android.R.anim.accelerate_decelerate_interpolator);
// translateAnimation.setInterpolator(new AccelerateInterpolator());
//...
ivImage.startAnimation(scaleAnimation);
}

至于参数中的 pivotXType 和 pivotYType 和在上文中已经提到过,这里就不在赘述,测试效果如下:

旋转动画(Rotate)

旋转动画对 View 就是对视图角度进行旋转,可使用 xml 定义旋转动画也可以使用代码创建旋转动画,旋转动画对应的 Animation 的子类是 RotateAnimation。

xml定义旋转动画:在 res/anim 下创建一个 xml 文件 rotate_anim.xml,在里面定义缩放动画,具体如下:

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1200"
android:startOffset ="0"
android:fillBefore = "true"
android:fillAfter = "false"
android:fillEnabled= "false"
android:repeatMode = "reverse"
android:repeatCount = "5"
android:interpolator = "@android:anim/accelerate_interpolator" android:fromDegrees="0"
android:toDegrees="100"
android:pivotY="50%"
android:pivotX="50%">
</rotate>

上述 xml 文件定义了一个旋转动画文件,其中缩放动画自有的属性含义如下:

<!--设置动画开始时的角度,正数表示顺时针,负数表示逆时针-->
android:fromDegrees="0"
<!--设置动画结束时的角度,正数表示顺时针,负数表示逆时针-->
android:toDegrees="100"
<!--设置水平方向旋转中心点的坐标-->
android:pivotY="50%"
<!--设置垂直方向旋转中心点的坐标-->
android:pivotX="50%"

其中 pivotX 和 pivotY 有三种设置方式在上文中已经说明。然后在 Activity 中获取该 xml 文件对应的 RotateAnimation,将其设置到想要设置旋转动画的 View 上即可,具体如下:

private void rotate(){
RotateAnimation rotateAnimation = (RotateAnimation) AnimationUtils.loadAnimation(this,R.anim.rotate_anim);
ivImage.startAnimation(rotateAnimation);
}

代码创建旋转动画:代码创建旋转动画使用 Animation 的子类 RotateAnimation,使用时直接创建 RotateAnimation 对象即可,具体如下:

//代码创建旋转动画
private void rotate(){
RotateAnimation rotateAnimation = new RotateAnimation(0,100,
Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
rotateAnimation.setRepeatMode(Animation.REVERSE);
rotateAnimation.setDuration(1200);
rotateAnimation.setRepeatCount(3);
rotateAnimation.setInterpolator(this,android.R.anim.accelerate_decelerate_interpolator);
// translateAnimation.setInterpolator(new AccelerateInterpolator());
//...
ivImage.startAnimation(rotateAnimation);
}

测试效果如下:

透明度动画(Alpha)

透明度动画就是修改 View 的透明度,可使用 xml 定义透明度动画也可以使用代码创建透明度动画,透明度动画对应的 Animation 的子类是 AlphaAnimation。

xml定义透明度动画:在 res/anim 下创建一个 xml 文件 alpha_anim.xml,在里面定义缩放动画,具体如下:

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000"
android:startOffset ="0"
android:fillBefore = "true"
android:fillAfter = "true"
android:fillEnabled= "false"
android:repeatMode = "restart"
android:repeatCount = "0"
android:interpolator = "@android:anim/accelerate_interpolator" android:fromAlpha="1"
android:toAlpha="0">
</alpha>

上述 xml 文件定义了一个透明度动画文件,其中透明度动画自有的属性含义如下:

<!--设置动画的开始透明度,0表示透明,1表示不透明-->
android:fromAlpha="1"
<!--设置动画的结束透明度,0表示透明,1表示不透明-->
android:toAlpha="0"

然后在 Activity 中获取该 xml 文件对应的 AlphaAnimation,将其设置到想要设置旋转动画的 View 上即可,具体如下:

private void alpha(){
AlphaAnimation alphaAnimation = (AlphaAnimation) AnimationUtils.loadAnimation(this,R.anim.alpha_anim);
ivImage.startAnimation(alphaAnimation);
}

代码创建透明度动画:代码创建透明度动画使用 Animation 的子类 AlphaAnimation,使用时直接创建 AlphaAnimation 对象即可,具体如下:

//代码创建透明度动画
private void alpha(){
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f,0.0f);
alphaAnimation.setRepeatMode(Animation.RESTART);
alphaAnimation.setDuration(1500);
alphaAnimation.setRepeatCount(3);
// alphaAnimation.setInterpolator(this,android.R.anim.accelerate_decelerate_interpolator);
// translateAnimation.setInterpolator(new AccelerateInterpolator());
//...
ivImage.startAnimation(alphaAnimation);
}

透明度动画测试效果如下:

到此为止,位移、缩放、旋转、透明度动画的内容介绍完了,除了单独使用这些动画,还可以组合这些动画实现更复杂的动画,

组合动画

组合动画使用 AnimationSet 来实现,可使用 xml 定义组合动画也可以使用代码创建组合动画,透明度动画对应的 Animation 的子类是 AnimationSet。

xml定义组合动画:在 res/anim 下创建一个 xml 文件 combine_anim.xml,在里面定义组合动画,具体如下:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1200"> <!--透明度动画-->
<alpha
android:repeatMode="reverse"
android:repeatCount="10"
android:fromAlpha="1"
android:toAlpha="0.5" /> <!--旋转动画-->
<rotate
android:repeatMode="reverse"
android:repeatCount="10"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="360" /> <!--缩放动画-->
<scale
android:repeatMode="reverse"
android:repeatCount="10"
android:fromXScale="1"
android:fromYScale="1"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="3"
android:toYScale="3" />
</set>

然后在 Activity 中获取该 xml 文件对应的 AnimationSet,将其设置到想要设置旋转动画的 View 上即可,具体如下:

private void combine(){
AnimationSet animationSet = (AnimationSet) AnimationUtils.loadAnimation(this,R.anim.combine_anim);
ivImage.startAnimation(animationSet);
}

代码创建组合动画:代码创建组合动画使用 Animation 的子类 AnimationSet,使用时直接创建 AnimationSet 对象,将要组合的动画按序添加到 AnimationSet 中,具体如下:

//代码创建组合动画
private void combine(){
AnimationSet animationSet = new AnimationSet(true);
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f,0.3f);
alphaAnimation.setRepeatMode(Animation.REVERSE);
alphaAnimation.setRepeatCount(3);
RotateAnimation rotateAnimation = new RotateAnimation(0,360,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0.5f);
rotateAnimation.setRepeatMode(Animation.REVERSE);
rotateAnimation.setRepeatCount(3);
ScaleAnimation scaleAnimation = new ScaleAnimation(1,3,1,3,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0.5f);
scaleAnimation.setRepeatMode(Animation.REVERSE);
scaleAnimation.setRepeatCount(3); animationSet.addAnimation(alphaAnimation);
animationSet.addAnimation(rotateAnimation);
animationSet.addAnimation(scaleAnimation); animationSet.setDuration(1200);
//AnimationSet不支持动画重复播放,如果想要组合动画重复播放可设置每个动画重复播放即可
// animationSet.setRepeatMode(Animation.REVERSE);
// animationSet.setRepeatCount(10); ivImage.startAnimation(animationSet);
}

组合动画测试效果如下:

总结

这篇文章总结了 Android 开发中帧动画(FrameAnimation)和补间动画(TweenAnimation)的使用,下一篇将会介绍属性动画(ObjectAnimator )。

可以关注公众号:零点小筑(jzman-blog),一起交流学习。

Android动画系列之帧动画和补间动画的更多相关文章

  1. Android动画效果之Tween Animation(补间动画)

    前言: 最近公司项目下个版本迭代里面设计了很多动画效果,在以往的项目中开发中也会经常用到动画,所以在公司下个版本迭代开始之前,抽空总结一下Android动画.今天主要总结Tween Animation ...

  2. android 补间动画

    android开发过程中,为了更好的展示应用程序,应用程序添加动画,能够很好地实现这个功能.如果动画中的图像变化有一定的规律,可以采用自动生成图像的方式来生成动画,例如图像的移动.旋转.缩放等.自动生 ...

  3. Android开发(26)--补间动画(Tween)的实现

    补间动画(Tween Animation) 补间动画与逐帧动画在本质上是不同的,逐帧动画通过连续播放图片来模拟动画的效果,而补间动画则是通过在两个关键帧之间补充渐变的动画效果来实现的.补间动画的优点是 ...

  4. android 帧动画,补间动画,属性动画的简单总结

      帧动画——FrameAnimation 将一系列图片有序播放,形成动画的效果.其本质是一个Drawable,是一系列图片的集合,本身可以当做一个图片一样使用 在Drawable文件夹下,创建ani ...

  5. Android 学习笔记多媒体技术之 Drawable类+Tween(补间动画)+Frame(帧动画)

    学习内容: 1.了解Drawable类的作用 2.如何使用Drawable... 3.了解Tween动画... 4.如何创建和使用Tween动画... 1.Drawable类...   Drawabl ...

  6. Android动画主要包含补间动画(Tween)View Animation、帧动画(Frame)Drawable Animation、以及属性动画Property Animation

    程序运行效果图: Android动画主要包含补间动画(Tween)View Animation.帧动画(Frame)Drawable Animation.以及属性动画Property Animatio ...

  7. Android动画总结#补间动画(Tween Animation/View Animation) #帧动画(Frame Animation/Drawable Animation)#属性动画(PropertyAnimation)

    1.共有三种动画,英文名字多种叫法如下 第一种动画:补间动画(Tween Animation/View Animation) 四个:RotateAnimation旋转. AlphaAnimation透 ...

  8. Android基础笔记(十)- 帧动画、补间动画具体解释、对话框

    帧动画 补间动画Tween Animation 对话框以及面试中的注意点 帧动画 帧动画非常easy,我们首先看一下Google官方解释This is a traditional animation ...

  9. Android中的帧动画与补间动画的使用

    前言 在日常开发中,我们有时候须要一些好看的动画效果,这时能够充分利用Android提供的这几种动画来实现. Android提供了3种类型的动画: 补间动画:补间动画能够应用于View,让你能够定义一 ...

随机推荐

  1. 团队作业5:Alpha版本测试和发布(歪瑞古德小队)

    目录 一.项目文档和代码 二.Alpha版本测试报告 2.1 功能测试 2.1.1 功能列表 2.1.2 场景测试 2.1.3 测试结果 2.1.4 bug清单 2.2 兼容性测试 2.3 性能测试 ...

  2. .NET 数据库事务的各种玩法进化

    事务是数据库系统中的重要概念,本文讲解作者从业 CRUD 十余载的事务多种使用方式总结. 以下所有内容都是针对单机事务而言,不涉及分布式事务相关的东西! 关于事务原理的讲解不针对具体的某个数据库实现, ...

  3. java爬取图片示例

    爬虫是什么 这里引用一下 wiki 中关于 网络爬虫的定义,相信大家看过后会有一个清晰的认识 网络爬虫(英语:web crawler),也叫网络蜘蛛(spider),是一种用来自动浏览万维网的网络机器 ...

  4. 简述BFS与DFS

    简述BFS与DFS 最近学习了数据结构课程以及应对蓝桥杯备考,所以花费了一点时间将比较重要的两个搜索BFS(宽度优先搜索)和DFS(深度优先搜索)大致思路以及代码整理出来,如有错误,还请各位大佬批评改 ...

  5. 23种设计模式 - 数据结构(Composite - iterator - Chain of Responsibility)

    其他设计模式 23种设计模式(C++) 每一种都有对应理解的相关代码示例 → Git原码 ⌨ 数据结构 Composite 动机(Motivation) 软件在某些情况下,客户代码过多依赖于对象容器复 ...

  6. Deep Env

    https://mirror.tuna.tsinghua.edu.cn/help/ubuntu/ Ready Ubuntu 18.04 这里直接wsl Ubuntu 的软件源配置文件是 /etc/ap ...

  7. 【转】ANDROID LOLLIPOP SCREEN CAPTURE AND SHARING

    https://datatheorem.github.io/android/2014/12/26/android-screencapture/ https://www.youtube.com/watc ...

  8. SpiderMonkey教程

    https://technotales.wordpress.com/2009/06/07/spidermonkey-introduction/ https://developer.mozilla.or ...

  9. python3笔记-字典

    5 1 # 创建字典 6 2 d=dict(name='lily',age=18,phone='') 7 3 print(d) 4 # {'name': 'lily', 'age': 18, 'pho ...

  10. vmware-workstation迁移虚拟机 15pro到12版本

    最近将测试的几台虚拟机进行了迁移,有几个点要注意,分享一下 1.环境介绍: 源服务器-ip-172.16.96.x 目标服务器-ip-172.16.96.x VMware版本-VMwareworkst ...