本文小編為大家詳細(xì)介紹“Android視圖動(dòng)畫怎么實(shí)現(xiàn)”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“Android視圖動(dòng)畫怎么實(shí)現(xiàn)”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來(lái)學(xué)習(xí)新知識(shí)吧。
成都創(chuàng)新互聯(lián)公司從2013年開(kāi)始,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站設(shè)計(jì)、做網(wǎng)站網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元金平做網(wǎng)站,已為上家服務(wù),為金平各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18982081108
視圖動(dòng)畫主要有兩種:
一、Tween Animation
譯為“補(bǔ)間動(dòng)畫”
1、scale
譯為“規(guī)模、比例”,是對(duì)View進(jìn)行特定范圍的縮放
2、alpha
通過(guò)改變View的透明度實(shí)現(xiàn)View隱現(xiàn)的效果
3、translate
譯為"轉(zhuǎn)移",是對(duì)View進(jìn)行位置的移動(dòng)
4、rotate
譯為“旋轉(zhuǎn)”,是讓View圍繞特定的點(diǎn)進(jìn)行旋轉(zhuǎn)
PS:所有View的移動(dòng)、隱藏、旋轉(zhuǎn)僅僅是看到的動(dòng)畫效果,實(shí)際View的位置/大小/比例并沒(méi)有發(fā)生本質(zhì)上的改變(比如說(shuō)View的位置通過(guò)動(dòng)畫進(jìn)行移動(dòng)后你注冊(cè)的點(diǎn)擊事件還是需要點(diǎn)擊到View的原始位置才可以被觸發(fā))。
二、Frame Animation
譯為逐幀動(dòng)畫
這個(gè)比較容易理解就是將多個(gè)具有特定連貫動(dòng)作的圖片在短時(shí)間內(nèi)進(jìn)行快速的切換達(dá)到動(dòng)畫的效果,本質(zhì)上所有的動(dòng)畫效果都是這種思想。
動(dòng)畫文件要存放在res/anim文件夾下,訪問(wèn)時(shí)采用R.anim.XXX的方式。默認(rèn)是沒(méi)有這個(gè)文件夾的需要手動(dòng)創(chuàng)建(右鍵res目錄-->New-->Android Resource Directory-->確定。)
image
動(dòng)畫文件的創(chuàng)建方式為:右鍵anim
文件夾選擇new,然后點(diǎn)擊Animation Resource file,選擇動(dòng)畫類型即可創(chuàng)建。
image
輸入后會(huì)自動(dòng)提示動(dòng)畫名稱,然后輸入名稱,確定即可。
image
這個(gè)動(dòng)畫參數(shù)相對(duì)來(lái)說(shuō)比較多, 就我個(gè)人而言在學(xué)習(xí)這個(gè)動(dòng)畫的時(shí)候花費(fèi)時(shí)間是最長(zhǎng)的。
這個(gè)動(dòng)畫主要是實(shí)現(xiàn)View的縮放,首先要想,要實(shí)現(xiàn)一個(gè)縮放的動(dòng)畫首先要確定什么參數(shù)/信息(好比說(shuō)想切割一張?zhí)囟ù笮〉募垙堃_定寬和高一樣),那么第一個(gè)就是要確定要圍繞哪個(gè)點(diǎn)(pivot)進(jìn)行縮放。
還需要知道在動(dòng)畫開(kāi)始(from)時(shí)View的大小(比例),以及動(dòng)畫結(jié)束(to)時(shí)View要處于的大小(比例)。就是要確定以下六個(gè)參數(shù)才可以完成一次縮放動(dòng)畫。
X則指定控件的寬度,Y則指定控件的高度,值越大則控件所占位置越大。
Android坐標(biāo)從左上角開(kāi)始算起。
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的百分比。
為了方便觀察,使用兩個(gè)同等位置和大小不同顏色的View來(lái)進(jìn)行觀察。動(dòng)畫的播放代碼在最下文已給出。
<?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="播放動(dòng)畫" 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),這個(gè)是從View本身來(lái)算起的,View的左上角為(0,0)點(diǎn),然后X軸坐標(biāo)向右,Y則向下分別走200像素,最終得到了箭頭指向的點(diǎn)(Pivot),那么開(kāi)始點(diǎn)確定了。
再看其它參數(shù),fromXScale
指定的是在動(dòng)畫開(kāi)始時(shí)X坐標(biāo)也就是寬度的大小(這里是按照比例計(jì)算的),0.5則代表View原始寬度的一半,fromYScale
則是高度了。
既然是向特定的比例進(jìn)行縮放,僅僅確定開(kāi)始的大小是不夠的,還要確定在動(dòng)畫播放到最后所要達(dá)到的大小,所以就有了toXScale
和toYScale
,這兩個(gè)則是指定在動(dòng)畫播放完成后View所處的大小。
這里我們是從View的0.5縮放到1.0,也就是從原始View的一半經(jīng)過(guò)3秒(duration
用來(lái)控制動(dòng)畫時(shí)長(zhǎng),1000為1秒。)后變成原始View的大小也就是1.0。
image
image
示例2:百分比Pivot
使用百分比確定Pivot也很簡(jiǎn)單,那么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>
這個(gè)計(jì)算和上邊那個(gè)其實(shí)是一樣的,只是基于的點(diǎn)不同而已,上邊是基于自身來(lái)算起,那么這個(gè)則是基于View的父布局來(lái)計(jì)算的。那么Pivot的位置就是:以View的左上角即(0,0)點(diǎn)為基礎(chǔ)加上父View特定的寬高百分比。
image
image
這個(gè)可以說(shuō)就非常簡(jiǎn)單了,主要是實(shí)現(xiàn)顏色的過(guò)度效果,fromAlpha
則是動(dòng)畫開(kāi)始的透明度,toAlpha
則是在動(dòng)畫最后顯示的透明度。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
,這個(gè)在scale
動(dòng)畫也用到了,用法都一樣,不在多說(shuō)。這里出現(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
說(shuō)白了就是移動(dòng)View的位置,就是從一個(gè)點(diǎn)移動(dòng)到另一個(gè)點(diǎn),最重要的就是確定兩個(gè)點(diǎn),那么則需要確定兩對(duì)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動(dòng)畫開(kāi)始時(shí)的位置,紫色線條則是整個(gè)View的70%的占比,最后集中到的點(diǎn)就是View要移動(dòng)到的最終的位置也就是結(jié)束點(diǎn)。
image
如何理解Set
(集合動(dòng)畫),其實(shí)很簡(jiǎn)單,比如現(xiàn)在有一個(gè)需求,我們既要旋轉(zhuǎn)又要有漸漸出現(xiàn)的效果,那么就要使用set
了,說(shuō)白了就是將多個(gè)動(dòng)畫組合起來(lái)。只要把上邊幾個(gè)都學(xué)會(huì)了,使用這個(gè)set
簡(jiǎn)直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
上邊所展示的都是通過(guò)xml文件寫的動(dòng)畫,都是靜態(tài)寫好了的。那么想要?jiǎng)討B(tài)的創(chuàng)建動(dòng)畫對(duì)象又該如何?其實(shí)很簡(jiǎn)單,通過(guò)代碼的方式創(chuàng)建動(dòng)畫它們的名稱和使用xml文件創(chuàng)建時(shí)名稱都是對(duì)應(yīng)的,提供的構(gòu)造函數(shù)也都是必備的參數(shù)值。
//創(chuàng)建Alpha動(dòng)畫var alpha = AlphaAnimation(0.0F, 1.0F) alpha.duration = 3000this.txtAnimation.startAnimation(alpha)//創(chuàng)建Rotate動(dòng)畫var rotate = RotateAnimation(0F, 360F, Animation.RELATIVE_TO_SELF, 50F, Animation.RELATIVE_TO_SELF, 50F)//創(chuàng)建Scale動(dòng)畫var scale = ScaleAnimation(0F, 1F, 0F, 1F, Animation.RELATIVE_TO_SELF, 50F, Animation.RELATIVE_TO_SELF, 50F)//創(chuàng)建translate動(dòng)畫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動(dòng)畫var set = AnimationSet(this, null) set.duration = 3000set.addAnimation(alpha) set.addAnimation(rotate)
以上所有的動(dòng)畫對(duì)象都是從Animation類繼承來(lái)的,所有有一些公共的屬性也會(huì)繼承過(guò)來(lái)。
image
//加載動(dòng)畫this.btnOpenAnimation.setOnClickListener { var animation = AnimationUtils.loadAnimation(this, R.anim.translate_anim) this.txtAnimation.startAnimation(animation) }
這個(gè)是Drawable形式的動(dòng)畫,存放在drawable文件夾中,使用animation-list
節(jié)點(diǎn)來(lái)表示。圖片素材是提前準(zhǔn)備好的。自己動(dòng)手嘗試下馬上就會(huì)理解了。
<?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>
這里我們給一個(gè)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="播放動(dòng)畫" 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視圖動(dòng)畫怎么實(shí)現(xiàn)”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過(guò)才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
文章題目:Android視圖動(dòng)畫怎么實(shí)現(xiàn)
網(wǎng)址分享:http://m.rwnh.cn/article38/ipghsp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、品牌網(wǎng)站制作、企業(yè)網(wǎng)站制作、域名注冊(cè)、網(wǎng)站改版、商城網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)