2013-06-04 134 views
17

用於Fragment.onCreateAnimator(int, boolean, int)方法整個文檔包含以下文本:Fragment.onCreateAnimator()的文檔在哪裏?

「當一個片段加載動畫調用。」

就是這樣。沒有關於參數的解釋。

這些參數是什麼意思? Even the source code doesn't reveal much.

+0

似乎這種方法的使用結果可以給一個想法http://grepcode.com/search/usages?type=method&id=repository.grepcode.com%24java%[email protected]%[email protected]。 2_r1 @ android%24app @Fragment @ onCreateAnimator%28int%2Cboolean%2Cint%29&k = u – sandrstar

回答

6

基於FragmentManager代碼和FragmentManagerImpl.loadAnimator(android.app.Fragment,int,boolean,int)用法似乎Fragment.onCreateAnimator(int, boolean, int)讓你定義自己的動畫片段爲隱藏,顯示,改變狀態。但是,我從來沒有在真實應用中看到它的使用。

關於參數:

  • int transit - 過渡類型(常量FragmentTransaction,例如,在使用here);
  • boolean enter - true如果它的狀態輸入,則爲false - 否則;
  • int transitionStyle - 來自資源的樣式ID(該樣式可能包含從onCreateAnimator錯過的動畫);
+0

感謝您深入挖掘。我在'onCreateAnimator()'方法的開頭放了一個'Log.d()'語句,發現在交換片段時總是將'transit'設置爲'0'。 –

+0

@NathanOsman你在片段事務上調用了setTransit()嗎? – JakeCataford

13

onCreateAnimator方法很奇怪。如果 '進入',否則

int nextAnim假假真真 - -

public Animator onCreateAnimator(int transit, boolean enter, int nextAnim)

int transit - 過渡類型,如sandrstar上述

boolean enter說: 我見過的原型是這樣的即將播放的動畫的資源ID。

因此,舉例來說,如果你試圖做一個卡翻蓋,from the documentation

// Create and commit a new fragment transaction that adds the fragment for the back of 
// the card, uses custom animations, and is part of the fragment manager's back stack. 
BackOfCardFragment backFragment = new BackOfCardFragment(); 

getFragmentManager() 
    .beginTransaction() 

    // Replace the default fragment animations with animator resources representing 
    // rotations when switching to the back of the card, as well as animator 
    // resources representing rotations when flipping back to the front (e.g. when 
    // the system Back button is pressed). 
    .setCustomAnimations(
     R.animator.card_flip_right_in, R.animator.card_flip_right_out, 
     R.animator.card_flip_left_in, R.animator.card_flip_left_out) 

    // Replace any fragments currently in the container view with a fragment 
    // representing the next page (indicated by the just-incremented currentPage 
    // variable). 
    .replace(R.id.container_view, backFragment) 

    // Add this transaction to the back stack, allowing users to press Back 
    // to get to the front of the card. 
    .addToBackStack(null) 

    // Commit the transaction. 
    .commit(); 

注:R.id.container_view在上面的例子是一個ViewGroup的包含現有片段ID你正在嘗試更換。

當執行上面的代碼,該方法onCreateAnimator將調用,並且nextAnim參數會傳遞到setCustomAnimations()功能的四個動畫ID中的一個,即R.animator.card_flip_right_in,R.animator.card_flip_right_out。 ..等

這似乎並沒有立即有用,因爲它沒有給你一個參考實際的Animator對象,你可以附加一個監聽器。但奇怪的是,你可以直接從nextAnim資源膨脹的另一個動畫,然後安裝聽衆的是,這將,奇怪的是,解僱所有被覆蓋的回調在正確的時間:

@Override 
public Animator onCreateAnimator(int transit, boolean enter, int nextAnim) { 
    Animator animator = null; 
    // In this example, i want to add a listener when the card_flip_right_in animation 
    // is about to happen. 
    if (nextAnim == R.animator.card_flip_right_in) { 
     animator = AnimatorInflater.loadAnimator(getActivity(), nextAnim); 
     // * Sometimes onCreateAnimator will be called and nextAnim will be 0, 
     // causing animator to be null. 
     // * I wanted to add a listener when the fragment was entering - 
     // your use case may be different. 
     if (animator != null && enter) { 

      animator.addListener(new Animator.AnimatorListener() { 
       @Override 
       public void onAnimationStart(Animator animation) { 
        // Do something when the card flip animation begins 
       } 

       @Override 
       public void onAnimationEnd(Animator animation) { 
        // Do something as soon as the card flip animation is over 
       } 

       @Override 
       public void onAnimationCancel(Animator animation) { 
       } 

       @Override 
       public void onAnimationRepeat(Animator animation) { 
       } 
      }); 
     } 
    } 
    return animator; 
} 

這樣,你應該能夠將偵聽器添加到片段轉換動畫師,就好像您自己創建了它們一樣。