Android動(dòng)畫總結(jié)

本文總結(jié)常用屬性方法等,詳細(xì)學(xué)習(xí)可使用如下郭霖大神文章:

Android屬性動(dòng)畫完全解析(上),初識(shí)屬性動(dòng)畫的基本用法

Android屬性動(dòng)畫完全解析(中),ValueAnimator和ObjectAnimator的高級(jí)用法

Android屬性動(dòng)畫完全解析(下),Interpolator和ViewPropertyAnimator的用法

視圖動(dòng)畫(View animation)

視圖動(dòng)畫分兩種子動(dòng)畫Tween animation和Frame animation,下面分別介紹他們的用法。

視圖動(dòng)畫(View Animation)之幀動(dòng)畫(Frame Animation)

  • 創(chuàng)建res/anim/sd_anim.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <item
        android:drawable="@mipmap/img_1"
        android:duration="80" />
    <item
        android:drawable="@mipmap/img_2"
        android:duration="80" />
    <item
        android:drawable="@mipmap/img_3"
        android:duration="80" />
    ...
</animation-list>
  • xml布局中
 <ImageView
        android:id="@+id/img_show"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:background="@anim/miao_gif" />
  • 使用
ImageView img_show = (ImageView) findViewById(R.id.img_show);
AnimationDrawable  anim = (AnimationDrawable) img_show.getBackground();
anim.start();//開始幀動(dòng)畫
anim.stop();//停止幀動(dòng)畫

視圖動(dòng)畫(View Animation)之補(bǔ)間動(dòng)畫(Tween Animation)

AlphaAnimation(透明度漸變)

anim_alpha.xml:

    <alpha xmlns:android="http://schemas.android.com/apk/res/android"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:fromAlpha="1.0"
        android:toAlpha="0.1"
        android:duration="2000"/>

屬性解釋:

  • fromAlpha :起始透明度
  • toAlpha:結(jié)束透明度
  • 透明度的范圍為:0-1,完全透明-完全不透明

ScaleAnimation(縮放漸變)

anim_scale.xml:

<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:fromXScale="0.2"
    android:toXScale="1.5"
    android:fromYScale="0.2"
    android:toYScale="1.5"
    android:pivotX="50%"
    android:pivotY="50%"
    android:duration="2000"/>

屬性解釋:

  • fromXScale/fromYScale:沿著X軸/Y軸縮放的起始比例
  • toXScale/toYScale:沿著X軸/Y軸縮放的結(jié)束比例
  • pivotX/pivotY:縮放的中軸點(diǎn)X/Y坐標(biāo),即距離自身左邊緣的位置,比如50%就是以圖像的 中心為中軸點(diǎn)

TranslateAnimation(位移漸變)

anim_translate.xml:

<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:fromXDelta="0"
    android:toXDelta="320"
    android:fromYDelta="0"
    android:toYDelta="0"
    android:duration="2000"/>

屬性解釋:

  • fromXDelta/fromYDelta:動(dòng)畫起始位置的X/Y坐標(biāo)
  • toXDelta/toYDelta:動(dòng)畫結(jié)束位置的X/Y坐標(biāo)

RotateAnimation(旋轉(zhuǎn)漸變)

anim_rotate.xml:

<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:fromDegrees="0"
    android:toDegrees="360"
    android:duration="1000"
    android:repeatCount="1"
    android:repeatMode="reverse"/>

屬性解釋:

  • fromDegrees/toDegrees:旋轉(zhuǎn)的起始/結(jié)束角度
  • repeatCount:旋轉(zhuǎn)的次數(shù),默認(rèn)值為0,代表一次,假如是其他值,比如3,則旋轉(zhuǎn)4次 另外,值為-1或者infinite時(shí),表示動(dòng)畫永不停止
  • repeatMode:設(shè)置重復(fù)模式,默認(rèn)restart,但只有當(dāng)repeatCount大于0或者infinite或-1時(shí) 才有效。還可以設(shè)置成reverse,表示偶數(shù)次顯示動(dòng)畫時(shí)會(huì)做方向相反的運(yùn)動(dòng)!

AnimationSet(組合漸變)

anim_set.xml:

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator"
    android:shareInterpolator="true" >

    <scale
        android:duration="2000"
        android:fromXScale="0.2"
        android:fromYScale="0.2"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.5"
        android:toYScale="1.5" />

    <rotate
        android:duration="1000"
        android:fromDegrees="0"
        android:repeatCount="1"
        android:repeatMode="reverse"
        android:toDegrees="360" />

    <translate
        android:duration="2000"
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="320"
        android:toYDelta="0" />

    <alpha
        android:duration="2000"
        android:fromAlpha="1.0"
        android:toAlpha="0.1" />

