总的来说,安卓动画可以分为两类,最初的传统动画和Android3.0之后的属性动画。
传统动画包括:帧动画( Frame Animation)和补间动画(Tweened Animation)。
下面来具体说一下各种动画的使用及特点:
帧动画:是最容易实现的一种动画,它依赖于完善的UI资源;实现原理是将一张张单独的图片连贯起来进行播放,从而在视觉上产生动画的效果,类似于某些制作gif动画的方式。
创建XML文件要放在/res/drawable/目录下,必须以animation-list为根元素,以item表示要轮换显示的图片,duration属性表示各项显示的时间。
xml:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/a_0"
android:duration="100" />
<item
android:drawable="@drawable/a_1"
android:duration="100" />
<item
android:drawable="@drawable/a_2"
android:duration="100" />
...
</animation-list>
java :
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_frame_animation);
ImageView animationImg1 = (ImageView) findViewById(R.id.animation1);
animationImg1.setImageResource(R.drawable.frame_anim1);
AnimationDrawable animationDrawable1 = (AnimationDrawable) animationImg1.getDrawable();
animationDrawable1.start();
}
补间动画:就是对场景里的对象不断的进行图像变化来产生动画效果(旋转、平移、放缩和渐变)。分别是 alpha(淡入淡出),translate(位移),scale(缩放大小),rotate(旋转)。
补间动画的实现方式:一种使用XML文件(文件放在res/anim),一种直接代码搞定动画类型
一般会采用xml 文件的形式;代码会更容易书写和阅读,同时也更容易复用。
首先,在res/anim/ 文件夹下定义如下的动画实现方式
xml实现:
alpha.xml
<?xml version="1.0" encoding="utf-8"?>
<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.xml
<?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"/>
java:
Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.alpha_anim);
img = (ImageView) findViewById(R.id.img);
img.startAnimation(animation);
使用set 标签将多个动画组合(代码源自Android SDK API):
<?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>
可以看到组合动画是可以嵌套使用的。
第二种java实现方式:
相关的类:
AlphaAnimation渐变透明度动画效果
Animation animationAlpha = new AlphaAnimation(0.0f, 1.0f);
animationAlpha.setDuration(3000);
ivAnim.startAnimation(animationAlpha);
ScaleAnimation渐变尺寸伸缩动画效果
Animation animationScale = new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animationScale.setDuration(3000);
ivAnim.startAnimation(animationScale);
TranslateAnimation画面转换位置移动动画效果
Animation animationTranslate = new TranslateAnimation(30.0f, -80.0f, 30.0f, 300.0f);
animationTranslate.setDuration(3000);
ivAnim.startAnimation(animationTranslate);
RotateAnimation画面转移旋转动画效果
Animation animationRotate = new RotateAnimation(0.0f,+350.0f,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animationRotate.setDuration(3000);
ivAnim.startAnimation(animationRotate);
补间动画缺点:视觉上的效果变化了,但是属性上的值没有变,或者真正的位置没有改变,还可以交互到
属性动画和补间动画的区别:
补间动画:是通过父容器来不停的绘制view,view控件本身是没有变化的;
属性动画:通过不停改变自己view的属性值,来改变view。
属性动画: 在Android 3.0中才引进的,它更改的是对象的实际属性;为了就是解决补间动画的缺点。
a.相关的类:ObjectAnimator, ValueAnimator, AnimatorSet, ViewCompat
b.用法:
ObjectAnimator anim = ObjectAnimator.ofInt(view,"transtionX",100);
anim.setDuration(100);
anim.start();
ViewCompat.animate(view).tranxlaionX(100)
.scaleX(1.2f)
.setDuration()
.start();
ValueAnimator的补充:
本质:是帮助我们定义了一个动画的执行流程,但是不会拥有任何的动画逻辑,可以让我们实现任何动画效果,比如你想改变一个View的width和height以及背景颜色等属性。另外还可以实现倒计时效果;
属性动画的低版本兼容:
Android 3.0以后引入了属性动画,属性动画可以轻而易举的办到许多View动画做不到的事。但是Android3.0以下是无法使用属性动画的,如果需要在Android3.0以下版本中使用属性动画,需要使用一个开源的jar包:nineoldandroids.jar(点击下载)