Android动画(一)-视图动画与帧动画
項目中好久沒用過動畫了,所以關于動畫的知識都忘光了。知識總是不用則忘。正好最近的版本要添加比較炫酷的動畫效果,所以也借著這個機會,寫博客來整理和總結關于動畫的一些知識。也方便自己今后的查閱。
Android中的動畫分為三類。
- View animation:視圖動畫,也叫做 Tween(補間)動畫。
- Drawable animation:也叫做Frame 動畫,幀動畫。
- Property animation: 屬性動畫。支持Android3.0以上版本。
我們根據類別,來分別介紹。
視圖動畫
視圖動畫是一種比較古老的,使用方式比較簡單的動畫。它可以在一個視圖容器內執行一系列的簡單變換(比如大小,旋轉等)。它控制的是整個view。它支持四種效果。透明度,旋轉,縮放,位移。分別對應著 Animation的四個子類,AlphaAnimation,RotateAnimation,ScaleAnimation和TranslateAnimation。
但是視圖動畫有一點需要特別注意,那就是不具備交互性。什么意思呢?比如 一個Button進行了平移變換,已經從之前的A點,移動到了B點。但是你點擊B點,該Button沒反應,相反你點擊A點,該Button才有反應。(雖然它的視圖已經不顯示在A點了)。所以說 視圖動畫改變的只是View的顯示,卻沒有改變View的真實布局屬性值。
視圖動畫可以通過Xml或Android代碼中定義。使用起來比較方便。
如果在xml文件中使用動畫,文件目錄是res/anim/filename.xml,并且View動畫既可以是單個動畫,也可以由一系列動畫組成:
它具體的使用方式如下:
在代碼中這樣應用。
//@author www.yaoxiaowen.com //本文地址: http://www.cnblogs.com/yaoxiaowen/p/7499556.html Animation myAnim = AnimationUtils.loadAnimation(this, R.anim.filename); myView.startAnimation(myAnim);而如果不借助于xml文件,直接在java代碼中定義,則類似這樣使用:
//旋轉動畫, 旋轉參考系為自身中心點 //@author www.yaoxiaowen.com //本文地址: http://www.cnblogs.com/yaoxiaowen/p/7499556.html RotateAnimation ra = new RotateAnimation(0, 360, RotateAnimation.RELATIVE_TO_SELF, 0.5F,RotateAnimation.RELATIVE_TO_SELF, 0.5F);ra.setDuration(5000); myView.startAnimation(ra);知道了基本用法,那么我們下面要做的,就是熟悉相關api了。。(具體api內容,參考了該篇博客,在此表示感謝)。
Animation是abstract的,它也是AlphaAnimation等視圖動畫的基類。
Animation類相關屬性方法如下:
| android:detachWallpaper | setDetachWallpaper(boolean) | 是否在壁紙上運行 |
| android:duration | setDuration(long) | 動畫持續時間,毫秒為單位 |
| android:fillAfter | setFillAfter(boolean) | 控件動畫結束時是否保持動畫最后的狀態 |
| android:fillBefore | setFillBefore(boolean) | 控件動畫結束時是否還原到開始動畫前的狀態 |
| android:fillEnabled | setFillEnabled(boolean) | 與android:fillBefore效果相同 |
| android:interpolator | setInterpolator(Interpolator) | 設定插值器(指定的動畫效果,譬如回彈等) |
| android:repeatCount | setRepeatCount(int) | 重復次數 |
| android:repeatMode | setRepeatMode(int) | 重復類型有兩個值,reverse表示倒序回放,restart表示從頭播放 |
| android:startOffset | setStartOffset(long) | 調用start函數之后等待開始運行的時間,單位為毫秒 |
| android:zAdjustment | setZAdjustment(int) | 表示被設置動畫的內容運行時在Z軸上的位置(top/bottom/normal),默認為normal |
除此之外,Animation還有一些常用的方法:
| reset() | 重置Animation的初始化 |
| cancel() | 取消Animation動畫 |
| start() | 開始Animation動畫 |
| setAnimationListener(AnimationListener listener) | 給當前Animation設置動畫監聽 |
| hasStarted() | 判斷當前Animation是否開始 |
| hasEnded() | 判斷當前Animation是否結束 |
Alpha相關屬性:
| android:fromAlpha | AlphaAnimation(float fromAlpha, …) | 動畫開始的透明度(0.0到1.0,0.0是全透明,1.0是不透明) |
| android:toAlpha | AlphaAnimation(…, float toAlpha) | 動畫結束的透明度,同上 |
Rotate相關屬性:
| android:fromDegrees | RotateAnimation(float fromDegrees, …) | 旋轉開始角度,正代表順時針度數,負代表逆時針度數 |
| android:toDegrees | RotateAnimation(…, float toDegrees, …) | 旋轉結束角度,正代表順時針度數,負代表逆時針度數 |
| android:pivotX | RotateAnimation(…, float pivotX, …) | 縮放起點X坐標(數值、百分數、百分數p,譬如50表示以當前View左上角坐標加50px為初始點、50%表示以當前View的左上角加上當前View寬高的50%做為初始點、50%p表示以當前View的左上角加上父控件寬高的50%做為初始點) |
| android:pivotY | RotateAnimation(…, float pivotY) | 縮放起點Y坐標,同上規律 |
Scale相關屬性:
| android:fromXScale | ScaleAnimation(float fromX, …) | 初始X軸縮放比例,1.0表示無變化 |
| android:toXScale | ScaleAnimation(…, float toX, …) | 結束X軸縮放比例 |
| android:fromYScale | ScaleAnimation(…, float fromY, …) | 初始Y軸縮放比例 |
| android:toYScale | ScaleAnimation(…, float toY, …) | 結束Y軸縮放比例 |
| android:pivotX | ScaleAnimation(…, float pivotX, …) | 縮放起點X軸坐標(數值、百分數、百分數p,譬如50表示以當前View左上角坐標加50px為初始點、50%表示以當前View的左上角加上當前View寬高的50%做為初始點、50%p表示以當前View的左上角加上父控件寬高的50%做為初始點) |
| android:pivotY | ScaleAnimation(…, float pivotY) | 縮放起點Y軸坐標,同上規律 |
Translate相關屬性:
| android:fromXDelta | TranslateAnimation(float fromXDelta, …) | 起始點X軸坐標(數值、百分數、百分數p,譬如50表示以當前View左上角坐標加50px為初始點、50%表示以當前View的左上角加上當前View寬高的50%做為初始點、50%p表示以當前View的左上角加上父控件寬高的50%做為初始點) |
| android:fromYDelta | TranslateAnimation(…, float fromYDelta, …) | 起始點Y軸從標,同上規律 |
| android:toXDelta | TranslateAnimation(…, float toXDelta, …) | 結束點X軸坐標,同上規律 |
| android:toYDelta | TranslateAnimation(…, float toYDelta) | 結束點Y軸坐標,同上規律 |
另外還有一個AnimationSet,它代表的是一系列動畫的組合。
在代碼當中我們可以這樣使用:
但是值得注意的是,如果我們對AnimationSet設置了一些屬性,那么有些屬性會影響到它所包含的子控件,而有些則不會。API文檔上是這樣解釋的。
The way that AnimationSet inherits behavior from Animation is important to understand. Some of the Animation attributes applied to AnimationSet affect the AnimationSet itself, some are pushed down to the children, and some are ignored, as follows:
- duration, repeatMode, fillBefore, fillAfter: These properties, when set on an AnimationSet object, will be pushed down to all child animations.
- repeatCount, fillEnabled: These properties are ignored for AnimationSet.
- startOffset, shareInterpolator: These properties apply to the AnimationSet itself.
視圖動畫是供View使用的,而View基類中和動畫相關的常用方法如下:
| startAnimation(Animation animation) | 對當前View開始設置的Animation動畫 |
| clearAnimation() | 取消當View在執行的Animation動畫 |
幀動畫
幀動畫是一種比較簡單的動畫,利用多張圖片就像放電影那樣輪流播放,然后就形成了動畫效果?;蛘哒f像播放幻燈片也是一個道理。因為它實質上就是多張圖,所以它也叫作 Drawable動畫。系統提供了 AnimationDrawable類來使用 幀動畫。該類和 Animation沒有繼承關系。
Goole官方demo給出的使用方式如下:
<!-- Animation frames are wheel0.png through wheel5.pngfiles inside the res/drawable/ folder --><animation-list android:id="@+id/selected" android:oneshot="false"><item android:drawable="@drawable/wheel0" android:duration="50" /><item android:drawable="@drawable/wheel1" android:duration="50" /><item android:drawable="@drawable/wheel2" android:duration="50" /><item android:drawable="@drawable/wheel3" android:duration="50" /><item android:drawable="@drawable/wheel4" android:duration="50" /><item android:drawable="@drawable/wheel5" android:duration="50" /></animation-list>在java代碼中加載使用該動畫。
// Load the ImageView that will host the animation and// set its background to our AnimationDrawable XML resource.ImageView img = (ImageView)findViewById(R.id.spinning_wheel_image);img.setBackgroundResource(R.drawable.spin_animation);// Get the background, which has been compiled to an AnimationDrawable object.AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();// Start the animation (looped playback by default).frameAnimation.start();對于幀動畫,注意以下幾點就ok了。
- android:oneshot屬性:true表示動畫只播放一次,然后停止在最后一幀上,false表示動畫循環播放。
item代表每一幀的圖片, android:drawable表示具體圖片引用,android:duration表示每一幀停留的時間。
- AnimationDrawable#start()方法不可以在Activity#onCreate()方法中調用,這和AnimationDrawable還未完全附著到window上有關,因此最好的調用時機是在Activity#onWindowFocusChanged()方法中。
Activity或fragment的切換動畫
跳轉Activity時,系統有默認的切換效果,不過我們也可以自定義,只要直接調用Activity中的一個方法即可,
/*** @author www.yaoxiaowen.com* @param enterAnim 進入Activity時,所需要的動畫資源id, 傳遞 0 代表 不使用動畫效果* @param exitAnim 離開Activity時,所需要的動畫資源id, 傳遞 0 代表 不使用動畫效果*/ public void overridePendingTransition (int enterAnim, int exitAnim)但是該方法必須要緊跟著startActivity(Intent)或finish()方法后面立即被調用,否則沒效果。
類似下面這樣
而Fragment也可以添加切換動畫。則要使用FragmentTransaction中的這個方法。
FragmentTransaction setCustomAnimations (int enter, int exit)不過要注意,Activity或fragment添加的動畫都應該是 視圖動畫,而不是屬性動畫。
作者: www.yaoxiaowen.com
博客地址: www.cnblogs.com/yaoxiaowen/
github: https://github.com/yaowen369
歡迎對于本人的博客內容批評指點,如果問題,可評論或郵件(yaowen369@gmail.com)聯系
歡迎轉載,轉載請注明出處.謝謝
總結
以上是生活随笔為你收集整理的Android动画(一)-视图动画与帧动画的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: express 源码阅读(全)
- 下一篇: TSC的分歧导致Node.js分支