Android動(dòng)畫(huà)學(xué)習(xí)筆記之補(bǔ)間動(dòng)畫(huà)
本文實(shí)例為大家分享了Android補(bǔ)間動(dòng)畫(huà)展示的具體代碼,供大家參考,具體內(nèi)容如下
首先看看補(bǔ)間動(dòng)畫(huà)的共同屬性:
Duration:動(dòng)畫(huà)持續(xù)的時(shí)間(單位:毫秒)
fillAfter:設(shè)置為true,動(dòng)畫(huà)轉(zhuǎn)化在動(dòng)畫(huà)被結(jié)束后被應(yīng)用
fillBefore:設(shè)置為true,動(dòng)畫(huà)轉(zhuǎn)化在動(dòng)畫(huà)開(kāi)始前被應(yīng)用
interpolator:動(dòng)畫(huà)插入器(加速、減速插入器)
repeatCount:動(dòng)畫(huà)重復(fù)的次數(shù)
repeatMode:順序動(dòng)畫(huà)(restart)/倒序動(dòng)畫(huà)(reverse)
startOffset:動(dòng)畫(huà)之間時(shí)間間隔
對(duì)于動(dòng)畫(huà)的創(chuàng)建一般有兩種方式:
第一種是在res/新建一個(gè)anim文件夾,然后在其下面分別建立四種動(dòng)畫(huà)
第二種方式是通過(guò)java代碼的方式創(chuàng)建
在補(bǔ)間動(dòng)畫(huà)中我們通常有以下四種動(dòng)畫(huà):
位移動(dòng)畫(huà)
創(chuàng)建方式一:
在anim文件下新建一個(gè)translate資源文件
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="0" //設(shè)置從x的哪個(gè)點(diǎn)起,也可以設(shè)置為百分比,以控件的寬為基準(zhǔn) android:toXDelta="100" //設(shè)置移動(dòng)到目標(biāo)x點(diǎn) android:fromYDelta="0" //設(shè)置從y的哪個(gè)點(diǎn)起,也可以設(shè)置為百分比,以控件的高為基準(zhǔn) android:toYDelta="0" //設(shè)置移動(dòng)到目標(biāo)y點(diǎn) android:repeatCount="2" //動(dòng)畫(huà)重復(fù)兩次,實(shí)際上你會(huì)發(fā)現(xiàn)是重復(fù)3次 //(restart模式下執(zhí)行相同方向3次) //這里的意思是,在首次執(zhí)行完之后再重復(fù)2次 //所以總的執(zhí)行次數(shù)為 n + 1次 //如果是reverse模式下,則反序移動(dòng)也算一次,所以在反序模式下 //往同一方向只有兩次,加上反序的一次剛好就是3次 android:repeatMode="restart" //沿當(dāng)前方向順序移動(dòng)一次,沒(méi)有反序移動(dòng), //如果設(shè)置為reverse則有一次順序/反序移動(dòng)的操作, android:duration="2000" //完成該動(dòng)作需要2秒 > </translate> //通過(guò)以下代碼注冊(cè)該動(dòng)畫(huà) private void creatTranslateByInflate(){ Animation animation = AnimationUtils.loadAnimation(this, R.anim.translate); mCircle.startAnimation(animation); }
創(chuàng)建方式二:(代碼創(chuàng)建)
private void creatTranslateByCode() { TranslateAnimation animation = new TranslateAnimation(0, 100, 0, 0); animation.setDuration(2000); animation.setRepeatMode(Animation.REVERSE); animation.setRepeatCount(2); mCircle.startAnimation(animation); }
旋轉(zhuǎn)動(dòng)畫(huà)
創(chuàng)建方式一:
<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="0"http://起始的角度數(shù), android:toDegrees="180"http://終止的角度,順時(shí)針(起始度 < 終止的角度) 逆時(shí)針 //(起始度 > 終止的角度) //這里就是順時(shí)針的旋轉(zhuǎn)180 android:pivotX="50%" //旋轉(zhuǎn)中軸的x點(diǎn),50%表示以控件寬為基準(zhǔn),在控件的中間x點(diǎn) android:pivotY="50%" //旋轉(zhuǎn)中軸的y點(diǎn),50%表示以控件高為基準(zhǔn),在控件的中間y點(diǎn) android:duration="2000" android:repeatMode="reverse" android:repeatCount="2" > </rotate> //通過(guò)以下代碼注冊(cè)該動(dòng)畫(huà) private void createRotateByInflate(){ Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate); mCircle.startAnimation(animation); }
創(chuàng)建方式二:(代碼創(chuàng)建)
private void createRotateByCode() { float pivotX = mCircle.getWidth() / 2.0f; float pivotY = mCircle.getHeight() / 2.0f; RotateAnimation animation = new RotateAnimation(0, 180, pivotX, pivotY); animation.setDuration(2000); animation.setRepeatMode(Animation.REVERSE); animation.setRepeatCount(2); mCircle.startAnimation(animation); }
縮放動(dòng)畫(huà)
創(chuàng)建方式一:
<?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:fromXScale="0.1"http:// android:toXScale="1.0" // android:fromYScale="0.1" android:toYScale="1.0" android:pivotY="50%" android:pivotX="50%" android:duration="2000" android:repeatMode="reverse" android:repeatCount="2"> </scale> //通過(guò)以下代碼注冊(cè)該動(dòng)畫(huà) private void createScaleByInflate(){ Animation animation = AnimationUtils.loadAnimation(this, R.anim.scale); mCircle.startAnimation(animation); }
創(chuàng)建方式二:(代碼創(chuàng)建)
private void createScaleByCode() { //創(chuàng)建動(dòng)畫(huà)Animation.RELATIVE_TO_PARENT 以父容器為參照物 //Animation.RELATIVE_TO_SELF 以自己為參照物, 如果以父容器為參照物會(huì)導(dǎo)致控件移動(dòng) float pivotX = mCircle.getWidth() / 2.0f; float pivotY = mCircle.getHeight() / 2.0f; ScaleAnimation animation = new ScaleAnimation(0.1f, 1.0f, 0.1f, 1.0f, pivotX,pivotY); animation.setDuration(2000); animation.setRepeatMode(Animation.REVERSE); animation.setRepeatCount(2); mCircle.startAnimation(animation); }
漸變動(dòng)畫(huà)
創(chuàng)建方式一:
<?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:fromAlpha="0.1" android:toAlpha="1.0" android:repeatMode="reverse" android:repeatCount="2" android:duration="2000"> </alpha> //通過(guò)以下代碼注冊(cè)該動(dòng)畫(huà) private void createAlphaByInflate(){ Animation animation = AnimationUtils.loadAnimation(this, R.anim.alpha); mCircle.startAnimation(animation); }
創(chuàng)建方式二:(代碼創(chuàng)建)
private void createAlphaByCode() { AlphaAnimation animation = new AlphaAnimation(0.1f, 1.0f); animation.setDuration(2000); animation.setRepeatMode(Animation.REVERSE); animation.setRepeatCount(2); mCircle.startAnimation(animation); }
以上的四種可以單獨(dú)使用也可以結(jié)合起來(lái)使用,如果要結(jié)合起來(lái)使用的話(huà),直接在anim文件夾下創(chuàng)建set集合,然后將需要結(jié)合的動(dòng)畫(huà)
放在一起即可
如下示例:
<?xml version="1.0" encoding="UTF-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <rotate android:toDegrees="3600" android:pivotY="50%" android:pivotX="50%" android:fromDegrees="0" android:duration="2000"/> <translate android:duration="3000" android:toYDelta="100%" android:toXDelta="150%" android:fromYDelta="-150%" android:fromXDelta="-100%"/> <alpha android:duration="5000" android:toAlpha="1.0" android:repeatMode="reverse" android:repeatCount="3" android:fromAlpha="0.5"/> </set>
基本的創(chuàng)建方式,以及基本屬性都在這里了,至于如何實(shí)現(xiàn)一個(gè)具有美感的效果圖,那就看個(gè)人的設(shè)計(jì)感了。
最后看看運(yùn)行結(jié)果圖
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android Studio實(shí)現(xiàn)補(bǔ)間動(dòng)畫(huà)
- Android動(dòng)畫(huà)系列之幀動(dòng)畫(huà)和補(bǔ)間動(dòng)畫(huà)的示例代碼
- Android補(bǔ)間動(dòng)畫(huà)基本使用(位移、縮放、旋轉(zhuǎn)、透明)
- Android旋轉(zhuǎn)、平移、縮放和透明度漸變的補(bǔ)間動(dòng)畫(huà)
- Android控件Tween動(dòng)畫(huà)(補(bǔ)間動(dòng)畫(huà))實(shí)現(xiàn)方法示例
- android 幀動(dòng)畫(huà),補(bǔ)間動(dòng)畫(huà),屬性動(dòng)畫(huà)的簡(jiǎn)單總結(jié)
- Android幀動(dòng)畫(huà)、補(bǔ)間動(dòng)畫(huà)、屬性動(dòng)畫(huà)用法詳解
- Android動(dòng)畫(huà)之補(bǔ)間動(dòng)畫(huà)(Tween Animation)基礎(chǔ)學(xué)習(xí)
- Android動(dòng)畫(huà)之補(bǔ)間動(dòng)畫(huà)(Tween Animation)實(shí)例詳解
- Android?Studio實(shí)現(xiàn)簡(jiǎn)單補(bǔ)間動(dòng)畫(huà)
相關(guān)文章
Android進(jìn)階KOOM線(xiàn)上APM監(jiān)控全面剖析
這篇文章主要為大家介紹了Android進(jìn)階KOOM線(xiàn)上APM監(jiān)控全面剖析詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01Android實(shí)現(xiàn)創(chuàng)意LoadingView動(dòng)畫(huà)效果
這篇文章主要介紹了Android實(shí)現(xiàn)創(chuàng)意LoadingView動(dòng)畫(huà)效果的相關(guān)資料,需要的朋友可以參考下2016-02-02Android布局中g(shù)ravity與layout_gravity屬性說(shuō)明
這篇文章主要介紹了Android布局中g(shù)ravity與layout_gravity屬性說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01Android利用Java優(yōu)雅消除復(fù)雜條件表達(dá)式的方法
這篇文章主要介紹了Android利用Java優(yōu)雅消除復(fù)雜條件表達(dá)式,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值。感興趣的小伙伴可以參考一下2022-06-06Flutter自定義實(shí)現(xiàn)彈出層的示例代碼
這篇文章主要為大家詳細(xì)介紹了Flutter如何自定義組件實(shí)現(xiàn)彈出層的效果,?文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-08-08Android音頻開(kāi)發(fā)之音頻采集的實(shí)現(xiàn)示例
本篇文章主要介紹了Android音頻開(kāi)發(fā)之音頻采集的實(shí)現(xiàn)示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04AndroidHttpClient詳解及調(diào)用示例
本文給大家介紹AndroidHttpClient結(jié)構(gòu)、使用方式及調(diào)用示例詳解,需要的朋友可以參考下2015-10-10android 監(jiān)聽(tīng)網(wǎng)絡(luò)狀態(tài)的變化及實(shí)戰(zhàn)的示例代碼
本篇文章主要介紹了android 監(jiān)聽(tīng)網(wǎng)絡(luò)狀態(tài)的變化及實(shí)戰(zhàn)的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01Android 詳解沉浸式狀態(tài)欄的實(shí)現(xiàn)流程
沉浸式就是要給用戶(hù)提供完全沉浸的體驗(yàn),使用戶(hù)有一種置身于虛擬世界之中的感覺(jué)。沉浸式模式就是整個(gè)屏幕中顯示都是應(yīng)用的內(nèi)容,沒(méi)有狀態(tài)欄也沒(méi)有導(dǎo)航欄,用戶(hù)不會(huì)被一些系統(tǒng)的界面元素所打擾,讓我們來(lái)實(shí)現(xiàn)下網(wǎng)上傳的沸沸揚(yáng)揚(yáng)的安卓沉浸式狀態(tài)欄2021-11-11