記錄Android中幾種動(dòng)畫(huà)的實(shí)現(xiàn)方式

一、視圖動(dòng)畫(huà)

視圖動(dòng)畫(huà)只是在視圖上產(chǎn)生動(dòng)畫(huà)效果,View的屬性(位置、大小、角度等)實(shí)際并未發(fā)生變化。Android中提供了AlphaAnimation、RotateAnimation、TranslateAnimation、ScaleAnimation四種實(shí)現(xiàn)視圖動(dòng)畫(huà)效果的API,同時(shí)提供了AnimationSet來(lái)實(shí)現(xiàn)動(dòng)畫(huà)集合,以便混合使用多種動(dòng)畫(huà)。以下示例對(duì)一個(gè)命名為mTestView的View使用AlphaAnimation動(dòng)畫(huà)效果,其他動(dòng)畫(huà)使用方式與此相同。

  1. AlphaAnimation示例:
AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
alphaAnimation.setDuration(2000);
mTestView.startAnimation(alphaAnimation);
//添加動(dòng)畫(huà)監(jiān)聽(tīng)回調(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) {

                }
            });
  1. 使用AnimationSet混合多種動(dòng)畫(huà)效果
 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);

二、屬性動(dòng)畫(huà)

屬性動(dòng)畫(huà)是Android 3.0之后添加的動(dòng)畫(huà)框架,最經(jīng)常使用的是AnimatorSet和ObjectAnimator的配合。ObjectAnimator可以精細(xì)化控制一個(gè)對(duì)象的一個(gè)屬性值,實(shí)現(xiàn)動(dòng)畫(huà)效果的同時(shí)也改變動(dòng)畫(huà)對(duì)象的相應(yīng)屬性。AnimatorSet可以組合多個(gè)ObjectAnimator,實(shí)現(xiàn)多動(dòng)畫(huà)混合。

  1. ObjectAnimator的使用
//參數(shù)一:要操縱的view;
//參數(shù)二:要操縱的屬性(translationX、translationY、rotationX、rotationY、rotation、scaleX、scaleY)
//參數(shù)三:可變數(shù)組參數(shù),代表屬性變化的一個(gè)取值過(guò)程。
 ObjectAnimator translationX = ObjectAnimator.ofFloat(mTestView, "translationX", 300);
 translationX.setDuration(500);
 translationX.start();
  1. PropertyValuesHolder:對(duì)同一對(duì)象的多個(gè)屬性同時(shí)作用多種動(dòng)畫(huà)
    示例:在平移過(guò)程中同時(shí)進(jìn)行縮放
 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();
  1. AnimatorSet:組合多個(gè)動(dòng)畫(huà),并且能夠?qū)Χ鄠€(gè)動(dòng)畫(huà)進(jìn)行精確的順序控制。
    示例:按順序執(zhí)行3個(gè)動(dòng)畫(huà):首先平移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();
  1. 使用View的animate方法實(shí)現(xiàn)屬性動(dòng)畫(huà)
    Android 3.0之后,View增加的animate方法來(lái)直接實(shí)現(xiàn)屬性動(dòng)畫(huà)。
mTestView.animate()
         .scaleX(1.5f)
         .x(300)
         .setDuration(1000)
         .withStartAction(new Runnable() {
              @Override
              public void run() {

              }
          })
         .withEndAction(new Runnable() {
              @Override
              public void run() {

              }
         }).start();
  1. 布局動(dòng)畫(huà)
    布局動(dòng)畫(huà)作用在布局上,當(dāng)給布局添加view時(shí),會(huì)添加一個(gè)動(dòng)畫(huà)過(guò)渡效果。如:給一個(gè)RelativeLayout添加一個(gè)AlphaAnimation,該布局中的view會(huì)以AlphaAnimation的動(dòng)畫(huà)效果出現(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按隨機(jī)順序出現(xiàn)
//LayoutAnimationController.ORDER_REVERSE:布局中的view反序出現(xiàn)
 lac.setOrder(LayoutAnimationController.ORDER_NORMAL);
 mTestLayout.setLayoutAnimation(lac);
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容