本文小編為大家詳細(xì)介紹“Android視圖動畫怎么實(shí)現(xiàn)”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“Android視圖動畫怎么實(shí)現(xiàn)”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識吧。
成都創(chuàng)新互聯(lián)公司從2013年開始,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站設(shè)計、做網(wǎng)站網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元金平做網(wǎng)站,已為上家服務(wù),為金平各地企業(yè)和個人服務(wù),聯(lián)系電話:18982081108
視圖動畫主要有兩種:
一、Tween Animation譯為“補(bǔ)間動畫”
1、scale譯為“規(guī)模、比例”,是對View進(jìn)行特定范圍的縮放
2、alpha通過改變View的透明度實(shí)現(xiàn)View隱現(xiàn)的效果
3、translate譯為"轉(zhuǎn)移",是對View進(jìn)行位置的移動
4、rotate譯為“旋轉(zhuǎn)”,是讓View圍繞特定的點(diǎn)進(jìn)行旋轉(zhuǎn)
PS:所有View的移動、隱藏、旋轉(zhuǎn)僅僅是看到的動畫效果,實(shí)際View的位置/大小/比例并沒有發(fā)生本質(zhì)上的改變(比如說View的位置通過動畫進(jìn)行移動后你注冊的點(diǎn)擊事件還是需要點(diǎn)擊到View的原始位置才可以被觸發(fā))。
二、Frame Animation譯為逐幀動畫
這個比較容易理解就是將多個具有特定連貫動作的圖片在短時間內(nèi)進(jìn)行快速的切換達(dá)到動畫的效果,本質(zhì)上所有的動畫效果都是這種思想。
動畫文件要存放在res/anim文件夾下,訪問時采用R.anim.XXX的方式。默認(rèn)是沒有這個文件夾的需要手動創(chuàng)建(右鍵res目錄-->New-->Android Resource Directory-->確定。)

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

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

image
這個動畫參數(shù)相對來說比較多, 就我個人而言在學(xué)習(xí)這個動畫的時候花費(fèi)時間是最長的。
這個動畫主要是實(shí)現(xiàn)View的縮放,首先要想,要實(shí)現(xiàn)一個縮放的動畫首先要確定什么參數(shù)/信息(好比說想切割一張?zhí)囟ù笮〉募垙堃_定寬和高一樣),那么第一個就是要確定要圍繞哪個點(diǎn)(pivot)進(jìn)行縮放。
還需要知道在動畫開始(from)時View的大小(比例),以及動畫結(jié)束(to)時View要處于的大小(比例)。就是要確定以下六個參數(shù)才可以完成一次縮放動畫。
X則指定控件的寬度,Y則指定控件的高度,值越大則控件所占位置越大。
Android坐標(biāo)從左上角開始算起。

image
其中fromXScale、toXScale、fromYScale、toYScale使用浮點(diǎn)類型,1.0表使原始大小,0.5則是縮放一半,1.5則是擴(kuò)大原大小的一半。舉例:原View寬高100、150,1.0:(100,150),0.5:(50,75),1.5:(150,225)。也可以使用精確值(DP/PX)。
pivotX、pivotY有三種表使方法,第一是采用像素值,第二則是較自身的百分比,第三則是較父View的百分比。
為了方便觀察,使用兩個同等位置和大小不同顏色的View來進(jìn)行觀察。動畫的播放代碼在最下文已給出。
<?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點(diǎn)
<?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è)置了pivotX和pivotY為200(px),這個是從View本身來算起的,View的左上角為(0,0)點(diǎn),然后X軸坐標(biāo)向右,Y則向下分別走200像素,最終得到了箭頭指向的點(diǎn)(Pivot),那么開始點(diǎn)確定了。
再看其它參數(shù),fromXScale指定的是在動畫開始時X坐標(biāo)也就是寬度的大小(這里是按照比例計算的),0.5則代表View原始寬度的一半,fromYScale則是高度了。
既然是向特定的比例進(jìn)行縮放,僅僅確定開始的大小是不夠的,還要確定在動畫播放到最后所要達(dá)到的大小,所以就有了toXScale和toYScale,這兩個則是指定在動畫播放完成后View所處的大小。
這里我們是從View的0.5縮放到1.0,也就是從原始View的一半經(jīng)過3秒(duration用來控制動畫時長,1000為1秒。)后變成原始View的大小也就是1.0。

image

image
示例2:百分比Pivot
使用百分比確定Pivot也很簡單,那么Pivot的位置就是:以View的左上角即(0,0)點(diǎn)為基礎(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>

image

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

image

image
這個可以說就非常簡單了,主要是實(shí)現(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>

image
首先要想完成旋轉(zhuǎn)要確定那些參數(shù)?肯定要確定旋轉(zhuǎn)要圍繞的點(diǎn)也就是pivot,這個在scale動畫也用到了,用法都一樣,不在多說。這里出現(xiàn)了名為degrees也就是角度的概念,也就是以特定點(diǎn)(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>

image
說白了就是移動View的位置,就是從一個點(diǎn)移動到另一個點(diǎn),最重要的就是確定兩個點(diǎn),那么則需要確定兩對X,Y坐標(biāo)了。
以下參數(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%的占比,最后集中到的點(diǎn)就是View要移動到的最終的位置也就是結(jié)束點(diǎn)。

image
如何理解Set(集合動畫),其實(shí)很簡單,比如現(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>

image
上邊所展示的都是通過xml文件寫的動畫,都是靜態(tài)寫好了的。那么想要動態(tài)的創(chuàng)建動畫對象又該如何?其實(shí)很簡單,通過代碼的方式創(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類繼承來的,所有有一些公共的屬性也會繼承過來。

image
//加載動畫this.btnOpenAnimation.setOnClickListener { var animation = AnimationUtils.loadAnimation(this, R.anim.translate_anim) this.txtAnimation.startAnimation(animation)
}這個是Drawable形式的動畫,存放在drawable文件夾中,使用animation-list節(jié)點(diǎn)來表示。圖片素材是提前準(zhǔn)備好的。自己動手嘗試下馬上就會理解了。
<?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視圖動畫怎么實(shí)現(xiàn)”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點(diǎn)還需要大家自己動手實(shí)踐使用過才能領(lǐng)會,如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
文章題目:Android視圖動畫怎么實(shí)現(xiàn)
網(wǎng)址分享:http://chinadenli.net/article38/ipghsp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、品牌網(wǎng)站制作、企業(yè)網(wǎng)站制作、域名注冊、網(wǎng)站改版、商城網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)