在Android的控件层面上,为了增强界面的感染力,已经引入了多种动画效果,如可以为Activity、对话框、输入法、子菜单、墙纸等设置动画效果,相关的配置文件实现位于frameworks/base/core/res/res/anim中。
补间动画即通过对场景里的对象不断做图像变换(透明度、平移、缩放、旋转)产生动画效果。针对不同的图像变换动画,Android提供了AlphaAnimation、ScaleAnimation、RotateAnimation、TranslateAnimation等四个类支持。
下面是一个补间动画的具体例子的实现:
代码:补间动画的实现
package com.miaozl.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
public class TweenAnimActivity extends Activity {
ImageView tweenImage;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.animation);
}
public void onWindowFocusChanged (boolean hasFocus){
if(hasFocus){
tweenImage = (ImageView) findViewById(R.id.anim);
tweenImage.setImageResource(R.drawable.photo1);
Animation tweenAnimation = AnimationUtils.loadAnimation(this, R.anim.tweenanim);
tweenImage.startAnimation(tweenAnimation);
}
}
}
下面是res/anim-hdpi/tweenanim.xml的实现:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="//schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<scale
android:interpolator= "@android:anim/accelerate_decelerate_interpolator"
android:fromXScale="0.5"
android:toXScale="1.5"
android:fromYScale="0.5"
android:toYScale="1.5"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="false"
android:startOffset="700"
android:duration="2000"
android:repeatCount="10"
/>
</set>
下图是补间动画的运行效果。

补间动画的效果图
需要注意的是,补间动画并不只是针对图像的,它实际上还能支持TextView等视图对象。