2013-05-03 34 views
0

判定我有兩個FrameLayout小號相互重疊的底部(讓我們稱之爲layout_frontlayout_back,其中每個佈局具有多個文字和圖像視圖)。最初layout_back的可見度將消失。動畫布局到的另一佈局,其距離在運行時

我想要什麼來實現:當我想layout_back顯示,我將它設置爲可見的,並且我希望動畫layout_front滑落到layout_back的底部。效果就像我有兩張「卡片」,滑下前面的卡片在後面顯示另一張卡片。

問題:第一個明顯的方式做到這一點是由layout_front這樣創建動畫的xml:

<set xmlns:android="http://schemas.android.com/apk/res/android"> 
<translate 
    android:duration="400" 
    android:fillAfter="true" 
    android:fromYDelta="0%" 
    android:toYDelta="100%" /> 
</set> 

1)我的第一個問題是,在layout_front不會動畫完成後,留在了那裏,但翻譯後立即回到原來的位置。我應該怎麼做才能讓在滑下後留在

2)我的第二個問題更嚴重。 layout_front滑動的距離(這是layout_back的高度)直到運行時才確定。有沒有辦法動態設置YDelta的值?

回答

0

我自己回答。佈局會是這個樣子:

<RelativeLayout 
     android:id="@+id/pair_layout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <FrameLayout 
      android:id="@id/layout_back" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@color/list_background" 
      android:visibility="gone" > 

      <TextView 
       android:id="@id/text" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textAppearance="?android:attr/textAppearanceMedium"/> 
     </FrameLayout> 

     <FrameLayout 
      android:id="@id/layout_front" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@color/background"> 

      <Button 
       android:id="@id/button_blue" 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" > 
      </Button> 
     </FrameLayout> 
    </RelativeLayout> 

佈局在後面由一個TextView的,而在前面的佈局是一個按鈕。當按鈕被點擊時,它將滑動到textview的底部。按鈕監聽器是這樣實現的:

public class ButtonListener implements OnClickListener { 
    final FrameLayout l_front; 
    final FrameLayout l_back; 

    public blueListener(FrameLayout f, FrameLayout b) { 
     this.l_back = b; 
     this.l_front = f; 
    } 

    @Override 
    public void onClick(View v) { 

     l_back.setVisibility(View.VISIBLE); 
     l_back.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); 
     l_front.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); 

     TranslateAnimation tanim = new TranslateAnimation(
       TranslateAnimation.ABSOLUTE, 0.0f, 
       TranslateAnimation.ABSOLUTE, 0.0f, 
       TranslateAnimation.ABSOLUTE, 0.0f, 
       TranslateAnimation.ABSOLUTE, l_back.getMeasuredHeight()); 
     tanim.setDuration(400); 
     tanim.setFillAfter(true); 
     tanim.setInterpolator(new DecelerateInterpolator()); 
     l_front.setAnimation(tanim); 
     ((RelativeLayout)l_front.getParent()).getLayoutParams().height 
     = l_back.getMeasuredHeight() + l_front.getMeasuredHeight(); 
    } 
}