</set>

Android開發(fā)中必定會(huì)涉及到動(dòng)畫方面的效果,那么就會(huì)遇到一個(gè)問題,如果控制動(dòng)畫開始速度,與結(jié)束速度。還有其他的一些效果

控制動(dòng)畫速度(動(dòng)畫差值器)

在xml里面設(shè)置屬性

android:interpolator="@android:anim/accelerate_interpolator" 設(shè)置動(dòng)畫為加速動(dòng)畫(動(dòng)畫播放中越來越快)

android:interpolator="@android:anim/decelerate_interpolator" 設(shè)置動(dòng)畫為減速動(dòng)畫(動(dòng)畫播放中越來越慢)

android:interpolator="@android:anim/accelerate_decelerate_interpolator" 設(shè)置動(dòng)畫為先加速在減速(開始速度最快 逐漸減慢)

android:interpolator="@android:anim/anticipate_interpolator" 先反向執(zhí)行一段,然后再加速反向回來(相當(dāng)于我們彈簧,先反向壓縮一小段,然后在加速?gòu)棾觯?
android:interpolator="@android:anim/anticipate_overshoot_interpolator" 同上先反向一段,然后加速反向回來,執(zhí)行完畢自帶回彈效果(更形象的彈簧效果)

android:interpolator="@android:anim/bounce_interpolator" 執(zhí)行完畢之后會(huì)回彈跳躍幾段(相當(dāng)于我們高空掉下一顆皮球,到地面是會(huì)跳動(dòng)幾下)

android:interpolator="@android:anim/cycle_interpolator" 循環(huán),動(dòng)畫循環(huán)一定次數(shù),值的改變?yōu)橐徽液瘮?shù):Math.sin(2* mCycles* Math.PI* input)

android:interpolator="@android:anim/linear_interpolator" 線性均勻改變

android:interpolator="@android:anim/overshoot_interpolator" 加速執(zhí)行,結(jié)束之后回彈

在代碼中設(shè)置,順序效果同上

animation.setInterpolator(new AccelerateInterpolator());

animation.setInterpolator(new DecelerateInterpolator());

animation.setInterpolator(new AccelerateDecelerateInterpolator());

animation.setInterpolator(new AnticipateInterpolator());

animation.setInterpolator(new AnticipateOvershootInterpolator());

animation.setInterpolator(new BounceInterpolator());

animation.setInterpolator(new CycleInterpolator(2));

animation.setInterpolator(new LinearInterpolator());

animation.setInterpolator(new OvershootInterpolator());

動(dòng)畫不設(shè)置Interpolator屬性即為默認(rèn)值,勻速
Interpolator屬性也可自定義

動(dòng)畫狀態(tài)的監(jiān)聽

  • setAnimationListener(new AnimationListener())方法,重寫下面的三個(gè)方法:
  • onAnimationStart():動(dòng)畫開始
  • onAnimationRepeat():動(dòng)畫重復(fù)
  • onAnimationEnd():動(dòng)畫結(jié)束

屬性動(dòng)畫

Animator ————
         ———— AnimatorSet
         ———— ValueAnimator
                    ———— ObjectAnimator
                    ———— TimeAnimator

AnimatorSet:Animator 子類,用來組合動(dòng)畫
ObjectAnimator:ValueAnimator 子類,

