Android的控件動畫效果均是基于補間動畫實現(xiàn)的,常用的控件動畫是基于ViewAnimator類進行的,其子類包含ViewFlipper、ViewSwitcher、ImageSwitcher、TextSwitcher等。下面是日歷應(yīng)用中關(guān)于ViewSwitcher的一個實現(xiàn):
代碼:ViewSwitcher的應(yīng)用
public View switchViews(boolean forward, float xOffSet, float width) {
float progress = Math.abs(xOffSet) / width;
if (progress > 1.0f) {
progress = 1.0f;
}
float inFromXValue, inToXValue;
float outFromXValue, outToXValue;
if (forward) {
inFromXValue = 1.0f - progress;
inToXValue = 0.0f;
outFromXValue = -progress;
outToXValue = -1.0f;
} else {
inFromXValue = progress - 1.0f;
inToXValue = 0.0f;
outFromXValue = progress;
outToXValue = 1.0f;
}
TranslateAnimation inAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, inFromXValue,
Animation.RELATIVE_TO_SELF, inToXValue,
Animation.ABSOLUTE, 0.0f,
Animation.ABSOLUTE, 0.0f);
TranslateAnimation outAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, outFromXValue,
Animation.RELATIVE_TO_SELF, outToXValue,
Animation.ABSOLUTE, 0.0f,
Animation.ABSOLUTE, 0.0f);
long duration = (long) (ANIMATION_DURATION * (1.0f - progress));
inAnimation.setDuration(duration);
outAnimation.setDuration(duration);
mViewSwitcher.setInAnimation(inAnimation);
mViewSwitcher.setOutAnimation(outAnimation);
CalendarView view = (CalendarView) mViewSwitcher.getCurrentView();
view.cleanup();
mViewSwitcher.showNext();
view = (CalendarView) mViewSwitcher.getCurrentView();
view.requestFocus();
view.reloadEvents();
return view;
}
對于控件動畫,對于一些常見的動畫效果,并不需要開發(fā)者自行實現(xiàn),下面是一個利用ViewFlipper和系統(tǒng)動畫實現(xiàn)的一個實例:
代碼:ViewFlipper的應(yīng)用
package com.miaozl.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.animation.AnimationUtils;
import android.widget.ViewFlipper;
public class ViewAnimActivity extends Activity {
private ViewFlipper mFlipper;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mFlipper = ((ViewFlipper) this.findViewById(R.id.flipper));
mFlipper.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));
mFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));
mFlipper.startFlipping(); //循環(huán)播放
}
}
下面是相應(yīng)的布局文件:
< ?xml version="1.0" encoding="utf-8"?>
< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
< ViewFlipper android:id="@+id/flipper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:flipInterval="2000"
android:autoStart="true"> //自動播放
< ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:src="@drawable/photo2"
android:id="@+id/one"/>
< ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:src="@drawable/photo3"
android:id="@+id/two"/>
< /ViewFlipper>
< /LinearLayout>
除了淡入淡出效果外,Android還支持下推(push down)、上推(push up)、shrink fade、grow fade等多種動畫效果。
動畫的進度是通過插補器(interpolator)來控制的,目前,Android支持七種插補器效果:加速減速插補器(accelerate decelerate interpolator)、加速插補器(accelerate interpolator)、預(yù)期插補器(anticipate interpolator)、預(yù)期超調(diào)插補器(anticipate_overshoot_interpolator)、彈跳插補器(bounce_interpolator)、圓插補器(cycle_interpolator)、減速插補器(decelerate interpolator)、線性插補器(linear_interpolator)、超調(diào)插補器(overshoot_interpolator)。如果沒有適合讀者的,讀者也可以通過實現(xiàn)TimeInterpolator接口自定義一個自己的插補器。