一、視圖動畫
視圖動畫隻是在視圖上産生動畫效果,View的屬性(位置、大小、角度等)實際并未發生變化。Android中提供了AlphaAnimation、RotateAnimation、TranslateAnimation、ScaleAnimation四種實作視圖動畫效果的API,同時提供了AnimationSet來實作動畫集合,以便混合使用多種動畫。以下示例對一個命名為mTestView的View使用AlphaAnimation動畫效果,其他動畫使用方式與此相同。
1. AlphaAnimation示例:
AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
alphaAnimation.setDuration(2000);
mTestView.startAnimation(alphaAnimation);
//添加動畫監聽回調
alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
2. 使用AnimationSet混合多種動畫效果
AnimationSet animationSet = new AnimationSet(true);
AlphaAnimation alphaAnimation1 = new AlphaAnimation(0, 1);
alphaAnimation1.setDuration(2000);
animationSet.addAnimation(alphaAnimation1);
TranslateAnimation translateAnimation1 = new TranslateAnimation(0, 200, 0, 200);
translateAnimation1.setDuration(2000);
animationSet.addAnimation(translateAnimation1);
mTestView.startAnimation(animationSet);
####二、屬性動畫
屬性動畫是Android 3.0之後添加的動畫架構,最經常使用的是AnimatorSet和ObjectAnimator的配合。ObjectAnimator可以精細化控制一個對象的一個屬性值,實作動畫效果的同時也改變動畫對象的相應屬性。AnimatorSet可以組合多個ObjectAnimator,實作多動畫混合。
1. ObjectAnimator的使用
//參數一:要操縱的view;
//參數二:要操縱的屬性(translationX、translationY、rotationX、rotationY、rotation、scaleX、scaleY)
//參數三:可變數組參數,代表屬性變化的一個取值過程。
ObjectAnimator translationX = ObjectAnimator.ofFloat(mTestView, "translationX", 300);
translationX.setDuration(500);
translationX.start();
2. PropertyValuesHolder:對同一對象的多個屬性同時作用多種動畫
示例:在平移過程中同時進行縮放
PropertyValuesHolder pvh1 = PropertyValuesHolder.ofFloat("translationX", 300f);
PropertyValuesHolder pvh2 = PropertyValuesHolder.ofFloat("scaleX", 1f,1.5f);
PropertyValuesHolder pvh3 = PropertyValuesHolder.ofFloat("scaleY", 1f, 1.5f);
ObjectAnimator.ofPropertyValuesHolder(mTestView, pvh1, pvh2, pvh3).setDuration(2000).start();
3. AnimatorSet:組合多個動畫,并且能夠對多個動畫進行精确的順序控制。
示例:按順序執行3個動畫:首先平移mTestView,然後mBtnAnimatorSet橫向縮放,最後mBtnAnimatorSet縱向縮放
ObjectAnimator translationX1 = ObjectAnimator.ofFloat(mTestView, "translationX", 300);
ObjectAnimator scaleX1 = ObjectAnimator.ofFloat(mBtnAnimatorSet, "scaleX", 1.5f);
ObjectAnimator scaleY1 = ObjectAnimator.ofFloat(mBtnAnimatorSet, "scaleY", 1.5f);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.setDuration(2000);
animatorSet.playSequentially(translationX1, scaleX1, scaleY1);
animatorSet.start();
4. 使用View的animate方法實作屬性動畫
Android 3.0之後,View增加的animate方法來直接實作屬性動畫。
mTestView.animate()
.scaleX(1.5f)
.x(300)
.setDuration(1000)
.withStartAction(new Runnable() {
@Override
public void run() {
}
})
.withEndAction(new Runnable() {
@Override
public void run() {
}
}).start();
5. 布局動畫
布局動畫作用在布局上,當給布局添加view時,會添加一個動畫過渡效果。如:給一個RelativeLayout添加一個AlphaAnimation,該布局中的view會以AlphaAnimation的動畫效果出現。
AlphaAnimation aa = new AlphaAnimation(0, 1);
aa.setDuration(3000);
LayoutAnimationController lac = new LayoutAnimationController(aa, 1f);
//LayoutAnimationController.ORDER_NORMAL:布局中的view按順序出現
//LayoutAnimationController.ORDER_RANDOM:布局中的view按随機順序出現
//LayoutAnimationController.ORDER_REVERSE:布局中的view反序出現
lac.setOrder(LayoutAnimationController.ORDER_NORMAL);
mTestLayout.setLayoutAnimation(lac);