Android動畫詳解之Tween動畫

2021-08-27 12:33:01 字數 4394 閱讀 5936

乙個tween動畫將對於view物件的內容進行一系列簡單的轉換,在animation提供了所以關於tween動畫的類,主要有四個常用的類,alphaanimation(透明度漸變),rotateanimation(旋轉動畫),scaleanimation(縮放動畫),translateanimation(移動動畫),animationset(乙個動畫的集合類),以下是對常用動畫特效類的構造方法的作用和引數進行講解

(1) alphaanimation

public alphaanimation(float fromalpha, float toalpha)

fromalpha - 開始時候的透明度,其中1表示完全不透明,0表示完全透明的

toalpha 結束時候的透明度

setduration(long durationmillis) 設定動畫執行的時間

setfillafter(boolean fillafter) 設定為true時,動畫停在執行完後的效果,預設是執行完動畫回到剛開始的效果

setrepeatcount(int repeatcount) 設定動畫重複次數,repeatcount預設為0,即執行一次,為1時,即執行2次

setrepeatmode(int repeatmode) 設定動畫重複的模式,有animation.reverse和animation.restart兩種方式,預設為animation.restart,animation.restart的意思就是說比如你設定重複次數為1,當執行完第一次動畫之後,回到動畫開始然後執行第二次動畫,而你設定animation.reverse時候,比如你動畫是從不透明----->透明,執行完第一次動畫的時候,變為不透明,然後執行第二次動畫,他就從不透明到透明,不知道大家理解我的意思了沒?

我就介紹幾個常用的方法,其他的動畫也有上面的那些方法,然後等下介紹setinterpolator(interpolator)方法

(2)rotateanimation

public rotateanimation(float fromdegrees, float todegrees, int pivotxtype, float pivotxvalue, int pivotytype, float pivotyvalue)

fromdegrees 動畫開始的角度

todegrees 動畫結束的角度

pivotxvalue,pivotyvalue 繞著旋轉的中心點的x座標和y座標

pivotxtype,pivotytype 旋轉中心點的的相對關係型別,有三種animation.absolute,animation.relative_to_self,或animation.relative_to_parent,animation.absolute絕對座標型別,也就是相對o點的位置,animation.relative_to_self相對自己,自己檢視的左上角那個點為o點位置,animation.relative_to_parent相對父檢視左上角那個點為o點位置,即自己view所在的viewgroup的位置

(3)scaleanimation

public scaleanimation(float fromx, float tox, float fromy, float toy, int pivotxtype, float pivotxvalue, int pivotytype, float pivotyvalue)

float fromx, float tox x軸方向從開始的大小到結束的大小

float fromy, float toy y軸方向從開始的大小到結束的大小

pivotxvalue,pivotyvalue 繞著縮放的中心點的x座標和y座標

pivotxtype,pivotytype 縮放中心點的的相對關係型別,有三種animation.absolute,animation.relative_to_self,或animation.relative_to_parent,animation.absolute絕對座標型別,也就是相對o點的位置,animation.relative_to_self相對自己,自己檢視的左上角那個點為o點位置,animation.relative_to_parent相對父檢視左上角那個點為o點位置,即自己view所在的viewgroup的位置

(4)translateanimation

public translateanimation(int fromxtype, float fromxvalue, int toxtype, float toxvalue, int fromytype, float fromyvalue, int toytype, float toyvalue)

fromxtype x軸上開始點相對型別

fromxvalue 開始點的值

toxtype x軸上結束點相對型別

toxvalue,結束點的值

y軸同理

(5)animationset

這是乙個動畫的集合類,可以設定多個動畫一起執行,比較簡單,我就不多介紹了

interpolator的解釋

interpolator定義乙個動畫的變化率(the rate of change)。這使得基本的動畫效果(alpha, scale, translate, rotate)得以加速,減速,重複等。

acceleratedecelerateinterpolator

在動畫開始與介紹的地方速率改變比較慢,在中間的時候加速

accelerateinterpolator

在動畫開始的地方速率改變比較慢,然後開始加速

cycleinterpolator

decelerateinterpolator

在動畫開始的地方速率改變比較慢,然後開始減速

linearinterpolator

在動畫的以均勻的速率改變

上面介紹的是通過**構造的動畫,當然我們也能通過xml檔案寫動畫,個人推薦使用xml檔案

動畫放在res下的anim下,下面我來介紹用**生成和xml檔案的方式

alphaanimation**實現

//構造透明變化動畫

animation alphaanimation = new alphaanimation(1.0f, 0.0f);

//設定動畫執行時間

alphaanimation.setduration(2000);

//設定動畫重複方式

alphaanimation.setrepeatmode(animation.reverse);

//設定動畫重複次數

alphaanimation.setrepeatcount(5);

//設定動畫變化率

alphaanimation.setinterpolator(new accelerateinterpolator());

alphaanimationxml實現

<?xml version="1.0" encoding="utf-8"?>

通過animationutils.loadanimation(this, r.anim.alpha)就能拿到動畫了

rotateanimation**實現

animation rotateanimation = new rotateanimation(0, 360, animation.relative_to_self, 0.5f, animation.relative_to_self, 0.5f);

rotateanimation.setduration(2000);

rotateanimation.setrepeatmode(animation.restart);

rotateanimation.setrepeatcount(5);

rotateanimation.setinterpolator(new linearinterpolator());

rotateanimation xml實現

<?xml version="1.0" encoding="utf-8"?>

值得注意的地方是android:pivotx="50%",android:pivoty="50%" 當相對自己的是要加"%",相對父容器就不要加「%",這裡就這個比較重要

rotateanimation

動畫可以自定義圓形進度條,給個例子吧,用的是xml檔案定義的

<?xml version="1.0" encoding="utf-8"?>

其他兩種就自行實現吧,相信大家看了介紹,實現另外兩種不是很困難,tween動畫就介紹到這裡了,寫的很亂,希望您提出寶貴的意見和建議,謝謝!不說了,吃飯去了!

Android動畫之幀動畫詳解

xml資源檔案方式 1.在res drawable目錄中放入需要的 3.在布局檔案中進行設定animationdrawable animationdrawable animationdrawable imageview.getbackground 開始動畫 animationdrawable.sta...

Android 動畫 動畫詳解之屬性動畫(五)

在前幾篇中,我們了解了補間動畫 插值器和屬性動畫中的valueanimator,這一篇,我們來了解下屬性動畫中的objectanimator objectanimator是通過指定屬性所對應的set方法來改變的。比如,我們上面指定的改變rotation的屬性值,在做動畫時就會到指定控制項 textv...

Android動畫之屬性動畫

補間動畫,只是乙個動畫效果,元件其實還在原來的位置上,xy沒有改變,屬性動畫則反之 import android.animation.animator import android.animation.animatorinflater import android.animation.animato...