Android動畫合集之補(bǔ)間動畫
本節(jié)引言:
本節(jié)帶來的是Android三種動畫中的第二種——補(bǔ)間動畫(Tween),和前面學(xué)的幀動畫不同,幀動畫 是通過連續(xù)播放圖片來模擬動畫效果,而補(bǔ)間動畫開發(fā)者只需指定動畫開始,以及動畫結(jié)束"關(guān)鍵幀", 而動畫變化的"中間幀"則由系統(tǒng)計算并補(bǔ)齊!好了,開始本節(jié)學(xué)習(xí)~
1.補(bǔ)間動畫的分類和Interpolator
Andoird所支持的補(bǔ)間動畫效果有如下這五種,或者說四種吧,第五種是前面幾種的組合而已~
- AlphaAnimation:透明度漸變效果,創(chuàng)建時許指定開始以及結(jié)束透明度,還有動畫的持續(xù) 時間,透明度的變化范圍(0,1),0是完全透明,1是完全不透明;對應(yīng)<alpha/>標(biāo)簽!
- ScaleAnimation:縮放漸變效果,創(chuàng)建時需指定開始以及結(jié)束的縮放比,以及縮放參考點(diǎn), 還有動畫的持續(xù)時間;對應(yīng)<scale/>標(biāo)簽!
- TranslateAnimation:位移漸變效果,創(chuàng)建時指定起始以及結(jié)束位置,并指定動畫的持續(xù) 時間即可;對應(yīng)<translate/>標(biāo)簽!
- RotateAnimation:旋轉(zhuǎn)漸變效果,創(chuàng)建時指定動畫起始以及結(jié)束的旋轉(zhuǎn)角度,以及動畫 持續(xù)時間和旋轉(zhuǎn)的軸心;對應(yīng)<rotate/>標(biāo)簽
- AnimationSet:組合漸變,就是前面多種漸變的組合,對應(yīng)<set/>標(biāo)簽
在開始講解各種動畫的用法之前,我們先要來講解一個東西:Interpolator
用來控制動畫的變化速度,可以理解成動畫渲染器,當(dāng)然我們也可以自己實(shí)現(xiàn)Interpolator 接口,自行來控制動畫的變化速度,而Android中已經(jīng)為我們提供了五個可供選擇的實(shí)現(xiàn)類:
- LinearInterpolator:動畫以均勻的速度改變
- AccelerateInterpolator:在動畫開始的地方改變速度較慢,然后開始加速
- AccelerateDecelerateInterpolator:在動畫開始、結(jié)束的地方改變速度較慢,中間時加速
- CycleInterpolator:動畫循環(huán)播放特定次數(shù),變化速度按正弦曲線改變: Math.sin(2 * mCycles * Math.PI * input)
- DecelerateInterpolator:在動畫開始的地方改變速度較快,然后開始減速
- AnticipateInterpolator:反向,先向相反方向改變一段再加速播放
- AnticipateOvershootInterpolator:開始的時候向后然后向前甩一定值后返回最后的值
- BounceInterpolator: 跳躍,快到目的值時值會跳躍,如目的值100,后面的值可能依次為85,77,70,80,90,100
- OvershottInterpolator:回彈,最后超出目的值然后緩慢改變到目的值
而這個東東,我們一般是在寫動畫xml文件時會用到,屬性是:android:interpolator, 而上面對應(yīng)的值是:@android:anim/linear_interpolator,其實(shí)就是駝峰命名法變下劃線而已 AccelerateDecelerateInterpolator對應(yīng):@android:anim/accelerate_decelerate_interpolator!
2.各種動畫的詳細(xì)講解
這里的android:duration都是動畫的持續(xù)時間,單位是毫秒~
1)AlphaAnimation(透明度漸變)
anim_alpha.xml:
<alpha xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromAlpha="1.0" android:toAlpha="0.1" android:duration="2000"/>
屬性解釋:
fromAlpha :起始透明度
toAlpha:結(jié)束透明度
透明度的范圍為:0-1,完全透明-完全不透明
2)ScaleAnimation(縮放漸變)
anim_scale.xml:
<scale xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator" android:fromXScale="0.2" android:toXScale="1.5" android:fromYScale="0.2" android:toYScale="1.5" android:pivotX="50%" android:pivotY="50%" android:duration="2000"/>
屬性解釋:
- fromXScale/fromYScale:沿著X軸/Y軸縮放的起始比例
- toXScale/toYScale:沿著X軸/Y軸縮放的結(jié)束比例
- pivotX/pivotY:縮放的中軸點(diǎn)X/Y坐標(biāo),即距離自身左邊緣的位置,比如50%就是以圖像的 中心為中軸點(diǎn)
3)TranslateAnimation(位移漸變)
anim_translate.xml:
<translate xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromXDelta="0" android:toXDelta="320" android:fromYDelta="0" android:toYDelta="0" android:duration="2000"/>
屬性解釋:
- fromXDelta/fromYDelta:動畫起始位置的X/Y坐標(biāo)
- toXDelta/toYDelta:動畫結(jié)束位置的X/Y坐標(biāo)
4)RotateAnimation(旋轉(zhuǎn)漸變)
anim_rotate.xml:
<rotate xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromDegrees="0" android:toDegrees="360" android:duration="1000" android:repeatCount="1" android:repeatMode="reverse"/>
屬性解釋:
- fromDegrees/toDegrees:旋轉(zhuǎn)的起始/結(jié)束角度
- repeatCount:旋轉(zhuǎn)的次數(shù),默認(rèn)值為0,代表一次,假如是其他值,比如3,則旋轉(zhuǎn)4次 另外,值為-1或者infinite時,表示動畫永不停止
- repeatMode:設(shè)置重復(fù)模式,默認(rèn)restart,但只有當(dāng)repeatCount大于0或者infinite或-1時 才有效。還可以設(shè)置成reverse,表示偶數(shù)次顯示動畫時會做方向相反的運(yùn)動!
5)AnimationSet(組合漸變)
非常簡單,就是前面幾個動畫組合到一起而已~
anim_set.xml:
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/decelerate_interpolator" android:shareInterpolator="true" > <scale android:duration="2000" android:fromXScale="0.2" android:fromYScale="0.2" android:pivotX="50%" android:pivotY="50%" android:toXScale="1.5" android:toYScale="1.5" /> <rotate android:duration="1000" android:fromDegrees="0" android:repeatCount="1" android:repeatMode="reverse" android:toDegrees="360" /> <translate android:duration="2000" android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="320" android:toYDelta="0" /> <alpha android:duration="2000" android:fromAlpha="1.0" android:toAlpha="0.1" /> </set>
3.寫個例子來體驗(yàn)下
好的,下面我們就用上面寫的動畫來寫一個例子,讓我們體會體會何為補(bǔ)間動畫: 首先來個簡單的布局:activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/btn_alpha" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="透明度漸變" /> <Button android:id="@+id/btn_scale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="縮放漸變" /> <Button android:id="@+id/btn_tran" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="位移漸變" /> <Button android:id="@+id/btn_rotate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="旋轉(zhuǎn)漸變" /> <Button android:id="@+id/btn_set" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="組合漸變" /> <ImageView android:id="@+id/img_show" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="48dp" android:src="@mipmap/img_face" /> </LinearLayout>
好噠,接著到我們的MainActivity.java,同樣非常簡單,只需調(diào)用AnimationUtils.loadAnimation() 加載動畫,然后我們的View控件調(diào)用startAnimation開啟動畫即可~
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Button btn_alpha; private Button btn_scale; private Button btn_tran; private Button btn_rotate; private Button btn_set; private ImageView img_show; private Animation animation = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bindViews(); } private void bindViews() { btn_alpha = (Button) findViewById(R.id.btn_alpha); btn_scale = (Button) findViewById(R.id.btn_scale); btn_tran = (Button) findViewById(R.id.btn_tran); btn_rotate = (Button) findViewById(R.id.btn_rotate); btn_set = (Button) findViewById(R.id.btn_set); img_show = (ImageView) findViewById(R.id.img_show); btn_alpha.setOnClickListener(this); btn_scale.setOnClickListener(this); btn_tran.setOnClickListener(this); btn_rotate.setOnClickListener(this); btn_set.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.btn_alpha: animation = AnimationUtils.loadAnimation(this, R.anim.anim_alpha); img_show.startAnimation(animation); break; case R.id.btn_scale: animation = AnimationUtils.loadAnimation(this, R.anim.anim_scale); img_show.startAnimation(animation); break; case R.id.btn_tran: animation = AnimationUtils.loadAnimation(this, R.anim.anim_translate); img_show.startAnimation(animation); break; case R.id.btn_rotate: animation = AnimationUtils.loadAnimation(this, R.anim.anim_rotate); img_show.startAnimation(animation); break; case R.id.btn_set: animation = AnimationUtils.loadAnimation(this, R.anim.anim_set); img_show.startAnimation(animation); break; } } }
運(yùn)行效果圖:
嘿嘿,有點(diǎn)意思是吧,還不動手試試,改點(diǎn)東西,或者自由組合動畫,做出酷炫的效果吧~
4.動畫狀態(tài)的監(jiān)聽
我們可以對動畫的執(zhí)行狀態(tài)進(jìn)行監(jiān)聽,調(diào)用動畫對象的:
- setAnimationListener(new AnimationListener())方法,重寫下面的三個方法:
- onAnimationStart():動畫開始
- onAnimtaionRepeat():動畫重復(fù)
- onAnimationEnd():動畫結(jié)束
即可完成動畫執(zhí)行狀態(tài)的監(jiān)聽~
5.為View動態(tài)設(shè)置動畫效果
先調(diào)用AnimationUtils.loadAnimation(動畫xml文件),然后View控件調(diào)用startAnimation(anim) 開始動畫~這是靜態(tài)加載的方式,當(dāng)然你也可以直接創(chuàng)建一個動畫對象,用Java代碼完成設(shè)置,再調(diào)用 startAnimation開啟動畫~
6.為Fragment設(shè)置過渡動畫
這里要注意一點(diǎn),就是Fragment是使用的v4包還是app包下的Fragment! 我們可以調(diào)用FragmentTransaction對象的setTransition(int transit)為Fragment指定標(biāo)準(zhǔn)的過場動畫,transit的可選值如下:
- TRANSIT_NONE:無動畫
- TRANSIT_FRAGMENT_OPEN:打開形式的動畫
- TRANSIT_FRAGMENT_CLOSE:關(guān)閉形式的動畫
上面的標(biāo)準(zhǔn)過程動畫是兩個都可以調(diào)用的,而不同的地方則在于自定義轉(zhuǎn)場動畫
setCustomAnimations()方法!
app包下的Fragment:setCustomAnimations(int enter, int exit, int popEnter, int popExit)分別是添加,移除,入棧,以及出棧時的動畫! 另外要注意一點(diǎn)的是,對應(yīng)的動畫類型是:屬性動畫(Property),就是動畫文件 的根標(biāo)簽要是:<objectAnimator>,<valueAnimator>或者是前面兩者放到一個<set>里;
v4包下的Fragment: v4包下的則支持兩種setCustomAnimations()
另外要注意一點(diǎn)的是,對應(yīng)的動畫類型是:補(bǔ)間動畫(Tween),和上面的View一樣~
可能你會有疑惑,你怎么知道對應(yīng)的動畫類型,其實(shí)只要你到Fragment源碼那里找下:
onCreateAnimation()方法的一個返回值就知道了:
v4包:
app包:
7.為Activity設(shè)置過場動畫
Activty設(shè)置過場動畫非常簡單,調(diào)用的方法是:overridePendingTransition(int enterAnim, int exitAnim)
用法很簡單:在startActivity(intent)或者finish()后添加
參數(shù)依次是:新Activity進(jìn)場時的動畫,以及舊Activity退場時的動畫
下面提供幾種比較簡單而且常用的過場動畫供大家使用~
下載傳送門:Activity常用過渡動畫.zip
8.寫個進(jìn)入APP后登陸注冊按鈕從底部彈出動畫效果的例子:
運(yùn)行效果圖:
代碼實(shí)現(xiàn):
首先是我們的布局文件:activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#DDE2E3" tools:context=".MainActivity"> <LinearLayout android:id="@+id/start_ctrl" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="vertical" android:visibility="gone"> <Button android:id="@+id/start_login" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#F26968" android:gravity="center" android:paddingBottom="15dp" android:paddingTop="15dp" android:text="登陸" android:textColor="#FFFFFF" android:textSize="18sp" /> <Button android:id="@+id/start_register" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#323339" android:gravity="center" android:paddingBottom="15dp" android:paddingTop="15dp" android:text="注冊" android:textColor="#FFFFFF" android:textSize="18sp" /> </LinearLayout></RelativeLayout>
接著是MainActivity.java:
public class MainActivity extends AppCompatActivity { private LinearLayout start_ctrl; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); start_ctrl = (LinearLayout) findViewById(R.id.start_ctrl); //設(shè)置動畫,從自身位置的最下端向上滑動了自身的高度,持續(xù)時間為500ms final TranslateAnimation ctrlAnimation = new TranslateAnimation( TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 1, TranslateAnimation.RELATIVE_TO_SELF, 0); ctrlAnimation.setDuration(500l); //設(shè)置動畫的過渡時間 start_ctrl.postDelayed(new Runnable() { @Override public void run() { start_ctrl.setVisibility(View.VISIBLE); start_ctrl.startAnimation(ctrlAnimation); } }, 2000); } }
注釋寫得很清楚了,這里就不BB解釋了,如果你對TranslateAnimation.RELATIVE_TO_SELF這個有疑惑,
請自己谷歌或者百度,限于篇幅(我懶),這里就不寫了,蠻簡單的~
9.本節(jié)代碼示例下載
本節(jié)小結(jié):
本節(jié)給大家細(xì)細(xì)地講解了下Android中的第二種動畫(漸變動畫),四種動畫的詳解,以及 設(shè)置動畫監(jiān)聽器,還有如何為View,F(xiàn)ragment和Activity設(shè)置動畫,最后還寫了一個進(jìn)入后 從APP底部彈出登陸按鈕和注冊按鈕的例子,篇幅可能有點(diǎn)長,不過都非常容易理解,相信 大家看完都能夠收獲滿滿~!好的,本節(jié)就到這里,謝謝~