在Android的控件層面上,為了增強界面的感染力,已經(jīng)引入了多種動畫效果,如可以為Activity、對話框、輸入法、子菜單、墻紙等設(shè)置動畫效果,相關(guān)的配置文件實現(xiàn)位于frameworks/base/core/res/res/anim中。
補間動畫即通過對場景里的對象不斷做圖像變換(透明度、平移、縮放、旋轉(zhuǎn))產(chǎn)生動畫效果。針對不同的圖像變換動畫,Android提供了AlphaAnimation、ScaleAnimation、RotateAnimation、TranslateAnimation等四個類支持。
下面是一個補間動畫的具體例子的實現(xiàn):
代碼:補間動畫的實現(xiàn)
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的實現(xiàn):
<?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等視圖對象。