ValueAnimator

  • res/animator/***.xml 中定義ValueAnimator
    語法
    <animator
            android:duration="int"  
            android:valueFrom="float | int | color"
            android:valueTo="float | int | color"
            android:startOffset="int"
            android:repeatCount="int"
            android:repeatMode=["repeat" | "reverse"]
            android:valueType=["intType" | "floatType"]/>

使用

        //使用xml屬性動(dòng)畫
        ValueAnimator valueAnimator = (ValueAnimator) AnimatorInflater.loadAnimator(this, R.animator.valueanimator);
        //動(dòng)畫監(jiān)聽
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Integer animatedValue = (Integer) animation.getAnimatedValue();
                view.scrollTo(animatedValue,0);
            }
        });
        valueAnimator.start();
  • Java屬性介紹
        //動(dòng)畫從0到10再到5再到0
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 10, 5, 0);
        //ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 10, 5, 0);
        /*
        ofObject不同于ofFloat和ofInt,多了TypeEvaluator參數(shù);其實(shí)ofInt和ofFloat使用的是系統(tǒng)默認(rèn)的IntEvaluator和FloatEvaluator,而ofObject中參數(shù)TypeEvaluator需要我們自定義
        */
        //ValueAnimator.ofObject(TypeEvaluator evaluator, Object... values);后文介紹
        //動(dòng)畫中每一幀更新的時(shí)候調(diào)用
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                final float animatedValue = (float) animation.getAnimatedValue();
            }
        });
        //動(dòng)畫的監(jiān)聽
        valueAnimator.addListener(new AnimatorListenerAdapter() {
            //動(dòng)畫取消
            @Override
            public void onAnimationCancel(Animator animation) {
                super.onAnimationCancel(animation);
            }

            //動(dòng)畫結(jié)束
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
            }

            //動(dòng)畫重復(fù)
            @Override
            public void onAnimationRepeat(Animator animation) {
                super.onAnimationRepeat(animation);
            }

            //動(dòng)畫開始
            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
            }

            //動(dòng)畫暫停
            @Override
            public void onAnimationPause(Animator animation) {
                super.onAnimationPause(animation);
            }

            //動(dòng)畫重啟
            @Override
            public void onAnimationResume(Animator animation) {
                super.onAnimationResume(animation);
            }
        });
        //動(dòng)畫執(zhí)行時(shí)間
        valueAnimator.setDuration(500);
        //動(dòng)畫重復(fù)次數(shù)
        valueAnimator.setRepeatCount(Integer.MAX_VALUE);
        /*
        動(dòng)畫循環(huán)模式,有兩種取值:
        ValueAnimator.RESTART:重新開始
        ValueAnimator.REVERSE:反向重新開始
         */
        valueAnimator.setRepeatMode(ValueAnimator.REVERSE);
        //動(dòng)畫差值器
        valueAnimator.setInterpolator(new DecelerateInterpolator());
        //設(shè)置動(dòng)畫延遲播放
        valueAnimator.setStartDelay(1000);
        //開始播放動(dòng)畫
        valueAnimator.start();
  • TypeEvaluator

ObjectAnimator

  • 屬性介紹
    ObjectAnimator 使用與 唯一不同在于
        /**
         第二個(gè)參數(shù) propertyName 說明:
         alpha:透明度
         rotation:旋轉(zhuǎn)
         rotationX:圍繞x軸旋轉(zhuǎn)
         rotationY:圍繞y軸旋轉(zhuǎn)
         translationX:在x軸平移
         translationY:在y軸平移
         scaleX:在x軸縮放
         scaleY:在y軸縮放
         backgroundColor:可以修改控件背景顏色
         等等
         */
        ObjectAnimator alpha = ObjectAnimator.ofFloat(imageView, "alpha", 1, 10, 10, 5, 1);
        alphaAnimator.setDuration(2000);
        alphaAnimator.setRepeatMode(ValueAnimator.REVERSE);
        alphaAnimator.setRepeatCount(10);
        alphaAnimator.setInterpolator(new DecelerateInterpolator());
        alphaAnimator.start();
        alphaAnimator.setStartDelay(1000);

了解/學(xué)習(xí)更多資源ResourceCollection

?著作權(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,380評(píng)論 1 38
  • 1 背景 不能只分析源碼呀,分析的同時(shí)也要整理歸納基礎(chǔ)知識(shí),剛好有人微博私信讓全面說說Android的動(dòng)畫,所以今...
    未聞椛洺閱讀 2,844評(píng)論 0 10
  • 在日常的Android開發(fā)中,經(jīng)常會(huì)使用到動(dòng)畫,這里就對(duì)Android開發(fā)中的動(dòng)畫做一下總結(jié) Android 動(dòng)畫...
    李建彪閱讀 481評(píng)論 0 1
  • 在日常的Android開發(fā)中,經(jīng)常會(huì)使用到動(dòng)畫,這里就對(duì)Android開發(fā)中的動(dòng)畫做一下總結(jié)。 Android 動(dòng)...
    IAM四十二閱讀 133,550評(píng)論 23 349
  • 第二十五天-第二十八天。 在北京找實(shí)習(xí)四個(gè)禮拜整。 以“找實(shí)習(xí)無果,回校再閉關(guān)學(xué)習(xí)兩個(gè)月,為秋招做準(zhǔn)備”結(jié)局。 沒...
    Klein_kartoffel閱讀 320評(píng)論 0 0

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