Android 開原始碼學習——NoBoringActionBar

來源:互聯網
上載者:User

標籤:

代碼功能

該樣本工程實現了自訂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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.