在日常的Android开发中,我们可能经常会使用到动画,但是Android有哪些动画,它们有什么区别,我们也需要去了解。下面就对Android中常用的动画做一下总结。
总的说来,Android中动画可分为两大类:传统动画(Animation)、属性动画(Animator)。
这一篇文章主要总结传统动画,如果属性动画,可以点击下面链接。
Android 动画(二)Animator。
或者想了解下插值器和估值器,可以点击下面链接。
Android 动画(三)插值器和估值器。
传统动画(Animation)分类
帧动画(Frame Animation)
帧动画是比较容易实现的一种动画。比较依赖UI资源,它的原理就是将一张张图片连贯起来进行播放,从而在视觉上产生动画的感觉。
在drawable文件夹下面创建 frame_anim1.xml文件夹:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <?xml version="1.0" encoding="utf-8"?> <animation-list android:oneshot="true" xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@color/design_default_color_primary" android:duration="200"/>
<item android:drawable="@color/design_default_color_primary_dark" android:duration="200"/>
<item android:drawable="@color/colorAccent" android:duration="200"/>
<!--当然item内部也可以有其他节点--> <!--<item android:duration="200">--> <!--<shape>--> <!--<corners android:radius="2dp"/>--> <!--<solid android:color="@color/colorAccent"/>--> <!--</shape>--> <!--</item>--> </animation-list>
|
oneshot参数说明:true表示动画只会执行一次,否则循环执行;
在代码中添加到view:
1 2 3
| imageView.setImageResource(R.drawable.frame_anim1); AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getDrawable(); animationDrawable.start();
|
补间动画
补间动画又可以分为四种形式,分别是alpha(淡入淡出),translate(位移),scale(缩放大小),rotate(旋转)。
xml的形式
补间动画的实现,一般会采用xml 文件的形式;会更容易书写和阅读,同时也更容易复用。
首先,在res/anim/文件夹下定义如下的动画实现方式:
alpha_anim.xml 动画实现
1 2 3 4 5
| <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000" android:fromAlpha="1.0" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:toAlpha="0.0" />
|
scale_anim.xml 动画实现
1 2 3 4 5 6 7 8 9
| <?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000" android:fromXScale="0.0" android:fromYScale="0.0" android:pivotX="50%" android:pivotY="50%" android:toXScale="1.0" android:toYScale="1.0"/>
|
代码调用:
1 2 3
| Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.alpha_anim); img = (ImageView) findViewById(R.id.img); img.startAnimation(animation);
|
当然,也可以使用set将多个动画组合起来使用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@[package:]anim/interpolator_resource" android:shareInterpolator=["true" | "false"] > <alpha android:fromAlpha="float" android:toAlpha="float" /> <scale android:fromXScale="float" android:toXScale="float" android:fromYScale="float" android:toYScale="float" android:pivotX="float" android:pivotY="float" /> <translate android:fromXDelta="float" android:toXDelta="float" android:fromYDelta="float" android:toYDelta="float" /> <rotate android:fromDegrees="float" android:toDegrees="float" android:pivotX="float" android:pivotY="float" /> <set> ... </set> </set>
|
interpolator:插值器,决定了View的动画的变化方式,也就是动画的变化速率。
pivot 决定了当前动画执行的参考位置
PivotX取值 |
含义 |
10 |
原点坐标加上 10px,作为起始点 |
10% |
原点坐标加上自己宽度的 10%(即控件水平中心)作为起始点 |
10%p |
原点坐标加上父控件宽度的 10% 作为起始点 x 轴坐标 |
代码形式
有时候,动画的属性值可能需要动态的调整,这个时候使用xml 就不合适了,需要使用java代码实现
1 2 3 4 5 6 7 8 9 10 11
| animation = new RotateAnimation(-deValue, deValue , Animation.RELATIVE_TO_SELF , pxValue , Animation.RELATIVE_TO_SELF , pyValue); animation.setDuration(timeValue);
animation.setFillAfter(true); // 动画是否需要保持最后的状态,停留在最后一帧。 animation.setRepeatCount(-1); // 动画的重复次数 animation.setRepeatMode(Animation.REVERSE); // 重复方式 img.startAnimation(animation);
|
也可以通过AnimationSet完成多个动画的组合使用。