Android中的基礎(chǔ)動(dòng)畫 視圖動(dòng)畫(View Animation)(View動(dòng)畫、Tween動(dòng)畫)

導(dǎo)讀

Android中的視圖動(dòng)畫(View Animation)(View動(dòng)畫、補(bǔ)間動(dòng)畫)

Android 動(dòng)畫的分類及介紹說(shuō)到在Android1.0版本的時(shí)候就有了,Tween動(dòng)畫一般直接作用頁(yè)面中的 View 上,實(shí)現(xiàn)基本的動(dòng)畫效果:平移、旋轉(zhuǎn)、縮放、透明度、或前幾者的組合,基本效果如下:

View動(dòng)畫中的旋轉(zhuǎn)、平移、縮放、透明動(dòng)畫效果

由于Tween動(dòng)畫的特性被屬性動(dòng)畫完美替代,故此,這里就不過(guò)多的進(jìn)行展開(kāi),并且Tween動(dòng)畫作用于視圖整體,只需設(shè)定初始狀態(tài)(關(guān)鍵幀)和結(jié)束狀態(tài)(關(guān)鍵幀),中間的狀態(tài)(變化過(guò)程)則由系統(tǒng)計(jì)算計(jì)算并補(bǔ)齊**,由此可見(jiàn)Tween動(dòng)畫的使用核心就是設(shè)置開(kāi)始狀態(tài)和結(jié)束狀態(tài)。

對(duì)應(yīng)的代碼(效果對(duì)應(yīng)上述GIF):

RotateAnimation(旋轉(zhuǎn)動(dòng)畫)

    private void showRotateAnimation () {
        tv2Title.setText("RotateAnimation 旋轉(zhuǎn)動(dòng)畫");
        RotateAnimation rotate = new RotateAnimation(0f, 360f, 
                      Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        LinearInterpolator lin = new LinearInterpolator();
        rotate.setInterpolator(lin);
        rotate.setDuration(3000);
        rotate.setRepeatCount(-1);
        tv2Show.setAnimation(rotate);
    }

TranslateAnimation(平移動(dòng)畫)

   private void showTranslateAnimation () {
        tv3Title.setText("TranslateAnimation 平移動(dòng)畫");
        Animation translateAnimation = new TranslateAnimation(0, 500, 0, 0);
        translateAnimation.setDuration(3000);
        translateAnimation.setRepeatCount(-1);
        tv3Show.startAnimation(translateAnimation);
    }

ScaleAnimation(縮放動(dòng)畫)

    private void showScaleAnimation() {
        tv4Title.setText("ScaleAnimation 縮放動(dòng)畫");
        Animation scaleAnimation = new ScaleAnimation(0, 2, 0, 2,
                   Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        scaleAnimation.setDuration(3000);
        scaleAnimation.setRepeatCount(-1);
        tv4Show.startAnimation(scaleAnimation);
    }

AlphaAnimation(透明度動(dòng)畫)

   private void showAlphaAnimation () {
        tv5Title.setText("AlphaAnimation 透明度動(dòng)畫");
        Animation alphaAnimation = new AlphaAnimation(1, 0);
        alphaAnimation.setDuration(3000);
        alphaAnimation.setRepeatCount(-1);
        tv5Show.startAnimation(alphaAnimation);
    }

AnimationSet (動(dòng)畫集合)

  private void showAnimationSet() {
        tv7Title.setText("組合 動(dòng)畫");
        AnimationSet setAnimation = new AnimationSet(true);
        setAnimation.setRepeatMode(Animation.RESTART);
        setAnimation.setRepeatCount(1);
        Animation rotate = new RotateAnimation(0, 360,
                           Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotate.setDuration(1000);
        rotate.setRepeatMode(Animation.RESTART);
        rotate.setRepeatCount(Animation.INFINITE);
        Animation translate = new TranslateAnimation(TranslateAnimation.RELATIVE_TO_PARENT, -0.5f,
                TranslateAnimation.RELATIVE_TO_PARENT, 0.5f,
                TranslateAnimation.RELATIVE_TO_SELF, 0
                , TranslateAnimation.RELATIVE_TO_SELF, 0);
        translate.setDuration(10000);
        Animation alpha = new AlphaAnimation(1, 0);
        alpha.setDuration(3000);
        alpha.setStartOffset(7000);
        Animation scale1 = new ScaleAnimation(1, 0.5f, 1, 0.5f, 
                                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        scale1.setDuration(1000);
        scale1.setStartOffset(4000);
        setAnimation.addAnimation(alpha);
        setAnimation.addAnimation(rotate);
        setAnimation.addAnimation(translate);
        setAnimation.addAnimation(scale1);
        tv7Show.startAnimation(setAnimation);

    }

這里就不貼上展示效果了。

Android中的視圖動(dòng)畫總結(jié)

先看看,旋轉(zhuǎn)動(dòng)畫和透明度動(dòng)畫構(gòu)造函數(shù):

    public RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) {... }
    public AlphaAnimation(float fromAlpha, float toAlpha) {... }
    

旋轉(zhuǎn)動(dòng)畫的構(gòu)造函數(shù)中需要fromDegrees、toDegrees參數(shù),即旋轉(zhuǎn)開(kāi)始的角度和旋轉(zhuǎn)結(jié)束的角度
透明度動(dòng)畫的構(gòu)造函數(shù)中需要fromAlpha、toAlpha參數(shù),即開(kāi)始動(dòng)畫時(shí)的透明度和結(jié)束時(shí)的透明度
再看平移動(dòng)畫、縮放動(dòng)畫的構(gòu)造函數(shù):

   public TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) {...}
   public ScaleAnimation(float fromX, float toX, float fromY, float toY,
            int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) {...}
    

平移動(dòng)畫的構(gòu)造函數(shù)中需要fromXDelta、toXDelta、fromYDelta、toYDelta參數(shù),即移動(dòng)開(kāi)始時(shí)的坐標(biāo),和結(jié)束時(shí)的坐標(biāo)
縮放度動(dòng)畫的構(gòu)造函數(shù)中需要pivotXType、pivotXValue、pivotYType、pivotYValue參數(shù),即縮放開(kāi)始時(shí)的坐標(biāo),和結(jié)束時(shí)的坐標(biāo)

果然應(yīng)對(duì)了前面說(shuō)的View動(dòng)畫作用于視圖整體,只需設(shè)定初始狀態(tài)(關(guān)鍵幀)和結(jié)束狀態(tài)(關(guān)鍵幀),中間的狀態(tài)(變化過(guò)程)則由系統(tǒng)計(jì)算計(jì)算并補(bǔ)齊**,由此可見(jiàn)View動(dòng)畫的使用核心就是設(shè)置開(kāi)始狀態(tài)和結(jié)束狀態(tài)。無(wú)論是設(shè)置坐標(biāo)系,還是設(shè)置透明度,都是設(shè)置了開(kāi)始和結(jié)束的狀態(tài),中間變化的過(guò)程由系統(tǒng)補(bǔ)齊。

當(dāng)然,Tween動(dòng)畫還有很多方法,比如上面通用的:

        xxxAnimation.setDuration(3000);////設(shè)置動(dòng)畫持續(xù)時(shí)間
        xxxAnimation.setRepeatCount(-1);//設(shè)置重復(fù)次數(shù)
        xxxView.startAnimation(xxxAnimation);//開(kāi)啟動(dòng)畫

以及動(dòng)畫監(jiān)聽(tīng)

alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {}

            @Override
            public void onAnimationEnd(Animation animation) {}

            @Override
            public void onAnimationRepeat(Animation animation) { }
        });

