標籤:
代碼功能
該樣本工程實現了自訂ActionBar,可以使一個圖片平滑過渡到ActionBar Icon的位置,並以ActionBar Icon的形式展現出來。而且還實現了背景圖片的自動切換,縮放和平移。可以在展示使用者資料等情境使用。
關鍵代碼學習
2.1 NoBoringActionBarActivity.java
setTitleAlpha(clamp(5.0F * ratio - 4.0F, 0.0F, 1.0F));
註:這行代碼是用來設定ActionBar Title 的透明度的,該方法控制Title在向上移動可移動總高度的4/5之後才開始顯示。
public static float clamp(float value, float min, float max) { return Math.max(min, Math.min(value, max));}
註:該方法限制Value的取值範圍為[min,max]
2.2 KenBurnsView.java
Handler.post(Runnable r);
註:將參數Runnable的執行個體對象加入到Handler訊息佇列中
Handler.removeCallbacks(Runnable r);
註:將參數Runnable的執行個體對象從訊息佇列中移除
private void start(View view, long duration, float fromScale, float toScale, float fromTranslationX, float fromTranslationY, float toTranslationX, float toTranslationY) { view.setScaleX(fromScale); view.setScaleY(fromScale); view.setTranslationX(fromTranslationX); view.setTranslationY(fromTranslationY); ViewPropertyAnimator propertyAnimator = view.animate().translationX(toTranslationX) .translationY(toTranslationY).scaleX(toScale).scaleY(toScale).setDuration(duration); propertyAnimator.start(); Log.d(TAG, "starting Ken Burns animation " + propertyAnimator);}
註:執行該方法的效果是使背景圖片隨機平移,縮放。
系統API使用與理解
3.1 ViewPropertyAnimator
This class may provide better performance for several simultaneous animations, because it will optimize invalidate calls to take place only once for several properties instead of each animated property independently causing its own invalidation.
根據官方API的解釋,該類在幾個動畫同時執行的時候使用效能比較好。
總結
這麼一個簡單的demo自己愣是用了3周時間才整理出來,太拖拉了。而且動畫那塊的東西現在自己還是沒有吃透,也許過幾天會發一篇關於安卓動畫的部落格吧。不囉嗦了,附件為自己簡單注釋過的代碼,有興趣的同學可以研究一下。
http://yunpan.cn/cfGZLjBZS6udk 訪問密碼 b0c7
Android 開原始碼學習——NoBoringActionBar