Android動畫
1.view動畫
標籤對應TransitionAnimation
標籤對應ScaleAnimation
標籤對應RotateAnimation
標籤對應AlphaAnimation
標籤對應SetAnimation
android:interpolator//插值器
android:shareInterpolator//集合中動畫是否和集合共享一個插值器
Animation animation=AnimationUtils.loadAnimation(this,R.anim.anim_test);
mButton.startAnimation(animation)
1.自定義view動畫
繼承抽象類Animation。重寫initialize和applyTransformation方法,initialize方法中做初始化工作,在applyTransformation中進行相應的矩陣變換。有時通過Camera來簡化居中變換的過程。
2.幀動畫
一組定義好的圖片,順序播放。AnimationDrawable來使用幀動畫。
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false" >
<item
android:drawable="@drawable/dialog01"
android:duration="200"/>
<item
android:drawable="@drawable/dialog02"
android:duration="200"/>
<item
android:drawable="@drawable/dialog03"
android:duration="200"/>
</animation-list>
android:oneshot
是否只執行一次。
AnimationDrawable drawable=(AnimationDrawable)mButton.getBackground();
drawable.start();
3.LayoutAnimation
作用於ViewGroup,當它的子元素出場都會具有這種動畫效果。常常用作ListView。
下邊是動畫anim_layout:
<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:animation="@anim/anim_item"
android:animationOrder="normal"
android:delay="0.5" >
</layoutAnimation>
android:delay
開始動畫的延遲時間。
android:animationOrder
動畫順序,normal,reverse,random,分別表示順序顯示,逆向顯示,隨機播放。
android:animation
指定具體的入場動畫。
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layoutAnimation="@anim/anim_layout" >
</ListView>
代碼中實現:
ListView listview=(ListView)findViewById(R.id.listview);
Animation animation=AnimationUtils.loadAnimation(this, R.anim.anim_item);
LayoutAnimationController controller = new LayoutAnimationController(animation);
controller.setDelay(0.5f);
controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
listview.setLayoutAnimation(controller);
4.Activity的切換效果
overridePendingTransition(int enterAnim,int exitAnimation)這個方法,必須在startAcitivity()或者finish()之後調用纔有效。
Fragment也可以添加切換動畫。通過FragmentTransition中的setCustionAnimations()方法來添加切換動畫。
2.屬性動畫
API 11新加入特性。有ValueAnimator、ObjectAnimator、AnimatorSet等概念。兼容11以下版本採用nineoldandroids,網址http:nineoldandroids.com.
1.使用
(1)改變一個對象的translationY屬性。
ObjectAnimator.ofFloat(mObject,"translationY",-mObject.getHeight()).start();
(2)改變對象的背景色屬性。3秒從0xFFFF8080到0xFF8080FF的漸變。無限循環加反轉效果。
ValueAnimator colorAnim=ObjectAnimator.ofInt(this,"backgroundColor",0xFFFF8080,0xFF8080FF);
colorAnim.setDuration(3000);
colorAnim.setEvaluator(new ArgbEvauator());
colorAnim.setRepeatCount(ValueAnimator.INFINITE);
colorAnim.setRepeatMode(ValueAnimator.REVERSE);
colorAnim.start();
(3)動畫集合,5秒對view的旋轉,評議,縮放,透明都進行改變。
AnimatorSet set = new AnimatorSet();
set.playTogether(ObjectAnimator.ofFloat(myView, "ratationX", 0, 360),
ObjectAnimator.ofFloat(myView, "ratationY", 0, 180),
ObjectAnimator.ofFloat(myView, "ratation", 0, -90),
ObjectAnimator.ofFloat(myView, "translationX", 0, 90),
ObjectAnimator.ofFloat(myView, "translationY", 0, 90),
ObjectAnimator.ofFloat(myView, "scaleX", 0, 1.5f),
ObjectAnimator.ofFloat(myView, "scaleY", 0, 0.5f),
ObjectAnimator.ofFloat(myView, "alpha", 0, 0.25f, 1));
set.setDuration(5*1000).start();
還可以通過XML來定義,屬性動畫定義在res/animator/目錄下。
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="together" >
<objectAnimator
android:duration="300"
android:propertyName="x"
android:valueTo="200"
android:valueType="intType" />
<animator
android:duration="300"
android:valueTo="200"
android:valueType="intType" />
</set>
android:propertyName
屬性動畫的作用對象的屬性的名稱。
startOffset
動畫的延遲時間。
valueType
android:propertyName所指定的屬性的類型,有intType和floatType兩個選線。如果android:propertyName指定的是顏色,不需要指定valueType,系統自動處理。
AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.anim.set_anim);
set.setTarget(mButton);
set.start();
2.插值器和估值器
TimeInterpolator
時間插值器,根據時間流逝的百分比計算出當前屬性值改變的百分比。系統預置的有LinearInterpolator(線性插值器:勻速動畫)、AccelerateDecelerateInterpolator(加速減速插值器:動畫兩頭慢中間快)和DecelerateInterpolator(加速插值器:動畫越來越慢)等。
TypeEvaluator
類型估值算法,根據當前屬性改變的百分比計算改變後的屬性值,系統預置有IntEvaluator(針對整形屬性)、FloatEvaluator(針對浮點型屬性)和ArgbEvaluator(針對color屬性)。
3.監聽器
AnimatorListener定義如下:
public static interface AnimatorListener{
void onAnimationStart(Animator animation);
void onAnimationEnd(Animator animation);
void onAnimationCancel(Animator animation);
void onAnimationRepeat(Animator animation);
}
AnimatorUpdateListener定義:
public static interface AnimatorUpdateListener{
void onAnimationUpdate(ValueAnimator animation);
}