Android Crossfading animation 淡出淡入動畫

來源:互聯網
上載者:User

標籤:android   style   blog   io   ar   color   os   使用   sp   

淡出淡入動畫就是我們常說的漸隱動畫,一個介面逐漸消失的時候另一個逐漸顯現。當你需要在應用中切換兩個視圖的時候這個動畫效果就顯得非常實用了。 這個動畫短小但很精緻,巧妙的銜接了視圖的切換。如果你不使用這種動畫會讓整個切換過程顯得生硬且急促。

準備開始1.建立成員變數連結到你需要時間動畫的view上。2.讓後顯示的view先Gone掉,避免它佔用layout的空間,避免計算它導致系統資源浪費,加快介面載入速度。3.用一個成員變數緩衝系統的config_shortAnimTime變數,這個變數是系統提供的一種動畫時間,他會確保動畫執行的更一致和更精緻。與此變數作用相似的還有config_longAnimTime和config_mediumAnimTime。(引用方法:int mShortAnimationDuration = getResources().getInteger(android.R.integer.config_shortAnimTime);)
用代碼標示:
public class CrossfadeActivity extends Activity {    private View mContentView;    private View mLoadingView;    private int mShortAnimationDuration;    ...    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_crossfade);        mContentView = findViewById(R.id.content);        mLoadingView = findViewById(R.id.loading_spinner);        // Initially hide the content view.        mContentView.setVisibility(View.GONE);        // Retrieve and cache the system's default "short" animation time.        mShortAnimationDuration = getResources().getInteger(                android.R.integer.config_shortAnimTime);    }
漸隱這個View以上我們已經做好準備工作了,接下來我們會來實現把它漸隱。1.把我們將要顯示的view的alpha值改為0, 將它Visibility改為Visible。現在這個view就能顯示了,但是你還看不到它,因為他是透明的。2.建立兩個alpha的動畫,一個是將alpha從0到1,另一個是將alpha值設定為從1到0。3.在Animator.AnimatorListener的監聽回調方法onAnimationEnd()將最開始顯示的View gone掉。儘管他的alpha值是0我們已經看不到他了,但是我們還是需要把它的visibility屬性設定為gone。這樣可以減少他佔用的空間,減少layout的計算,從而提升運行速度。
下面我們來看一下代碼:
private View mContentView;private View mLoadingView;private int mShortAnimationDuration;...private void crossfade() {    // Set the content view to 0% opacity but visible, so that it is visible    // (but fully transparent) during the animation.    mContentView.setAlpha(0f);    mContentView.setVisibility(View.VISIBLE);    // Animate the content view to 100% opacity, and clear any animation    // listener set on the view.    mContentView.animate()            .alpha(1f)            .setDuration(mShortAnimationDuration)            .setListener(null);    // Animate the loading view to 0% opacity. After the animation ends,    // set its visibility to GONE as an optimization step (it won't    // participate in layout passes, etc.)    mLoadingView.animate()            .alpha(0f)            .setDuration(mShortAnimationDuration)            .setListener(new AnimatorListenerAdapter() {                @Override                public void onAnimationEnd(Animator animation) {                    mLoadingView.setVisibility(View.GONE);                }            });}

--------------本文完---------------題外話:1.動畫最好使用FrameLayout而不是LinearLayout或RelativeLayout。因為動畫涉及很多座標的問題,絕對位置和相對位置。使用別的布局容易導致座標計算變的困難且難以琢磨。所以使用動畫的布局最好使用FrameLayout。2.從API12即Andoird3.1起view新增了animate()方法可以直接向上面例子一樣執行動畫,而不用載入一個xml動畫,或者建立一個Animation對象了,不過還是看個人習慣了。

Android Crossfading animation 淡出淡入動畫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.