溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android視圖動畫怎么實現(xiàn)

發(fā)布時間:2022-01-12 11:03:34 來源:億速云 閱讀:119 作者:iii 欄目:移動開發(fā)

本文小編為大家詳細介紹“Android視圖動畫怎么實現(xiàn)”,內(nèi)容詳細,步驟清晰,細節(jié)處理妥當(dāng),希望這篇“Android視圖動畫怎么實現(xiàn)”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識吧。

介紹

視圖動畫主要有兩種:

一、Tween Animation譯為“補間動畫”
1、scale譯為“規(guī)模、比例”,是對View進行特定范圍的縮放
2、alpha通過改變View的透明度實現(xiàn)View隱現(xiàn)的效果
3、translate譯為"轉(zhuǎn)移",是對View進行位置的移動
4、rotate譯為“旋轉(zhuǎn)”,是讓View圍繞特定的點進行旋轉(zhuǎn)
PS:所有View的移動、隱藏、旋轉(zhuǎn)僅僅是看到的動畫效果,實際View的位置/大小/比例并沒有發(fā)生本質(zhì)上的改變(比如說View的位置通過動畫進行移動后你注冊的點擊事件還是需要點擊到View的原始位置才可以被觸發(fā))。

二、Frame Animation譯為逐幀動畫
這個比較容易理解就是將多個具有特定連貫動作的圖片在短時間內(nèi)進行快速的切換達到動畫的效果,本質(zhì)上所有的動畫效果都是這種思想。

如何創(chuàng)建視圖動畫文件目錄

動畫文件要存放在res/anim文件夾下,訪問時采用R.anim.XXX的方式。默認是沒有這個文件夾的需要手動創(chuàng)建(右鍵res目錄-->New-->Android Resource Directory-->確定。)

Android視圖動畫怎么實現(xiàn)

image

動畫文件的創(chuàng)建方式為:右鍵anim文件夾選擇new,然后點擊Animation Resource file,選擇動畫類型即可創(chuàng)建。

Android視圖動畫怎么實現(xiàn)

image

輸入后會自動提示動畫名稱,然后輸入名稱,確定即可。

Android視圖動畫怎么實現(xiàn)

image

scale(縮放) 動畫

這個動畫參數(shù)相對來說比較多, 就我個人而言在學(xué)習(xí)這個動畫的時候花費時間是最長的。

這個動畫主要是實現(xiàn)View的縮放,首先要想,要實現(xiàn)一個縮放的動畫首先要確定什么參數(shù)/信息(好比說想切割一張?zhí)囟ù笮〉募垙堃_定寬和高一樣),那么第一個就是要確定要圍繞哪個點(pivot)進行縮放。

還需要知道在動畫開始(from)時View的大小(比例),以及動畫結(jié)束(to)時View要處于的大小(比例)。就是要確定以下六個參數(shù)才可以完成一次縮放動畫。

X則指定控件的寬度,Y則指定控件的高度,值越大則控件所占位置越大。

Android坐標從左上角開始算起。

Android視圖動畫怎么實現(xiàn)

image

其中fromXScaletoXScale、fromYScale、toYScale使用浮點類型,1.0表使原始大小,0.5則是縮放一半,1.5則是擴大原大小的一半。舉例:原View寬高100、150,1.0:(100,150),0.5:(50,75),1.5:(150,225)。也可以使用精確值(DP/PX)。

pivotX、pivotY有三種表使方法,第一是采用像素值,第二則是較自身的百分比,第三則是較父View的百分比。

為了方便觀察,使用兩個同等位置和大小不同顏色的View來進行觀察。動畫的播放代碼在最下文已給出。

<?xml version="1.0" encoding="utf-8"?><RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <Button android:layout_width="match_parent" android:layout_height="wrap_content"
            android:text="播放動畫"
            android:id="@+id/btnOpenAnimation"
    />
    <TextView android:layout_width="300dp" android:layout_height="300dp"
              android:layout_centerInParent="true"
              android:layout_gravity="center"
              android:background="#03A9F4"/>
    <TextView android:layout_width="300dp" android:layout_height="300dp"
              android:layout_centerInParent="true"
              android:id="@+id/txtAnimation"
              android:layout_gravity="center"
              android:background="#FF00"/></RelativeLayout>

示例1:使用像素值確定Pivot點

<?xml version="1.0" encoding="utf-8"?><scale xmlns:android="http://schemas.android.com/apk/res/android"
       android:fromXScale="0.5"
       android:toXScale="1.0"
       android:duration="3000"
       android:fromYScale="0.5"
       android:toYScale="1.0"
       android:pivotX="200"
       android:pivotY="200"></scale>

