1. 程式人生 > >Android動畫筆記之Animation

Android動畫筆記之Animation

簡介

Android提供了兩種機制,您可以使用它們來建立簡單的動畫:補間動畫,您可以在其中告訴Android對檢視內容執行一系列簡單的轉換(位置,大小,旋轉等); 和逐幀動畫,一個接一個地載入一系列Drawable資源。 兩種動畫型別都可以在任何View物件中使用,以提供簡單的旋轉計時器,活動圖示和其他有用的UI元素。 Tweened動畫由此包處理(android.view.animation); 逐幀動畫由AnimationDrawable類處理。

使用

amin資料夾下的屬性

在xml屬性有哪些以及他們的用法

屬性 意圖(作用)
alpha 透明度漸變效果
scale 尺寸伸縮效果
translate 移動效果
rotate 旋轉效果
set 全部的集合
  • 注:在res目錄下新建一個資料夾命名為anim,然後建立xml檔案。

載入使用方式

 Animation animation = AnimationUtils.loadAnimation(this, R.amin.demo);
 button.startAnimation(animation );

插補器Interpolator認識

插補器InterpolatorBaseInterpolator.class是由預設插值器擴充套件的抽象類。

插補器子類(.class) xml屬性引用 意圖(效果)
AccelerateDecelerateInterpolator @android:anim/accelerate_decelerate_interpolator 變化率開始和結束緩慢但在中間加速
AccelerateInterpolator @android:anim/accelerate_interpolator 變化率開始緩慢而然後加速
AnticipateInterpolator @android:anim/anticipate_interpolator 變化開始向後然後向前飛行
AnticipateOvershootInterpolator @android:anim/anticipate_overshoot_interpolator 變化開始向後然後向前飛過並超過目標值,最後返回到最終值
BounceInterpolator @android:anim/bounce_interpolator 變化在結束時反彈
CycleInterpolator @android:anim/cycle_interpolator 重複動畫指定的週期數,變化率遵循正弦曲線模式
DecelerateInterpolator @android:anim/decelerate_interpolator 變化率快速開始,然後減速
LinearInterpolator @android:anim/linea_interpolator 變化是恆定的方式改變
OvershootInterpolator @android:anim/overshoot_interpolator 變化向前晃動並超過最後一個值然後返回
PathInterpolator @android:anim/path_interpolator 根據路徑的設定來改變

程式碼自定義建立動畫

Animation.class類分析 可應用於檢視,曲面或其他物件的動畫的抽象基類。它的子類原始碼結構如下:

動畫子類(.class) 解釋說明 構造器
ScaleAnimation 控制物件比例的動畫。您可以指定用於縮放中心的點 ScaleAnimation(Context context, AttributeSet attrs)從資源載入ScaleAnimation時使用的構造方法
下面的都是從程式碼構建ScaleAnimation時使用的構造器:ScaleAnimation(float fromX, float toX, float fromY, float toY)
ScaleAnimation(float fromX, float toX, float fromY, float toY,float pivotX, float pivotY)
ScaleAnimation(float fromX, float toX, float fromY, float toY,int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
AlphaAnimation 控制物件alpha級別的動畫,用於淡入淡出效果 AlphaAnimation(Context context, AttributeSet attrs)從資源載入AlphaAnimation時使用的構造方法。
下面的都是從程式碼構建AlphaAnimation時使用的構造器:
AlphaAnimation(float fromAlpha, float toAlpha)
RotateAnimation 控制物件旋轉的動畫,該旋轉在X-Y平面中佔據位置,可以指定用於旋轉中心的點,如果未指定,則(0,0)預設旋轉點 RotateAnimation(Context context, AttributeSet attrs)從資源載入RotateAnimation時使用的構造方法
RotateAnimation(float fromDegrees, float toDegrees)從程式碼構建RotateAnimation時使用的構造方法,預設的pivotX / pivotY點是(0,0)
RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY); pivotX/pivotY:物件旋轉的點的X/Y座標
RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue,int pivotYType, float pivotYValue)
pivotYType/pivotYValue指定Animation型別如:Animation.RELATIVE_TO_SELF
TranslateAnimation 控制物件位置的動畫,移動效果 TranslateAnimation(Context context, AttributeSet attrs):從資源載入TranslateAnimation時使用的構造方法
TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)從程式碼構建TranslateAnimation時使用的構造方法
TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue,int fromYType, float fromYValue, int toYType, float toYValue)
AnimationSet 表示應該一起播放的一組動畫(全部動畫的集合) AnimationSet(Context context, AttributeSet attrs)
AnimationSet(boolean shareInterpolator)從程式碼中使用建構函式,如果此設定中的所有動畫都應使用與此AnimationSet關聯的插值器,則傳遞true,如果每個動畫都應使用自己的插值器,則傳遞false。

總結

本篇主要簡單記錄Animation相關動畫資訊。