2014-04-30 38 views
1

我想實現一張紙上出現的項目列表的一張紙的效果。我提出了一種方法,但是當我正在研究它時,我不能一直認爲必須有更好的方法。動畫ListView到視圖

我的方法:

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    > 
    <ListView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/orderDetailListView"> 

    </ListView> 

</LinearLayout> 

動畫代碼:

final int newMargin = <some value>; 
Animation a = new Animation() { 

@Override 
protected void applyTransformation(float interpolatedTime, Transformation t) { 
    LayoutParams params = listView.getLayoutParams(); 
    params.leftMargin = (int)(newMargin * interpolatedTime); 
    yourView.setLayoutParams(params); 
    } 
}; 

listView.startAnimation(a)的

我的問題:

有沒有更好的方法?我不是在問一個暗示性的問題,我只是想知道是否有某種內置函數可以使觀點從一個設定點逐漸出現。

Image from yplan app

+0

我不完全確定你想要動畫看起來像什麼,它聽起來像一個簡單的翻譯動畫? – rperryng

+0

看看這個庫:https://github.com/nhaarman/ListViewAnimations。希望它可以幫助。 –

+0

我從yplan應用程序添加圖片是一樣的想法。我沒有試圖移動它,因爲它顯示它緩慢出現 – Mika

回答

1

好像你正在努力實現一個簡單的翻譯動畫。處理這種情況的一種方法是使用在Api級別10中引入的Property Animation Api(並且通過Jake Wharton的nine old androids庫將其慷慨地backport到api級別1)。

一個簡單的例子可以是以下

ObjectAnimator.ofFloat(theViewObject, "translationY", -theViewObject.getHeight(), 0) 
     .setDuration(2000) // 2 seconds 
     .start(); 

有時,呼籲像getHeight()getRight()等,返回0,這是因爲該視圖實際上沒有被抽呢。爲了適應這一點,你可以註冊一個回調來知道視圖何時被繪製。

// Listen for when the view has been drawn so we can get its dimensions 
final ViewTreeObserver viewTreeObserver = theViewObject.getViewTreeObserver(); 
if (viewTreeObserver.isAlive()) { 
    viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
     @TargetApi(Build.VERSION_CODES.JELLY_BEAN) 
     @SuppressWarnings("deprecation") 
     @Override 
     public void onGlobalLayout() { 

      ObjectAnimator.ofFloat(
        theViewObject, "translationY", -theViewObject.getHeight(), 0) 
        .setDuration(2000) // 2 seconds 
        .start(); 

      // stop listening for updates to the view so that this is only called once 
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { 
       viewTreeObserver.removeOnGlobalLayoutListener(this); 
      } else { 
       viewTreeObserver.removeGlobalOnLayoutListener(this); 
      } 
     } 
    }); 
} 
+0

哦這麼簡單!來自iOS的我經常抱怨Android,但有時我會把劍和盾放在腳下! – Mika

+0

一個跟進問題。當我將ViewObject放在LinearLayout中時,這怎麼辦? (在問題中輸入精確的代碼) – Mika

+0

這可能是因爲視圖尚未繪製,所以'getHeight()'返回0.我將編輯該片段以顯示如何解決此問題 – rperryng