這里我們分別設(shè)置了pivotXpivotY為200(px),這個是從View本身來算起的,View的左上角為(0,0)點,然后X軸坐標向右,Y則向下分別走200像素,最終得到了箭頭指向的點(Pivot),那么開始點確定了。

再看其它參數(shù),fromXScale指定的是在動畫開始時X坐標也就是寬度的大小(這里是按照比例計算的),0.5則代表View原始寬度的一半,fromYScale則是高度了。

既然是向特定的比例進行縮放,僅僅確定開始的大小是不夠的,還要確定在動畫播放到最后所要達到的大小,所以就有了toXScaletoYScale,這兩個則是指定在動畫播放完成后View所處的大小。

這里我們是從View的0.5縮放到1.0,也就是從原始View的一半經(jīng)過3秒(duration用來控制動畫時長,1000為1秒。)后變成原始View的大小也就是1.0。

Android視圖動畫怎么實現(xiàn)

image

Android視圖動畫怎么實現(xiàn)

image

示例2:百分比Pivot

使用百分比確定Pivot也很簡單,那么Pivot的位置就是:以View的左上角即(0,0)點為基礎(chǔ)加上View特定的寬高百分比。

<?xml version="1.0" encoding="utf-8"?><scale xmlns:android="http://schemas.android.com/apk/res/android"
       android:fromXScale="0.0"
       android:toXScale="1.0"
       android:duration="5000"
       android:fromYScale="0.0"
       android:toYScale="1.0"
       android:pivotX="70%"
       android:pivotY="70%"></scale>

Android視圖動畫怎么實現(xiàn)

image

Android視圖動畫怎么實現(xiàn)

image

示例3:父View百分比Pivot

<?xml version="1.0" encoding="utf-8"?><scale xmlns:android="http://schemas.android.com/apk/res/android"
       android:fromXScale="0.0"
       android:toXScale="1.0"
       android:duration="5000"
       android:fromYScale="0.0"
       android:toYScale="1.0"
       android:pivotX="50%p"
       android:pivotY="50%p"></scale>

這個計算和上邊那個其實是一樣的,只是基于的點不同而已,上邊是基于自身來算起,那么這個則是基于View的父布局來計算的。那么Pivot的位置就是:以View的左上角即(0,0)點為基礎(chǔ)加上父View特定的寬高百分比。

Android視圖動畫怎么實現(xiàn)

image

Android視圖動畫怎么實現(xiàn)

image

alpha動畫

這個可以說就非常簡單了,主要是實現(xiàn)顏色的過度效果,fromAlpha則是動畫開始的透明度,toAlpha則是在動畫最后顯示的透明度。0.0代表完全透明1.0則是View的原色。

<?xml version="1.0" encoding="utf-8"?><alpha xmlns:android="http://schemas.android.com/apk/res/android"
       android:fromAlpha="0.0"
       android:duration="3000"
       android:toAlpha="1.0"></alpha>

Android視圖動畫怎么實現(xiàn)

image

rotate(旋轉(zhuǎn)) 動畫

首先要想完成旋轉(zhuǎn)要確定那些參數(shù)?肯定要確定旋轉(zhuǎn)要圍繞的點也就是pivot,這個在scale動畫也用到了,用法都一樣,不在多說。這里出現(xiàn)了名為degrees也就是角度的概念,也就是以特定點(pivot)為中心從多少度(fromDegrees),旋轉(zhuǎn)到多少度(toDegrees)。以下示例是從0轉(zhuǎn)到360度,正好一圈。

<?xml version="1.0" encoding="utf-8"?><rotate xmlns:android="http://schemas.android.com/apk/res/android"
        android:fromDegrees="0"
        android:toDegrees="360"
        android:pivotY="50%"
        android:pivotX="50%"
        android:duration="3000"></rotate>

Android視圖動畫怎么實現(xiàn)

image

translate(位移) 動畫

說白了就是移動View的位置,就是從一個點移動到另一個點,最重要的就是確定兩個點,那么則需要確定兩對X,Y坐標了。

以下參數(shù)的使用方式和在最上邊提到的pivot是一樣的都可以使用精確值和百分比。

<?xml version="1.0" encoding="utf-8"?><translate xmlns:android="http://schemas.android.com/apk/res/android"
           android:fromXDelta="10%"
           android:toXDelta="70%"
           android:fromYDelta="0%"
           android:toYDelta="70%"
           android:duration="3000"></translate>

其中黑色線條框住的是View的原始位置,黃色框住的是View動畫開始時的位置,紫色線條則是整個View的70%的占比,最后集中到的點就是View要移動到的最終的位置也就是結(jié)束點。

Android視圖動畫怎么實現(xiàn)

image

Set(集合動畫)

