2013-01-08 74 views
0

我想要使用TranslateAnimation更改佈局。無法在android中使用動畫更改佈局

我的XML是像

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 



<Button 
    android:id="@+id/buttonsld" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/textsld" 
    android:text="slideup" /> 

<LinearLayout 
    android:id="@+id/layoutsld2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/buttonsld2" 
    android:layout_below="@+id/buttonsld" 
    android:background="@drawable/iz" 
    android:orientation="vertical" > 
</LinearLayout> 

<LinearLayout 
    android:id="@+id/layoutsld3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/buttonsld2" 
    android:layout_below="@+id/buttonsld" 
    android:background="@drawable/fon" 
    android:orientation="vertical" 
    android:visibility="gone" > 
</LinearLayout> 

<Button 
    android:id="@+id/buttonsld2" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:text="slidedown" /> 

</RelativeLayout> 

,我的代碼是這樣

private int x= 0; 

buttonslideup.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       if (x == 0) { 
        lin_lay2.setVisibility(View.GONE); 
        lin_lay3.setVisibility(View.VISIBLE); 
        Animation slide = new TranslateAnimation(0, 0, 100, 0); 
        slide.setDuration(1000); 
        slide.setFillAfter(true); 

        lin_lay3.startAnimation(slide); 
        x = 1; 
       } else { 
        lin_lay3.setVisibility(View.GONE); 

        lin_lay2.setVisibility(View.VISIBLE); 
        Animation slide = new TranslateAnimation(0, 0, 
        100, 0); 

        slide.setDuration(1000); 
        slide.setFillAfter(true); 
        lin_lay2.startAnimation(slide); 
        x = 0; 
       } 

      } 
     }); 

但是當我利用這一點,在第一,lin_lay3幻燈片,來篩選並沒有問題,但是當我再次單擊按鈕時,幻燈片爲lin_lay2,但它落後於lin_lay3,我看不到lin_lay2

我想要的是改變這些layouts點擊button

+0

隱藏linearlayout3 ..如果你不想.. – itsrajesh4uguys

+0

你可以在代碼中看到有setvisibility(View.GONE),這是行不通的 – kaplanfat

+0

做到這樣。首先檢查linearlayout3是否可見或根據該更改代碼..並繼續 – itsrajesh4uguys

回答

0
you start another activity on button click & see the animation of ur activity layout. 
    just put this code after starActivity(intent).. 

    overridePendingTransition(R.anim.slide_in_top, R.anim.slide_out_bottom); 


    slide_in_top.xml 
    <?xml version="1.0" encoding="utf-8"?> 
    <translate xmlns:android="http://schemas.android.com/apk/res/android" 
     android:duration="@integer/slideanim" 
     android:fromYDelta="-100%p" 
     android:toYDelta="0" /> 

    slide_out_bottom.xml 
    <?xml version="1.0" encoding="utf-8"?> 
    <translate xmlns:android="http://schemas.android.com/apk/res/android" 
     android:duration="@integer/slideanim" 
     android:fromYDelta="0" 
     android:toYDelta="100%p" /> 

    in value folder create integer.xml & put 

    <item name="slideanim" format="integer" type="integer">1000</item>