Android動畫系列之幀動畫和補間動畫的示例代碼
Android 提供三種動畫:幀動畫、補間動畫和屬性動畫,本篇文章介紹幀動畫以及補間動畫的使用,屬性動畫的使用將在后面的文章中分享,那就來復(fù)習(xí)一下這兩種動畫的使用吧。
FrameAnimation
FrameAnimation 即逐幀動畫,通俗來說就是按照圖片動作順序依次播放來形成動畫,創(chuàng)建 FrameAnimation 可用 xml 定義也可直接使用代碼創(chuàng)建。
xml創(chuàng)建幀動畫
在 res/drawable 文件夾下創(chuàng)建一個 drawable 文件,使用 animation-list 標(biāo)簽,具體內(nèi)容如下:
<?xml version="1.0" encoding="utf-8"?> <!--FrameAnimator--> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@drawable/zzlx1" android:duration="100" /> <item android:drawable="@drawable/zzlx2" android:duration="100" /> <item android:drawable="@drawable/zzlx3" android:duration="100" /> <!--...--> </animation-list>
屬性 oneshot 為 true 表示動畫只能播放一次,false 表示動畫循環(huán)播放,drawable 是當(dāng)前動作對應(yīng)的圖片,duration 是其持續(xù)時間,duration 長度影響動畫播放的快慢,然后在 Activity 中使用獲取該 drawable 文件對應(yīng)的 AnimationDrawable,然后使用 AnimationDrawable 對象來控制動畫的狀態(tài),參考如下:
//獲取Frame動畫文件對應(yīng)的AnimationDrawable mAnimationDrawable = (AnimationDrawable) getResources().getDrawable(R.drawable.frame_animator); //設(shè)置AnimationDrawable為圖片的背景 imageView.setBackground(mAnimationDrawable); //開啟動畫 mAnimationDrawable.start(); //停止動畫 mAnimationDrawable.stop();
代碼創(chuàng)建幀動畫
使用代碼創(chuàng)建幀動畫就是創(chuàng)建 AnimationDrawable 對象,然后在 AnimationDrawable 中添加對應(yīng)的 Frame 即可,代碼參考如下:
//代碼創(chuàng)建Frame動畫 mAnimationDrawable = new AnimationDrawable(); //設(shè)置動畫循環(huán)播放,true為動畫只播放一次 mAnimationDrawable.setOneShot(false); mAnimationDrawable.addFrame(getResources().getDrawable(R.drawable.zzlx1),100); mAnimationDrawable.addFrame(getResources().getDrawable(R.drawable.zzlx2),100); mAnimationDrawable.addFrame(getResources().getDrawable(R.drawable.zzlx3),100); //... imageView.setBackground(mAnimationDrawable); //開啟動畫 mAnimationDrawable.start(); //停止動畫 mAnimationDrawable.stop();
FrameAnimation 效果如下:
TweenAnimation
TweenAnimation 即常說的補間動畫,主要有以下幾種:
- 位移動畫(Translation)
- 縮放動畫(Scale)
- 旋轉(zhuǎn)動畫(Rotate)
- 透明度動畫(Alpha)
- 組合動畫
上述動畫都有自己特有的一下屬性,下面來看一看這些動畫通用的一些屬性,具體如下:
<!--設(shè)置動畫持續(xù)時間--> android:duration="1200" <!--動畫開始的延時--> android:startOffset ="1000" <!--動畫播放完是否回到動畫開始的位置,默認(rèn)true,如果fillBefore設(shè)置為false,動畫不會停留在結(jié)束位置,不知道是不是bug--> android:fillBefore = "true" <!--動畫播放完之后是否回到動畫結(jié)束的位置,默認(rèn)false,如果fillAfter設(shè)置為true,動畫則會停留在結(jié)束位置--> android:fillAfter = "false" <!--設(shè)置fill...屬性是否啟用,對fillAfter無效--> android:fillEnabled= "true" <!--設(shè)置動畫重復(fù)模式,restart為重新播放,reverse為倒序回放,和repeatCount搭配使用--> android:repeatMode = "restart" <!--設(shè)置動畫重復(fù)次數(shù)--> android:repeatCount = "0" <!--設(shè)置動畫插值器,這里的插值器是動畫開始速度較慢,后面加速--> android:interpolator = "@android:anim/accelerate_interpolator"
如果在代碼中進行對應(yīng)動畫實現(xiàn),這些屬性也有對應(yīng)的屬性設(shè)置,直接設(shè)置即可。
位移動畫(Translate)
位移動畫對 View 進行水平方向或垂直方向位置的平移,可指定起始位置和結(jié)束位置,可使用 xml 定義位移動畫也可以使用代碼創(chuàng)建位移動畫,位移動畫對應(yīng)的 Animation 的子類是 TranslateAnimation。
xml定義位移動畫:在 res/anim 下創(chuàng)建一個xml文件 translation_anim.xml,在該文件中定義位移動畫如下:
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1200" android:startOffset ="0" android:fillBefore = "true" android:fillAfter = "false" android:fillEnabled= "false" android:repeatMode = "reverse" android:repeatCount = "5" android:interpolator = "@android:anim/accelerate_interpolator" android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="100" android:toYDelta="100">
上述 xml 文件定義了一個位移動畫文件,其中位移動畫自有的屬性含義如下:
<!--水平方向動畫開始的位置--> android:fromXDelta="0" <!--垂直方向動畫開始的位置--> android:fromYDelta="0" <!--水平方向動畫結(jié)束的位置--> android:toXDelta="100" <!--垂直方向動畫結(jié)束的位置--> android:toYDelta="100"
然后在 Activity 中獲取該 xml 文件對應(yīng)的 TranslateAnimation,將其設(shè)置到想要設(shè)置位移動畫的 View 上即可,具體如下:
private void translation(){ //獲取在anim下定義的動畫文件 TranslateAnimation translateAnimation = (TranslateAnimation) AnimationUtils.loadAnimation(this, R.anim.translation_anim);、 //設(shè)置并開啟動畫 ivImage.startAnimation(translateAnimation); }
代碼中創(chuàng)建位移動畫:代碼創(chuàng)建位移動畫使用 Animation 的子類 TranslateAnimation,使用時直接創(chuàng)建 TranslateAnimation 對象即可,具體如下:
//代碼創(chuàng)建位移動畫 private void translation(){ //表示相對View自身原點(View左上角)像素偏移量 TranslateAnimation translateAnimation = new TranslateAnimation(0,100,0,100); //設(shè)置動畫持續(xù)時間 translateAnimation.setDuration(1200); //設(shè)置動畫重復(fù)模式 translateAnimation.setRepeatMode(Animation.REVERSE); //設(shè)置動畫重復(fù)次數(shù) translateAnimation.setRepeatCount(3); translateAnimation.setFillAfter(true); //設(shè)置動畫插值器 translateAnimation.setInterpolator(this,android.R.anim.accelerate_interpolator); // translateAnimation.setInterpolator(new AccelerateInterpolator()); //... ivImage.startAnimation(translateAnimation); }
上面參數(shù)中使用的時像素的偏移量,API 還提供了針對 View 自身一個父 View 的百分比的設(shè)置方式,下面這種創(chuàng)建 TranslateAnimation 對象的方式和上面實現(xiàn)的效果是一樣的。具體如下:
/** * ABSOLUTE:表示相對View自身原點(View左上角)像素偏移量 * 此時和TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)一樣 * RELATIVE_TO_SELF:表示相對View自身的百分比,如0.5f表示View自身大小的50%,1.0f表示View自身大小 * RELATIVE_TO_PARENT:表示相對父View的百分比,如0.5f表示View自身大小的50%,1.0f表示View自身大小 */ TranslateAnimation translateAnimation = new TranslateAnimation( Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0.46f, Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0.46f);
使用時可根據(jù)需要選擇合適的構(gòu)造方式創(chuàng)建 TranslateAnimation,測試效果如下:
縮放動畫(Scale)
縮放動畫對 View 就是對視圖進行一定程度的放大和縮小,可使用 xml 定義位移動畫也可以使用代碼創(chuàng)建位移動畫,縮放動畫對應(yīng)的 Animation 的子類是 ScaleAnimation。
xml定義縮放動畫:在 res/anim 下創(chuàng)建一個 xml 文件 scale_anim.xml,在里面定義縮放動畫,具體如下:
<?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1200" android:startOffset ="0" android:fillBefore = "true" android:fillAfter = "false" android:fillEnabled= "false" android:repeatMode = "reverse" android:repeatCount = "3" android:interpolator = "@android:anim/accelerate_interpolator" android:fromXScale="1" android:fromYScale="1" android:toXScale="3" android:toYScale="3" android:pivotX="50%" android:pivotY="50%"> </scale>
上述 xml 文件定義了一個縮放動畫文件,其中縮放動畫自有的屬性含義如下:
<!--設(shè)置水平方向上的起始縮放倍數(shù)--> android:fromXScale="1" <!--設(shè)置垂直方向上的起始縮放倍數(shù)--> android:fromYScale="1" <!--設(shè)置水平方向上的結(jié)束縮放倍數(shù)--> android:toXScale="3" <!--設(shè)置垂直方向上的結(jié)束縮放倍數(shù)--> android:toYScale="3" <!--設(shè)置縮放中心水平方向上的坐標(biāo)--> android:pivotX="50%" <!--設(shè)置縮放中心垂直方向上的坐標(biāo)--> android:pivotY="50%">
其中 pivotX 和 pivotY 有三種設(shè)置方式:
- 數(shù)字:如50表示縮放中心相較 View 原點偏移 50px
- 百分比:如 50% 表示縮放中心相較 View 原點偏移 View 自身大小的 50%
- 百分比p:如 50%p 表示縮放中心相較 View 原點偏移父 View 自身大小的 50%
然后在 Activity 中獲取該 xml 文件對應(yīng)的 ScaleAnimation,將其設(shè)置到想要設(shè)置位移動畫的 View 上即可,具體如下:
private void scale(){ ScaleAnimation scaleAnimation = (ScaleAnimation) AnimationUtils.loadAnimation(this,R.anim.scale_anim); ivImage.startAnimation(scaleAnimation); }
代碼創(chuàng)建縮放動畫:代碼創(chuàng)建縮放動畫使用 Animation 的子類 ScaleAnimation,使用時直接創(chuàng)建 ScaleAnimation 對象即可,具體如下:
//代碼創(chuàng)建縮放動畫 private void scale(){ ScaleAnimation scaleAnimation = new ScaleAnimation(1,3,1,3, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f); scaleAnimation.setRepeatMode(Animation.REVERSE); scaleAnimation.setDuration(500); scaleAnimation.setRepeatCount(5); scaleAnimation.setInterpolator(this,android.R.anim.accelerate_decelerate_interpolator); // translateAnimation.setInterpolator(new AccelerateInterpolator()); //... ivImage.startAnimation(scaleAnimation); }
至于參數(shù)中的 pivotXType 和 pivotYType 和在上文中已經(jīng)提到過,這里就不在贅述,測試效果如下:
旋轉(zhuǎn)動畫(Rotate)
旋轉(zhuǎn)動畫對 View 就是對視圖角度進行旋轉(zhuǎn),可使用 xml 定義旋轉(zhuǎn)動畫也可以使用代碼創(chuàng)建旋轉(zhuǎn)動畫,旋轉(zhuǎn)動畫對應(yīng)的 Animation 的子類是 RotateAnimation。
xml定義旋轉(zhuǎn)動畫:在 res/anim 下創(chuàng)建一個 xml 文件 rotate_anim.xml,在里面定義縮放動畫,具體如下:
<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1200" android:startOffset ="0" android:fillBefore = "true" android:fillAfter = "false" android:fillEnabled= "false" android:repeatMode = "reverse" android:repeatCount = "5" android:interpolator = "@android:anim/accelerate_interpolator" android:fromDegrees="0" android:toDegrees="100" android:pivotY="50%" android:pivotX="50%"> </rotate>
上述 xml 文件定義了一個旋轉(zhuǎn)動畫文件,其中縮放動畫自有的屬性含義如下:
<!--設(shè)置動畫開始時的角度,正數(shù)表示順時針,負(fù)數(shù)表示逆時針--> android:fromDegrees="0" <!--設(shè)置動畫結(jié)束時的角度,正數(shù)表示順時針,負(fù)數(shù)表示逆時針--> android:toDegrees="100" <!--設(shè)置水平方向旋轉(zhuǎn)中心點的坐標(biāo)--> android:pivotY="50%" <!--設(shè)置垂直方向旋轉(zhuǎn)中心點的坐標(biāo)--> android:pivotX="50%"
其中 pivotX 和 pivotY 有三種設(shè)置方式在上文中已經(jīng)說明。然后在 Activity 中獲取該 xml 文件對應(yīng)的 RotateAnimation,將其設(shè)置到想要設(shè)置旋轉(zhuǎn)動畫的 View 上即可,具體如下:
private void rotate(){ RotateAnimation rotateAnimation = (RotateAnimation) AnimationUtils.loadAnimation(this,R.anim.rotate_anim); ivImage.startAnimation(rotateAnimation); }
代碼創(chuàng)建旋轉(zhuǎn)動畫:代碼創(chuàng)建旋轉(zhuǎn)動畫使用 Animation 的子類 RotateAnimation,使用時直接創(chuàng)建 RotateAnimation 對象即可,具體如下:
//代碼創(chuàng)建旋轉(zhuǎn)動畫 private void rotate(){ RotateAnimation rotateAnimation = new RotateAnimation(0,100, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f); rotateAnimation.setRepeatMode(Animation.REVERSE); rotateAnimation.setDuration(1200); rotateAnimation.setRepeatCount(3); rotateAnimation.setInterpolator(this,android.R.anim.accelerate_decelerate_interpolator); // translateAnimation.setInterpolator(new AccelerateInterpolator()); //... ivImage.startAnimation(rotateAnimation); }
測試效果如下:
透明度動畫(Alpha)
透明度動畫就是修改 View 的透明度,可使用 xml 定義透明度動畫也可以使用代碼創(chuàng)建透明度動畫,透明度動畫對應(yīng)的 Animation 的子類是 AlphaAnimation。
xml定義透明度動畫:在 res/anim 下創(chuàng)建一個 xml 文件 alpha_anim.xml,在里面定義縮放動畫,具體如下:
<?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:duration="3000" android:startOffset ="0" android:fillBefore = "true" android:fillAfter = "true" android:fillEnabled= "false" android:repeatMode = "restart" android:repeatCount = "0" android:interpolator = "@android:anim/accelerate_interpolator" android:fromAlpha="1" android:toAlpha="0"> </alpha>
上述 xml 文件定義了一個透明度動畫文件,其中透明度動畫自有的屬性含義如下:
<!--設(shè)置動畫的開始透明度,0表示透明,1表示不透明--> android:fromAlpha="1" <!--設(shè)置動畫的結(jié)束透明度,0表示透明,1表示不透明--> android:toAlpha="0"
然后在 Activity 中獲取該 xml 文件對應(yīng)的 AlphaAnimation,將其設(shè)置到想要設(shè)置旋轉(zhuǎn)動畫的 View 上即可,具體如下:
private void alpha(){ AlphaAnimation alphaAnimation = (AlphaAnimation) AnimationUtils.loadAnimation(this,R.anim.alpha_anim); ivImage.startAnimation(alphaAnimation); }
代碼創(chuàng)建透明度動畫:代碼創(chuàng)建透明度動畫使用 Animation 的子類 AlphaAnimation,使用時直接創(chuàng)建 AlphaAnimation 對象即可,具體如下:
//代碼創(chuàng)建透明度動畫 private void alpha(){ AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f,0.0f); alphaAnimation.setRepeatMode(Animation.RESTART); alphaAnimation.setDuration(1500); alphaAnimation.setRepeatCount(3); // alphaAnimation.setInterpolator(this,android.R.anim.accelerate_decelerate_interpolator); // translateAnimation.setInterpolator(new AccelerateInterpolator()); //... ivImage.startAnimation(alphaAnimation); }
透明度動畫測試效果如下:
到此為止,位移、縮放、旋轉(zhuǎn)、透明度動畫的內(nèi)容介紹完了,除了單獨使用這些動畫,還可以組合這些動畫實現(xiàn)更復(fù)雜的動畫,
組合動畫
組合動畫使用 AnimationSet 來實現(xiàn),可使用 xml 定義組合動畫也可以使用代碼創(chuàng)建組合動畫,透明度動畫對應(yīng)的 Animation 的子類是 AnimationSet。
xml定義組合動畫:在 res/anim 下創(chuàng)建一個 xml 文件 combine_anim.xml,在里面定義組合動畫,具體如下:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1200"> <!--透明度動畫--> <alpha android:repeatMode="reverse" android:repeatCount="10" android:fromAlpha="1" android:toAlpha="0.5" /> <!--旋轉(zhuǎn)動畫--> <rotate android:repeatMode="reverse" android:repeatCount="10" android:fromDegrees="0" android:pivotX="50%" android:pivotY="50%" android:toDegrees="360" /> <!--縮放動畫--> <scale android:repeatMode="reverse" android:repeatCount="10" android:fromXScale="1" android:fromYScale="1" android:pivotX="50%" android:pivotY="50%" android:toXScale="3" android:toYScale="3" /> </set>
然后在 Activity 中獲取該 xml 文件對應(yīng)的 AnimationSet,將其設(shè)置到想要設(shè)置旋轉(zhuǎn)動畫的 View 上即可,具體如下:
private void combine(){ AnimationSet animationSet = (AnimationSet) AnimationUtils.loadAnimation(this,R.anim.combine_anim); ivImage.startAnimation(animationSet); }
代碼創(chuàng)建組合動畫:代碼創(chuàng)建組合動畫使用 Animation 的子類 AnimationSet,使用時直接創(chuàng)建 AnimationSet 對象,將要組合的動畫按序添加到 AnimationSet 中,具體如下:
//代碼創(chuàng)建組合動畫 private void combine(){ AnimationSet animationSet = new AnimationSet(true); AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f,0.3f); alphaAnimation.setRepeatMode(Animation.REVERSE); alphaAnimation.setRepeatCount(3); RotateAnimation rotateAnimation = new RotateAnimation(0,360, Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF,0.5f); rotateAnimation.setRepeatMode(Animation.REVERSE); rotateAnimation.setRepeatCount(3); ScaleAnimation scaleAnimation = new ScaleAnimation(1,3,1,3, Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF,0.5f); scaleAnimation.setRepeatMode(Animation.REVERSE); scaleAnimation.setRepeatCount(3); animationSet.addAnimation(alphaAnimation); animationSet.addAnimation(rotateAnimation); animationSet.addAnimation(scaleAnimation); animationSet.setDuration(1200); //AnimationSet不支持動畫重復(fù)播放,如果想要組合動畫重復(fù)播放可設(shè)置每個動畫重復(fù)播放即可 // animationSet.setRepeatMode(Animation.REVERSE); // animationSet.setRepeatCount(10); ivImage.startAnimation(animationSet); }
組合動畫測試效果如下:
總結(jié)
這篇文章總結(jié)了 Android 開發(fā)中幀動畫(FrameAnimation)和補間動畫(TweenAnimation)的使用,下一篇將會介紹屬性動畫(ObjectAnimator )。
到此這篇關(guān)于Android動畫系列之幀動畫和補間動畫的示例代碼的文章就介紹到這了,更多相關(guān)Android幀動畫和補間動畫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android 判斷某個Activity 是否在前臺運行的實例
下面小編就為大家分享一篇Android 判斷某個Activity 是否在前臺運行的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03Android使用gradle讀取并保存數(shù)據(jù)到BuildConfg流程詳解
這篇文章主要介紹了Android使用gradle從資源目錄讀取數(shù)據(jù)并存到BuildConfg內(nèi),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-02-02Android自定義Dialog實現(xiàn)通用圓角對話框
這篇文章主要為大家詳細介紹了Android自定義Dialog實現(xiàn)通用圓角對話框,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-11-11Android自定義TitleView標(biāo)題開發(fā)實例
這篇文章主要介紹了Android自定義TitleView標(biāo)題開發(fā)實例的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09Android仿QQ復(fù)制昵稱效果的實現(xiàn)方法
這篇文章主要介紹了Android仿QQ復(fù)制昵稱效果的實現(xiàn)方法,主要依賴的是一個開源項目,需要的朋友可以參考下2019-05-05Android自定義View實現(xiàn)餅狀圖帶動畫效果
這篇文章主要為大家詳細介紹了Android自定義View實現(xiàn)餅狀圖帶動畫效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12Android 使用FragmentTabHost實現(xiàn)底部菜單功能
這篇文章主要介紹了Android 使用FragmentTabHost實現(xiàn)底部菜單功能,詳細給大家介紹了FragmentTabHost配合Fragment的使用方法,需要的朋友可以參考下2017-12-12