android 動畫分類,android分類
Android 平台提供了一套完整的動畫架構,在Android3.0之前有兩種動畫Tween Animation(補間動畫)和Frame Animation(幀動畫),
對應SDK中的View Animation和Drawable Animation。
在Android3.0之後,新增了一種動畫Property Animation(屬性動畫)。
一: 補間動畫(res/anim/ )
Tween Animation可以對view實現一系列的轉換,給出兩個主要畫面格,通過一些演算法將給定屬性值在給定的時間內在兩個主要畫面格間漸層。可以通過 代碼 和 xml 定義,代碼中定義用: AlphaAnimation…
|
特點:
Tween Animation只能應用於View對象,並且只支援一部分屬性。它並不會改變屬性的值,只是改變了View對象繪製的位置。比如,一個按鈕在動畫過後,不再原來的位置,但是觸發點擊事件的任然是原座標。
xml中必須有一個跟節點,可以是漸層<alpha>, 伸縮<scale>, 移動<translate>, 旋轉<rotate>, 或者 <set>。<set>是一個動畫集,可以包含前面的一個或多個。 |
1 <!-- XML中定義 – --> 2 <set android:shareInterpolator="false"> 3 <scale 4 android:interpolator="@android:anim/accelerate_decelerate_interpolator" 5 android:fromXScale="1.0" 6 android:toXScale="1.4" 7 android:fromYScale="1.0" 8 android:toYScale="0.6" 9 android:pivotX="50%"10 android:pivotY="50%"11 android:fillAfter="false"12 android:duration="700" />13 </set>
代碼中使用:
1 ImageView spaceshipImage = (ImageView) findViewById(R.id.spaceshipImage);2 Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump);3 spaceshipImage.startAnimation(hyperspaceJumpAnimation);
定義動畫的三個步驟 1. 建立動畫對象 2. 設定時間長度 3 . 開啟動畫
Interpolators 插值器
|
可以讓動畫按照一定的頻率運動,實現加速、減速、重複、回彈等效果。
XML 檔案存放在:res/anim 下,並定義android:interpolator="@android:anim/.."來使用 |
二: 幀動畫(res/drawable/)
Frame Animation是一系列的圖片按順序顯示,來類比動畫的效果。 代碼中通過AnimationDrawable來定義。
1 <animation-list xmlns:android="http://schemas.android.com/apk/res/android"2 android:oneshot="true">3 <item android:drawable="@drawable/rocket_thrust1" android:duration="200" />4 <item android:drawable="@drawable/rocket_thrust2" android:duration="200" />5 <item android:drawable="@drawable/rocket_thrust3" android:duration="200" />6 </animation-list>
必須以<animation-list>為根項目,以<item>表示要輪換顯示的圖片,duration屬性工作表示各項顯示的時間。
1 protected void onCreate(Bundle savedInstanceState) { 2 super.onCreate(savedInstanceState); 3 setContentView(R.layout.main); 4 imageView = (ImageView) findViewById(R.id.imageView1); 5 imageView.setBackgroundResource(R.drawable.drawable_anim); 6 anim = (AnimationDrawable) imageView.getBackground(); 7 } 8 9 public boolean onTouchEvent(MotionEvent event) {10 if (event.getAction() == MotionEvent.ACTION_DOWN) {11 anim.stop();12 anim.start();13 return true;14 }15 return super.onTouchEvent(event);16 }
注意事項:
- 要在代碼中調用Imageview的setBackgroundResource方法,如果直接在XML布局檔案中設定其src屬性當觸發動畫時會FC
- 在動畫start()之前要先stop(),不然在第一次動畫之後會停在最後一幀,這樣動畫就只會觸發一次
- 最後一點是SDK中提到的,不要在onCreate中調用start,因為AnimationDrawable還沒有完全跟Window相關聯,如果想要介面顯示時就開始動畫的話,可以在onWindowFoucsChanged()中調用start()。
三 : 屬性動畫(res/animator/)//3.0之後新加的特性
Property Animation更改的是對象的實際屬性,如Button的縮放,Button的位置與大小屬性值都改變了。它不止可以應用於View,還可以應用於almost任何對象,在任何時候(即使沒有draws to the screen)。Property Animation只是表示一個值在一段時間內的改變,當值改變時要做什麼事情完全是你自己決定的。
|
Property Animation允許我們定義的屬性:
- Duration :時間長度 ,default length is 300 ms.
- Time interpolation :屬性值的計算方式,如先快後慢
- Repeat count and behavior:重複次數與方式
- Animator sets :動畫集合,存放幾個動畫,這些動畫可以同時播放也可以設定不同的位移
- Frame refresh delay:多少時間重新整理一次 ,default is 10 ms ,受系統進程調度與硬體的影響
|
①: ValueAnimator :包含Property Animation動畫的所有核心功能,如動畫時間,開始、結束屬性值,相應時間屬性值計算方法等。
應用ValueAnimator有兩個步聚:
第一步 ValuAnimiator 已經完成了,我們只需要實現ValueAnimator.onUpdateListener介面
1 ValueAnimator animation = ValueAnimator.ofFloat(0f, 1f); 2 animation.setDuration(1000); 3 animation.addUpdateListener(new AnimatorUpdateListener() { 4 @Override 5 public void onAnimationUpdate(ValueAnimator animation) { 6 TextView tv = new TextView(getApplication()); 7 tv.setTranslationX((Float) animation.getAnimatedValue()); 8 } 9 });10 animation.setInterpolator(new CycleInterpolator(3));11 animation.start();
②:ObjectAnimator :它是ValueAnimator的子類,它完成了ValueAnimator中 前兩步 操作
為了讓 ObjectAnimator 正確的更新屬性, 我們必須做下面的操作:
- 對象應該有一個setter函數:set<PropertyName>(駝峰命名法),例如, 如果屬性是
foo, 你必須有一個 setFoo() 方法,如果沒有,則只能使用ValueAnimator
- 如上面的例子中,像ofFloat之類的工場方法,第一個參數為對象名,第二個為屬性名稱,後面的參數為可變參數,如果values…參數只設定了一個值的話,那麼會假定為目的值,屬性值的變化範圍為當前值到目的值,為了獲得當前值,該對象要有相應屬性的getter方法:get<PropertyName>
- 如果有getter方法,其應傳回值類型應與相應的setter方法的參數類型一致。
1 複製代碼 2 tv=(TextView)findViewById(R.id.textview1); 3 btn=(Button)findViewById(R.id.button1); 4 btn.setOnClickListener(new OnClickListener() { 5 @Override 6 public void onClick(View v) { 7 ObjectAnimator oa 8 =ObjectAnimator.ofFloat(tv, "alpha" 9 , 0f, 1f);10 oa.setDuration(3000);11 oa.start();12 }13 });
在有些情況下,我們可能需要在onAnimationUpdate() callback中 執行invalidate() 方法來強制螢幕重繪。
③:AnimatorSet動畫集
在動畫集中,我們可以設定組中動畫的時序關係,如同時播放,順序播放等。
1 AnimatorSet bouncer = new AnimatorSet(); 2 bouncer.play(bounceAnim).before(squashAnim1); 3 bouncer.play(squashAnim1).with(squashAnim2); 4 bouncer.play(squashAnim1).with(stretchAnim1); 5 bouncer.play(squashAnim1).with(stretchAnim2); 6 bouncer.play(bounceBackAnim).after(stretchAnim2); 7 ValueAnimator fadeAnim = ObjectAnimator.ofFloat(newBall, "alpha", 1f, 0f); 8 fadeAnim.setDuration(250); 9 AnimatorSet animatorSet = new AnimatorSet();10 animatorSet.play(bouncer).before(fadeAnim);11 animatorSet.start();
④: 動畫監聽
Animator.AnimatorListener中定義了動畫的一些公用介面
1 onAnimationStart()2 3 onAnimationEnd()4 5 onAnimationRepeat()6 7 //當動畫被取消時調用,同時會調用onAnimationEnd().8 onAnimationCancel()
注意:
通過觀察我們發現AnimatorListenerAdapter這個類 實現了Animator.AnimatorListener介面{每個方法的內部實現都是空} ,我們可以通過繼承這個類 {只複寫需要複寫的方法},而不是實現介面,來達到精簡代碼的操作。
1 ObjectAnimator oa=ObjectAnimator.ofFloat(tv, "alpha", 0f, 1f);2 oa.setDuration(3000);3 oa.addListener(new AnimatorListenerAdapter(){4 public void on AnimationEnd(Animator animation){5 Log.i("Animation","end");6 }7 });8 oa.start();
⑤ViewGroups過渡動畫
1 <LinearLayout2 android:orientation="vertical"3 android:layout_width="wrap_content"4 android:layout_height="match_parent"5 android:id="@+id/verticalContainer"6 <!--設定為true,在添加或者移除View的時候,播放動畫-->7 android:animateLayoutChanges="true" />8 複製代碼
常見過度動畫類型:
- LayoutTransition.APPEARING :當一個View在ViewGroup中出現時,對此View設定的動畫
- LayoutTransition.CHANGE_APPEARING 當一個View在ViewGroup中出現時,對此View對其他View位置造成影響,對其他View設定的動畫
- LayoutTransition.DISAPPEARING 當一個View在ViewGroup中消失時,對此View設定的動畫
- LayoutTransition.CHANGE_DISAPPEARING 當一個View在ViewGroup中消失時,對此View對其他View位置造成影響,對其他View設定的動畫
⑥View的animate()方法 和 ViewPropertyAnimator
1 // need API12 2 imageView.animate() 3 .alpha(0) 4 .y(50).setDuration(1000) 5 // need API 12 6 .withStartAction(new Runnable() { 7 //動畫前要執行的操作 8 @Override 9 public void run() {10 Log.e(TAG, "START");11 }12 // need API 1613 }).withEndAction(new Runnable() {14 // 動畫後要執行 的操作15 @Override16 public void run() {17 Log.e(TAG, "END");18 runOnUiThread(new Runnable() {19 @Override20 public void run() {21 imageView.setY(0);22 imageView.setAlpha(1.0f);23 }24 });25 }26 }).start();
ViewPropertyAnimator類多用於對一個View的多個屬性進行動畫,該類對多屬性動畫進行了最佳化,會合并一些invalidate()來減少重新整理視圖
1 /**與上面代碼的f功能相同*/2 PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("alpha", 1f, 3 0f, 1f); 4 PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("y", 50, 0); 5 ObjectAnimator.ofPropertyValuesHolder(mBlueBall, pvhX, pvhY).setDuration(1000).start();
⑦TypeEvaluator
根據屬性的開始、結束值與TimeInterpolation計算出的因子計算出目前時間的屬性值
- IntEvaluator:屬性的實值型別為int
- FloatEvaluator:屬性的實值型別為float
- ArgbEvaluator:屬性的實值型別為十六進位顏色值
- TypeEvaluator:一個介面,可以通過實現該介面自訂Evaluator
1 public class FloatEvaluator implements TypeEvaluator { 2 3 public Object evaluate(float fraction, Object startValue, Object endValue) { 4 float startFloat = ((Number) startValue).floatValue(); 5 /** 6 fraction 是根據動畫執行的時間跟應用的Interplator,計算出的一個0~1之間的因子 7 */ 8 return startFloat + fraction * (((Number) endValue).floatValue() - startFloat); 9 }10 }
⑧XML中使用屬性動畫
XML:
1 <?xml version="1.0" encoding="utf-8"?> 2 <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" 3 android:duration="1000" 4 android:propertyName="scaleX" 5 android:valueFrom="1.0" 6 android:valueTo="2.0" 7 android:valueType="floatType" > 8 </objectAnimator>
code:
1 public void scaleX(View view) 2 { 3 // 載入動畫 4 Animator anim = AnimatorInflater.loadAnimator(this, R.animator.scalex); 5 anim.setTarget(mMv); 6 anim.start(); 7 }
學習來自此處,謝謝大家!