一屡律、視圖動畫
視圖動畫只是在視圖上產(chǎn)生動畫效果挪圾,View的屬性(位置浅萧、大小、角度等)實際并未發(fā)生變化哲思。Android中提供了AlphaAnimation洼畅、RotateAnimation、TranslateAnimation棚赔、ScaleAnimation四種實現(xiàn)視圖動畫效果的API帝簇,同時提供了AnimationSet來實現(xiàn)動畫集合徘郭,以便混合使用多種動畫。以下示例對一個命名為mTestView的View使用AlphaAnimation動畫效果丧肴,其他動畫使用方式與此相同残揉。
- AlphaAnimation示例:
AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
alphaAnimation.setDuration(2000);
mTestView.startAnimation(alphaAnimation);
//添加動畫監(jiān)聽回調(diào)
alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
- 使用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之后添加的動畫框架闪湾,最經(jīng)常使用的是AnimatorSet和ObjectAnimator的配合冲甘。ObjectAnimator可以精細化控制一個對象的一個屬性值,實現(xiàn)動畫效果的同時也改變動畫對象的相應(yīng)屬性途样。AnimatorSet可以組合多個ObjectAnimator,實現(xiàn)多動畫混合濒憋。
- ObjectAnimator的使用
//參數(shù)一:要操縱的view何暇;
//參數(shù)二:要操縱的屬性(translationX、translationY凛驮、rotationX裆站、rotationY、rotation黔夭、scaleX宏胯、scaleY)
//參數(shù)三:可變數(shù)組參數(shù),代表屬性變化的一個取值過程本姥。
ObjectAnimator translationX = ObjectAnimator.ofFloat(mTestView, "translationX", 300);
translationX.setDuration(500);
translationX.start();
- 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();
- AnimatorSet:組合多個動畫肩袍,并且能夠?qū)Χ鄠€動畫進行精確的順序控制。
示例:按順序執(zhí)行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();
- 使用View的animate方法實現(xiàn)屬性動畫
Android 3.0之后,View增加的animate方法來直接實現(xiàn)屬性動畫先舷。
mTestView.animate()
.scaleX(1.5f)
.x(300)
.setDuration(1000)
.withStartAction(new Runnable() {
@Override
public void run() {
}
})
.withEndAction(new Runnable() {
@Override
public void run() {
}
}).start();
- 布局動畫
布局動畫作用在布局上艰管,當給布局添加view時,會添加一個動畫過渡效果蒋川。如:給一個RelativeLayout添加一個AlphaAnimation牲芋,該布局中的view會以AlphaAnimation的動畫效果出現(xiàn)。
AlphaAnimation aa = new AlphaAnimation(0, 1);
aa.setDuration(3000);
LayoutAnimationController lac = new LayoutAnimationController(aa, 1f);
//LayoutAnimationController.ORDER_NORMAL:布局中的view按順序出現(xiàn)
//LayoutAnimationController.ORDER_RANDOM:布局中的view按隨機順序出現(xiàn)
//LayoutAnimationController.ORDER_REVERSE:布局中的view反序出現(xiàn)
lac.setOrder(LayoutAnimationController.ORDER_NORMAL);
mTestLayout.setLayoutAnimation(lac);