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

  • Tween animation補(bǔ)間動(dòng)畫(huà),主要是通過(guò)對(duì)控件實(shí)現(xiàn)透明度(alpha)、尺寸(scale)、位置(translate)、旋轉(zhuǎn)rotate)進(jìn)行改變,通過(guò)集合(set)的方式,實(shí)現(xiàn)連續(xù)的動(dòng)畫(huà)效果
  • Frame animation

Tween動(dòng)畫(huà)

1、直接子類(lèi)

  1. AlphaAnimation;控制一個(gè)對(duì)象的透明度播放的動(dòng)畫(huà)
  2. RotateAnimation;控制一個(gè)對(duì)象旋轉(zhuǎn)的動(dòng)畫(huà)
  3. ScaleAnimation;控制一個(gè)對(duì)象尺寸的動(dòng)畫(huà)
  4. TranslateAnimation;控制一個(gè)對(duì)象位置的動(dòng)畫(huà)

2、監(jiān)聽(tīng)器

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

        @Override
        public void onAnimationEnd(Animation animation) {

        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
 });

3、屬性及其對(duì)應(yīng)方法(所有子類(lèi)都擁有)

  • android:detachWallpaper: 是否在壁紙上運(yùn)行

  • android:duration: 動(dòng)畫(huà)時(shí)長(zhǎng),單位毫秒。

  • android:fillAfter: 設(shè)置為true,控件動(dòng)畫(huà)結(jié)束時(shí)將保持動(dòng)畫(huà)最后一幀(xml文件中,需要設(shè)置在set便簽才生效)。

  • android:fillBefore: 設(shè)置為true,控件動(dòng)畫(huà)結(jié)束時(shí)將保持動(dòng)畫(huà)開(kāi)始第一幀(感覺(jué)很坑爹,設(shè)置true和false還有刪除這個(gè)屬性,效果都一樣)。

  • android:fillEnabled: 效果和fillBefore一樣(同樣坑爹,經(jīng)測(cè)試這個(gè)屬性可有可無(wú),求打臉。

  • android:interpolator: 插值器。設(shè)置動(dòng)畫(huà)速率的變化(譬如加速、減速、勻速等),后面詳說(shuō)。

  • android:repeatCount: 動(dòng)畫(huà)重復(fù)次數(shù)

  • ndroid:repeatMode: 重復(fù)模式,有reverse(倒序)和restart(重復(fù))兩種,必須配合repeatCount一起使用

  • android:startOffset: 延遲一定毫秒之后才開(kāi)始動(dòng)畫(huà)

  • android:zAdjustment: 表示被設(shè)置動(dòng)畫(huà)的內(nèi)容在動(dòng)畫(huà)運(yùn)行時(shí)在Z軸上的位置,有以下三個(gè)值

    • normal 默認(rèn)值,保持內(nèi)容在Z軸上的位置不變
    • top 保持在Z周最上層
    • bottom 保持在Z軸最下層

4、Interpolator

主要實(shí)現(xiàn)動(dòng)畫(huà)的速率變化,interpolator作為一個(gè)接口,然后抽象類(lèi)BaseInterpolator實(shí)現(xiàn)Interpolator接口,在BaseInterpolator的子類(lèi)就是android一系列Android提供的插值器。

用法:

  1. 在XML的標(biāo)簽下設(shè)置:android:interpolator=”@android:anim/accelerate_decelerate_interpolator”
  2. 在JAVA代碼中使用:animation.setInterpolator(new AccelerateDecelerateInterpolator());

以下提供android 所有的插值器

  • AccelerateDecelerateInterpolator:開(kāi)始和結(jié)束速度慢,中間部分加速
  • AccelerateInterpolator:開(kāi)始緩慢,然后加速
  • AnticipateInterpolator:開(kāi)始后退,然后前進(jìn)
  • AnticipateOvershootInterpolator:開(kāi)始后退,然后前進(jìn),直到超出目標(biāo)值,再后退至目標(biāo)值
  • BounceInterpolator:在結(jié)束時(shí)彈跳
  • CycleInterpolator:在指定的周期內(nèi)重復(fù)動(dòng)畫(huà),速度變化遵循正弦規(guī)律
  • DecelerateInterpolator:開(kāi)始加速,結(jié)束緩慢
  • LinearInterpolator:勻速
  • OvershootInterpolator:前進(jìn),直到超出目標(biāo)值,再后退至目標(biāo)值
  • PathInterpolator: 根據(jù)路勁變化改變速率

5、AnimationSet

在res/anim/.xml文件下實(shí)現(xiàn)多種動(dòng)畫(huà)(透明度、尺寸、位置、旋轉(zhuǎn))

6、相關(guān)代碼實(shí)現(xiàn)

  • java代碼實(shí)現(xiàn)動(dòng)畫(huà)

        private void alpha() {
          AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
          alphaAnimation.setDuration(2000);
          alphaAnimation.setFillAfter(true);
          imv.setAnimation(alphaAnimation);
          //imv.startAnimation(alphaAnimation);
      }
    
      private void rote() {
          RotateAnimation rotateAnimation = new RotateAnimation(0, 720f);
          rotateAnimation.setDuration(2000);
          rotateAnimation.setFillAfter(true);
          imv2.setAnimation(rotateAnimation);
      }
      private void scale() {
          ScaleAnimation scaleAnimation = new ScaleAnimation(0f, 2f, 0f, 3f);
          scaleAnimation.setDuration(3000);
          scaleAnimation.setFillAfter(true);
          imv.setAnimation(scaleAnimation);
      }
    
      private void translate() {
          TranslateAnimation  animation = new TranslateAnimation(0f, 200f, 0f, 300f);
          animation.setDuration(3000);
          animation.setFillAfter(true);
          imv.setAnimation(animation);
    
      }
    
  • xml實(shí)現(xiàn)動(dòng)畫(huà)

      <?xml version="1.0" encoding="utf-8"?>
      <set xmlns:android="http://schemas.android.com/apk/res/android"
          android:fillAfter="true"
          android:shareInterpolator="false">
          <scale
              android:duration="1500"
              android:fromXScale="1.0"
              android:fromYScale="1.0"
              android:interpolator="@android:anim/accelerate_decelerate_interpolator"
              android:pivotX="50%"
              android:pivotY="50%"
              android:toXScale="1.5"
              android:toYScale="0.5" />
      
          <set
              android:duration="2000"
              android:interpolator="@android:anim/accelerate_decelerate_interpolator"
              android:startOffset="1500">
      
              <scale
                  android:duration="1500"
                  android:fromXScale="1.5"
                  android:fromYScale="0.6"
                  android:interpolator="@android:anim/accelerate_decelerate_interpolator"
                  android:pivotX="50%"
                  android:pivotY="50%"
                  android:toXScale="0.0"
                  android:toYScale="0.0" />
      
              <rotate
                  android:fromDegrees="0.0"
                  android:pivotX="50%"
                  android:pivotY="50%"
                  android:toDegrees="-45" />
          </set>
      
      </set>
    
  • java中引用res文件

         private void animationSet(){
            Animation animation = AnimationUtils.loadAnimation(getActivity(), R.anim.animation_set);
            imv.startAnimation(animation);
    
        }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • Animation Animation類(lèi)是所有動(dòng)畫(huà)(scale、alpha、translate、rotate)的基...
    四月一號(hào)閱讀 1,936評(píng)論 0 10
  • 最近工作比較清閑,所以想系統(tǒng)的復(fù)習(xí)和學(xué)習(xí)下自己比較短缺的知識(shí),所以。。。 程序運(yùn)行效果圖: Android動(dòng)畫(huà)主要...
    小沈新手閱讀 518評(píng)論 0 1
  • 1 背景 不能只分析源碼呀,分析的同時(shí)也要整理歸納基礎(chǔ)知識(shí),剛好有人微博私信讓全面說(shuō)說(shuō)Android的動(dòng)畫(huà),所以今...
    未聞椛洺閱讀 2,750評(píng)論 0 10
  • 安卓動(dòng)畫(huà)目前共分為三種動(dòng)畫(huà)逐幀動(dòng)畫(huà)、補(bǔ)間動(dòng)畫(huà)和屬性動(dòng)畫(huà)。 一、逐幀動(dòng)畫(huà)(frame-by-frame animat...
    V1tas閱讀 438評(píng)論 0 1
  • 已經(jīng)是凌晨?jī)牲c(diǎn)半了,我覺(jué)得沒(méi)有比我昨天過(guò)的悲催了。可能我就是會(huì)在深夜里胡思亂想而且易把一些事物都想到壞事處。 即使...
    肉啊肉2閱讀 296評(píng)論 0 0