標籤:根據 art tran 沒有 無效 public initial rap tla
接上篇《android開發藝術探索》讀書筆記(五)--Drawable
No1:
自訂動畫:派生一種新動畫只需要繼承Animation這個抽象類別,然後重寫它的initialize和applyTransformation方法,在initialize方法中做一些初始化工作,在applyTransformation中進行相應的矩陣變換即可,很多時候需要採用Camera來簡化矩陣變換的過程。
No2:
屬性動畫PropertyAnimation補間動畫TweenAnimation幀動畫FrameAnimation
No3: LayoutAnimation:LayoutAnimation作用於ViewGroup,為ViewGroup指定一個動畫,這樣當它的子項目出場時都會具有這種動畫效果。這種效果常常被用在ListView上。
//res/anim/anim_layout.xml<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android" android:animation="@anim/anim_item" android:animationOrder="normal" android:delay="0.5" />
//res/anim/anim_item.xml<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="500" android:interpolator="@android:anim/accelerate_interpolator" android:shareInterpolator="true"> <alpha android:fromAlpha="0.0" android:toAlpha="1.0" /> <translate android:fromXDelta="1000" android:toXDelta="0" /></set>
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.administrator.layoutanimation.MainActivity"> <ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fff4f7f9" android:cacheColorHint="#00000000" android:divider="#dddbdb" android:dividerHeight="1.0px" android:layoutAnimation="@anim/anim_layout" android:listSelector="@android:color/transparent" /></RelativeLayout>
還可通過LayoutAnimationController來實現
ListView listView = (ListView) findViewById(R.id.list);Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim_item);LayoutAnimationController controller = new LayoutAnimationController(animation);controller.setDelay(0.5f);controller.setOrder(LayoutAnimationController.ORDER_NORMAL);listView.setLayoutAnimation(controller);
No4:
Activity切換效果,主要用到overridePendingTransition(int enterAnim,int exitAnim),此方法必須在startActivity(Intent)或者finish()之後調用才會生效
enterAnim--Activity被開啟時,所需的動畫資源id
exitAnim--Activity被暫停時,所需的動畫資源id
Fragment切換動畫,可以通過FragmentTransaction中的setCustomAnimations()方法來實現。切換動畫需要是View動畫
No5:
屬性動畫可以對任意對象的屬性進行動畫而不僅僅是View,可以採用開源動畫庫nineoldandroids來相容以前的版本
No6:
TimeInterpolator時間插值器,它的作用是根據時間流逝的百分比來計算出當前屬性值改變的百分比。
TypeEvaluator類型估值演算法(估值器),它的作用是根據當前屬性改變的百分比來計算改變後的屬性值。
自訂插值器需要實現Interpolator或者TimeInterpolator,自訂估值演算法需要實現TypeEvaluator。
No7:AnimatorListener可以監聽動畫的開始、結束、取消和重複播放。AnimatorUpdateListener可以監聽整個過程,動畫每播放一幀,onAnimationUpdate就會被調用一次。
No8:我們對object的屬性abc做動畫,如果想讓動畫生效,需要同時滿足兩個條件1)object必須要提供setAbc方法,如果動畫的時候沒有傳遞初始值,那麼還要提供getAbc方法,因為系統要去取abc屬性的初始值(如果這條不滿足,程式直接Crash)2)object的setAbc對屬性abc所做的改變必須能夠通過某種方式反映出來,比如會帶來UI的改變之類的(如果這條不滿足,動畫無效果但不會Crash)解決不滿足條件二的方法:
private void performAnimate(){ ViewWrapper wrapper = new ViewWrapper(mButton); ObjectAnimator.ofInt(wrapper,"width"500).setDuration(5000).start();}@Overridepublic void onClick(View v){ if(v == mButton){ performAnimate(); }}private static class ViewWrapper{ private View mTarget; public ViewWrapper(View target){ mTarget = target; } public void setWidth(int width){ mTarget.getLayoutParams().width = width; mTarget.requestLayout(); }}
No9:
ValueAnimator監聽動畫過程,本身不作用於任何對象。它可以對一個值做動畫,然後我們可以監聽其動畫過程,在動畫過程中修改我們的對象的屬性值,這樣也就相當於我們的對象做了動畫。
No10:
使用動畫注意事項:
1)OOM--幀動畫圖片數量較多且圖片較大時就極易出現OOM
2)記憶體泄露--無限迴圈屬性動畫需要在Activity退出時及時停止,否則導致Activity無法釋放從而造成記憶體泄露,View動畫不存在此問題
3)相容--動畫在3.0以下系統有相容性問題
4)需要使用px--盡量使用dp
5)動畫元素互動--屬性動畫和View動畫的單擊事件觸發位置
6)硬體加速--硬體加速會提高動畫的流暢性
《android開發藝術探索》讀書筆記(五)--動畫