一、View动画
View动画即补间动画。补间动画通过起始和终止的位置等属性计算动画。
补间动画可以对View对象的内容进行简单的转换,如位置,大小,旋转,透明度。如果View有背景也会随之变化。
可以使用XML或代码中定义View动画。推荐用XML,比硬编码方式更具可读性以及可复用性。
View动画可以指定动画方式,开始时间,持续时间,顺序还是同步的。
XML文件放在res/anim中,该文件必须具有单个根元素:<aplha>、<scale>、<translate>、<rotate>,插值元素,或者<set>,默认情况下,所有动画同时运行,若要顺序发生,则需要指定startOffset。
下面的XML用于先拉伸,再旋转的动画效果。
<set android:shareInterpolator="false">
<scale
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromXScale="1.0"
android:toXScale="1.4"
android:fromYScale="1.0"
android:toYScale="0.6"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="false"
android:duration="700" />
<set android:interpolator="@android:anim/decelerate_interpolator">
<scale
android:fromXScale="1.4"
android:toXScale="0.0"
android:fromYScale="0.6"
android:toYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="700"
android:duration="400"
android:fillBefore="false" />
<rotate
android:fromDegrees="0"
android:toDegrees="-45"
android:toYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="700"
android:duration="400" />
</set>
</set>
本例中屏幕左上角为(0,0),且向下向右增加。某些值,如pivotX,可以是相对于本身或者相对于父对象,50%为相对于本身50%,50为相对于父容器50%。还可以通过interpolator来指定随着时间推移转换状态。
之后可以使用代码获得该动画引用,然后应用到View中。
ImageView spaceshipImage = (ImageView) findViewById(R.id.spaceshipImage);
Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump);
spaceshipImage.startAnimation(hyperspaceJumpAnimation);
还可以先设置Animation.setStartTime()定义开始时间,再startAnimation()启动动画。
注意,动画只是改变View的内容,而不是其真正的位置。动画可以超出View的边界绘制不会被裁剪,所以使用View动画,类似于点击事件的触发将保持在View原位置,而不一定是View的显示位置。同时,动画如果超出View的父容器边界,就会被裁剪。
二、Drawable动画
Drawable动画,是通过一个接一个地加载Drawable资源创建的动画。类似于逐帧动画。AnimationDrawable类是Drawable动画的基础。
Drawable动画可以代码中实现,但还是推荐在XML中定义。该动画XML文件放在res/drawable目录。
<animation-list>为根元素,一系列的<item>为子元素,其中设置图片与持续时间。
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item android:drawable="@drawable/rocket_thrust1" android:duration="200" />
<item android:drawable="@drawable/rocket_thrust2" android:duration="200" />
<item android:drawable="@drawable/rocket_thrust3" android:duration="200" />
</animation-list>
oneshot=true表示仅播放一次,结束后保持在最后一帧,为false则表示循环播放。可以设置其为background,然后再调用启动。
AnimationDrawable rocketAnimation;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
rocketImage.setBackgroundResource(R.drawable.rocket_thrust);
rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
}
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
rocketAnimation.start();
return true;
}
return super.onTouchEvent(event);
}
注意,在onCreate()中,AnimationDrawable无法调用start(),因为AnimationDrawable还未完全attach到window。如果要立即播放而不需要交互,则可以在onWindowFocusChanged()方法中调用start(),即当前window获得焦点时启动动画。