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)畫的分類及介紹說到在Android1.0版本的時(shí)候就有了,Tween動(dòng)畫一般直接作用頁面中的 View 上山宾,實(shí)現(xiàn)基本的動(dòng)畫效果:平移至扰、旋轉(zhuǎn)、縮放塌碌、透明度渊胸、或前幾者的組合,基本效果如下:

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

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

對應(yīng)的代碼(效果對應(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)開始的角度和旋轉(zhuǎn)結(jié)束的角度
透明度動(dòng)畫的構(gòu)造函數(shù)中需要fromAlpha、toAlpha參數(shù)馋记,即開始動(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)開始時(shí)的坐標(biāo)茸习,和結(jié)束時(shí)的坐標(biāo)
縮放度動(dòng)畫的構(gòu)造函數(shù)中需要pivotXType畜隶、pivotXValue、pivotYType号胚、pivotYValue參數(shù)籽慢,即縮放開始時(shí)的坐標(biāo),和結(jié)束時(shí)的坐標(biāo)

果然應(yīng)對了前面說的View動(dòng)畫作用于視圖整體涕刚,只需設(shè)定初始狀態(tài)(關(guān)鍵幀)和結(jié)束狀態(tài)(關(guān)鍵幀)嗡综,中間的狀態(tài)(變化過程)則由系統(tǒng)計(jì)算計(jì)算并補(bǔ)齊**,由此可見View動(dòng)畫的使用核心就是設(shè)置開始狀態(tài)和結(jié)束狀態(tài)杜漠。無論是設(shè)置坐標(biāo)系极景,還是設(shè)置透明度察净,都是設(shè)置了開始和結(jié)束的狀態(tài),中間變化的過程由系統(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);//開啟動(dòng)畫

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

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ù)會在補(bǔ)上

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末筑悴,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子稍途,更是在濱河造成了極大的恐慌阁吝,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件械拍,死亡現(xiàn)場離奇詭異突勇,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)坷虑,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進(jìn)店門甲馋,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人迄损,你說我怎么就攤上這事定躏。” “怎么了海蔽?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵共屈,是天一觀的道長绑谣。 經(jīng)常有香客問我党窜,道長,這世上最難降的妖魔是什么借宵? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任幌衣,我火速辦了婚禮,結(jié)果婚禮上壤玫,老公的妹妹穿的比我還像新娘豁护。我一直安慰自己,他們只是感情好欲间,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布楚里。 她就那樣靜靜地躺著,像睡著了一般猎贴。 火紅的嫁衣襯著肌膚如雪班缎。 梳的紋絲不亂的頭發(fā)上蝴光,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天,我揣著相機(jī)與錄音达址,去河邊找鬼蔑祟。 笑死,一個(gè)胖子當(dāng)著我的面吹牛沉唠,可吹牛的內(nèi)容都是我干的疆虚。 我是一名探鬼主播,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼满葛,長吁一口氣:“原來是場噩夢啊……” “哼径簿!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起嘀韧,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤牍帚,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后乳蛾,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體暗赶,經(jīng)...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年肃叶,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了蹂随。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,040評論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡因惭,死狀恐怖岳锁,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情蹦魔,我是刑警寧澤激率,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站勿决,受9級特大地震影響乒躺,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜低缩,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一嘉冒、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧咆繁,春花似錦讳推、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至坏为,卻和暖如春究驴,著一層夾襖步出監(jiān)牢的瞬間慨仿,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工纳胧, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留镰吆,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓跑慕,卻偏偏與公主長得像万皿,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子核行,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,979評論 2 355

推薦閱讀更多精彩內(nèi)容