最后,Tween動(dòng)畫還有許許多多的方法,由于Tween動(dòng)畫可被屬性動(dòng)畫完美替代,就不再繼續(xù)深入。Tween動(dòng)畫的xml方式后續(xù)會(huì)在補(bǔ)上

?著作權(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)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 【Android 動(dòng)畫】 動(dòng)畫分類補(bǔ)間動(dòng)畫(Tween動(dòng)畫)幀動(dòng)畫(Frame 動(dòng)畫)屬性動(dòng)畫(Property ...
    Rtia閱讀 6,437評(píng)論 1 38
  • 概述 在Android開(kāi)發(fā)的過(guò)程中,View的變化是很常見(jiàn)的,如果View變化的過(guò)程沒(méi)有動(dòng)畫來(lái)過(guò)渡而是瞬間完成,會(huì)...
    小蕓論閱讀 39,162評(píng)論 18 134
  • 本文目錄1. 幀動(dòng)畫(Frame動(dòng)畫)2. 補(bǔ)間動(dòng)畫(視圖動(dòng)畫、Tween動(dòng)畫)3. 屬性動(dòng)畫(Property動(dòng)...
    天涯的盡頭s風(fēng)沙閱讀 1,095評(píng)論 0 3
  • 1 背景 不能只分析源碼呀,分析的同時(shí)也要整理歸納基礎(chǔ)知識(shí),剛好有人微博私信讓全面說(shuō)說(shuō)Android的動(dòng)畫,所以今...
    未聞椛洺閱讀 2,861評(píng)論 0 10
  • 我現(xiàn)在狀況,今年已經(jīng)31歲的我,沒(méi)有車,沒(méi)有房,沒(méi)有人追,還有,已經(jīng)失業(yè)1個(gè)多月。 這段時(shí)間,每天就家里和姐姐家兩...
    大齡青年又怎樣閱讀 416評(píng)論 0 1

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