视图动画主要有两种:android
1、Tween Animation
译为“补间动画”
一、scale
译为“规模、比例”,是对View进行特定范围的缩放
二、alpha
经过改变View的透明度实现View隐现的效果
三、translate
译为"转移",是对View进行位置的移动
四、rotate
译为“旋转”,是让View围绕特定的点进行旋转
PS:全部View的移动、隐藏、旋转仅仅是看到的动画效果,实际View的位置/大小/比例并无发生本质上的改变(好比说View的位置经过动画进行移动后你注册的点击事件仍是须要点击到View的原始位置才能够被触发)。函数
2、Frame Animation
译为逐帧动画
这个比较容易理解就是将多个具备特定连贯动做的图片在短期内进行快速的切换达到动画的效果,本质上全部的动画效果都是这种思想。布局
动画文件要存放在res/anim文件夹下,访问时采用R.anim.XXX的方式。默认是没有这个文件夹的须要手动建立(右键res目录-->New-->Android Resource Directory-->肯定。)学习
动画文件的建立方式为:右键anim
文件夹选择new,而后点击Animation Resource file,选择动画类型便可建立。动画
输入后会自动提示动画名称,而后输入名称,肯定便可。this
这个动画参数相对来讲比较多, 就我我的而言在学习这个动画的时候花费时间是最长的。spa
这个动画主要是实现View的缩放,首先要想,要实现一个缩放的动画首先要肯定什么参数/信息(比如说想切割一张特定大小的纸张要肯定宽和高同样),那么第一个就是要肯定要围绕哪一个点(pivot)进行缩放。code
还须要知道在动画开始(from)时View的大小(比例),以及动画结束(to)时View要处于的大小(比例)。就是要肯定如下六个参数才能够完成一次缩放动画。xml
X则指定控件的宽度,Y则指定控件的高度,值越大则控件所占位置越大。对象
Android坐标从左上角开始算起。
其中fromXScale
、toXScale
、fromYScale
、toYScale
使用浮点类型,1.0表使原始大小,0.5则是缩放一半,1.5则是扩大原大小的一半。举例:原View宽高100、150,1.0:(100,150),0.5:(50,75),1.5:(150,225)。也可使用精确值(DP/PX)。
pivotX
、pivotY
有三种表使方法,第一是采用像素值,第二则是较自身的百分比,第三则是较父View的百分比。
为了方便观察,使用两个同等位置和大小不一样颜色的View来进行观察。动画的播放代码在最下文已给出。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="播放动画" android:id="@+id/btnOpenAnimation" /> <TextView android:layout_width="300dp" android:layout_height="300dp" android:layout_centerInParent="true" android:layout_gravity="center" android:background="#03A9F4"/> <TextView android:layout_width="300dp" android:layout_height="300dp" android:layout_centerInParent="true" android:id="@+id/txtAnimation" android:layout_gravity="center" android:background="#FF00"/> </RelativeLayout>
示例1:使用像素值肯定Pivot点
<?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:fromXScale="0.5" android:toXScale="1.0" android:duration="3000" android:fromYScale="0.5" android:toYScale="1.0" android:pivotX="200" android:pivotY="200"> </scale>
这里咱们分别设置了pivotX
和pivotY
为200(px),这个是从View自己来算起的,View的左上角为(0,0)点,而后X轴坐标向右,Y则向下分别走200像素,最终获得了箭头指向的点(Pivot),那么开始点肯定了。
再看其它参数,fromXScale
指定的是在动画开始时X坐标也就是宽度的大小(这里是按照比例计算的),0.5则表明View原始宽度的一半,fromYScale
则是高度了。
既然是向特定的比例进行缩放,仅仅肯定开始的大小是不够的,还要肯定在动画播放到最后所要达到的大小,因此就有了toXScale
和toYScale
,这两个则是指定在动画播放完成后View所处的大小。
这里咱们是从View的0.5缩放到1.0,也就是从原始View的一半通过3秒(duration
用来控制动画时长,1000为1秒。)后变成原始View的大小也就是1.0。
示例2:百分比Pivot
使用百分比肯定Pivot也很简单,那么Pivot的位置就是:以View的左上角即(0,0)点为基础加上View特定的宽高百分比。
<?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:fromXScale="0.0" android:toXScale="1.0" android:duration="5000" android:fromYScale="0.0" android:toYScale="1.0" android:pivotX="70%" android:pivotY="70%"> </scale>
示例3:父View百分比Pivot
<?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:fromXScale="0.0" android:toXScale="1.0" android:duration="5000" android:fromYScale="0.0" android:toYScale="1.0" android:pivotX="50%p" android:pivotY="50%p"> </scale>
这个计算和上边那个实际上是同样的,只是基于的点不一样而已,上边是基于自身来算起,那么这个则是基于View的父布局来计算的。那么Pivot的位置就是:以View的左上角即(0,0)点为基础加上父View特定的宽高百分比。
这个能够说就很是简单了,主要是实现颜色的过分效果,fromAlpha
则是动画开始的透明度,toAlpha
则是在动画最后显示的透明度。0.0表明彻底透明1.0则是View的原色。
<?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:fromAlpha="0.0" android:duration="3000" android:toAlpha="1.0"> </alpha>
首先要想完成旋转要肯定那些参数?确定要肯定旋转要围绕的点也就是pivot
,这个在scale
动画也用到了,用法都同样,不在多说。这里出现了名为degrees
也就是角度的概念,也就是以特定点(pivot
)为中心从多少度(fromDegrees
),旋转到多少度(toDegrees
)。如下示例是从0转到360度,正好一圈。
<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="0" android:toDegrees="360" android:pivotY="50%" android:pivotX="50%" android:duration="3000" > </rotate>
说白了就是移动View的位置,就是从一个点移动到另外一个点,最重要的就是肯定两个点,那么则须要肯定两对X,Y坐标了。
如下参数的使用方式和在最上边提到的pivot
是同样的均可以使用精确值和百分比。
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="10%" android:toXDelta="70%" android:fromYDelta="0%" android:toYDelta="70%" android:duration="3000"> </translate>
其中黑色线条框住的是View的原始位置,黄色框住的是View动画开始时的位置,紫色线条则是整个View的70%的占比,最后集中到的点就是View要移动到的最终的位置也就是结束点。
如何理解Set
(集合动画),其实很简单,好比如今有一个需求,咱们既要旋转又要有渐渐出现的效果,那么就要使用set
了,说白了就是将多个动画组合起来。只要把上边几个都学会了,使用这个set
简直so easy。
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="3000"> <alpha android:fromAlpha="0.0" android:toAlpha="1.0" ></alpha> <rotate android:pivotX="50%" android:pivotY="50%" android:fromDegrees="0" android:toDegrees="180" ></rotate> </set>
上边所展现的都是经过xml文件写的动画,都是静态写好了的。那么想要动态的建立动画对象又该如何?其实很简单,经过代码的方式建立动画它们的名称和使用xml文件建立时名称都是对应的,提供的构造函数也都是必备的参数值。
//建立Alpha动画 var alpha = AlphaAnimation(0.0F, 1.0F) alpha.duration = 3000 this.txtAnimation.startAnimation(alpha) //建立Rotate动画 var rotate = RotateAnimation(0F, 360F, Animation.RELATIVE_TO_SELF, 50F, Animation.RELATIVE_TO_SELF, 50F) //建立Scale动画 var scale = ScaleAnimation(0F, 1F, 0F, 1F, Animation.RELATIVE_TO_SELF, 50F, Animation.RELATIVE_TO_SELF, 50F) //建立translate动画 var translate = TranslateAnimation( Animation.RELATIVE_TO_SELF, 10F, Animation.RELATIVE_TO_SELF, 80F, Animation.RELATIVE_TO_SELF, 0F, Animation.RELATIVE_TO_SELF, 70F ) //建立Set动画 var set = AnimationSet(this, null) set.duration = 3000 set.addAnimation(alpha) set.addAnimation(rotate)
以上全部的动画对象都是从Animation类继承来的,全部有一些公共的属性也会继承过来。
//加载动画 this.btnOpenAnimation.setOnClickListener { var animation = AnimationUtils.loadAnimation(this, R.anim.translate_anim) this.txtAnimation.startAnimation(animation) }
这个是Drawable形式的动画,存放在drawable文件夹中,使用animation-list
节点来表示。图片素材是提早准备好的。本身动手尝试下立刻就会理解了。
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:duration="100" android:drawable="@drawable/a001"></item> <item android:duration="100" android:drawable="@drawable/a002"></item> <item android:duration="100" android:drawable="@drawable/a003"></item> <item android:duration="100" android:drawable="@drawable/a004"></item> <item android:duration="100" android:drawable="@drawable/a005"></item> <item android:duration="100" android:drawable="@drawable/a006"></item> <item android:duration="100" android:drawable="@drawable/a007"></item> <item android:duration="100" android:drawable="@drawable/a008"></item> <item android:duration="100" android:drawable="@drawable/a009"></item> <item android:duration="100" android:drawable="@drawable/a010"></item> <item android:duration="100" android:drawable="@drawable/a011"></item> <item android:duration="100" android:drawable="@drawable/a012"></item> <item android:duration="100" android:drawable="@drawable/a013"></item> <item android:duration="100" android:drawable="@drawable/a014"></item> <item android:duration="100" android:drawable="@drawable/a015"></item> <item android:duration="100" android:drawable="@drawable/a016"></item> <item android:duration="100" android:drawable="@drawable/a017"></item> <item android:duration="100" android:drawable="@drawable/a018"></item> </animation-list>
这里咱们给一个TextView设置了background
属性。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="播放动画" android:id="@+id/btnOpenAnimation" /> <TextView android:layout_width="300dp" android:layout_height="300dp" android:layout_centerInParent="true" android:id="@+id/txtAnimation" android:background="@drawable/anim_refresh" android:layout_gravity="center" /> </RelativeLayout>
具体调用
var animationDrawable = this.txtAnimation.background as AnimationDrawable animationDrawable.start()
显示效果