如何理解Set(集合動畫),其實很簡單,比如現(xiàn)在有一個需求,我們既要旋轉(zhuǎn)又要有漸漸出現(xiàn)的效果,那么就要使用set了,說白了就是將多個動畫組合起來。只要把上邊幾個都學(xué)會了,使用這個set簡直so easy。

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"
     android:duration="3000">
    <alpha
            android:fromAlpha="0.0"
            android:toAlpha="1.0"
    ></alpha>
    <rotate
            android:pivotX="50%"
            android:pivotY="50%"
            android:fromDegrees="0"
            android:toDegrees="180"
    ></rotate></set>

Android視圖動畫怎么實現(xiàn)

image

動態(tài)創(chuàng)建動畫

上邊所展示的都是通過xml文件寫的動畫,都是靜態(tài)寫好了的。那么想要動態(tài)的創(chuàng)建動畫對象又該如何?其實很簡單,通過代碼的方式創(chuàng)建動畫它們的名稱和使用xml文件創(chuàng)建時名稱都是對應(yīng)的,提供的構(gòu)造函數(shù)也都是必備的參數(shù)值。

 //創(chuàng)建Alpha動畫var alpha = AlphaAnimation(0.0F, 1.0F)
alpha.duration = 3000this.txtAnimation.startAnimation(alpha)//創(chuàng)建Rotate動畫var rotate = RotateAnimation(0F, 360F, Animation.RELATIVE_TO_SELF, 50F, Animation.RELATIVE_TO_SELF, 50F)//創(chuàng)建Scale動畫var scale = ScaleAnimation(0F, 1F, 0F, 1F, Animation.RELATIVE_TO_SELF, 50F, Animation.RELATIVE_TO_SELF, 50F)//創(chuàng)建translate動畫var translate = TranslateAnimation(
    Animation.RELATIVE_TO_SELF,    10F,
    Animation.RELATIVE_TO_SELF,    80F,
    Animation.RELATIVE_TO_SELF,    0F,
    Animation.RELATIVE_TO_SELF,    70F
)//創(chuàng)建Set動畫var set = AnimationSet(this, null)
set.duration = 3000set.addAnimation(alpha)
set.addAnimation(rotate)

從Animation繼承的屬性

以上所有的動畫對象都是從Animation類繼承來的,所有有一些公共的屬性也會繼承過來。

Android視圖動畫怎么實現(xiàn)

image

動畫的播放

//加載動畫this.btnOpenAnimation.setOnClickListener {    var animation = AnimationUtils.loadAnimation(this, R.anim.translate_anim)    this.txtAnimation.startAnimation(animation)
}

Tween Animation(逐幀動畫)

這個是Drawable形式的動畫,存放在drawable文件夾中,使用animation-list節(jié)點來表示。圖片素材是提前準備好的。自己動手嘗試下馬上就會理解了。

<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:duration="100" android:drawable="@drawable/a001"></item>
    <item android:duration="100" android:drawable="@drawable/a002"></item>
    <item android:duration="100" android:drawable="@drawable/a003"></item>
    <item android:duration="100" android:drawable="@drawable/a004"></item>
    <item android:duration="100" android:drawable="@drawable/a005"></item>
    <item android:duration="100" android:drawable="@drawable/a006"></item>
    <item android:duration="100" android:drawable="@drawable/a007"></item>
    <item android:duration="100" android:drawable="@drawable/a008"></item>
    <item android:duration="100" android:drawable="@drawable/a009"></item>
    <item android:duration="100" android:drawable="@drawable/a010"></item>
    <item android:duration="100" android:drawable="@drawable/a011"></item>
    <item android:duration="100" android:drawable="@drawable/a012"></item>
    <item android:duration="100" android:drawable="@drawable/a013"></item>
    <item android:duration="100" android:drawable="@drawable/a014"></item>
    <item android:duration="100" android:drawable="@drawable/a015"></item>
    <item android:duration="100" android:drawable="@drawable/a016"></item>
    <item android:duration="100" android:drawable="@drawable/a017"></item>
    <item android:duration="100" android:drawable="@drawable/a018"></item></animation-list>

這里我們給一個TextView設(shè)置了background屬性。

<?xml version="1.0" encoding="utf-8"?><RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <Button android:layout_width="match_parent" android:layout_height="wrap_content"
            android:text="播放動畫"
            android:id="@+id/btnOpenAnimation"
    />
    <TextView android:layout_width="300dp" android:layout_height="300dp"
              android:layout_centerInParent="true"
              android:id="@+id/txtAnimation"
              android:background="@drawable/anim_refresh"
              android:layout_gravity="center"
    /></RelativeLayout>

具體調(diào)用

var animationDrawable = this.txtAnimation.background as AnimationDrawable
animationDrawable.start()

顯示效果

Android視圖動畫怎么實現(xiàn)

讀到這里,這篇“Android視圖動畫怎么實現(xiàn)”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領(lǐng)會,如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI