2011-08-01 48 views
0

我有動畫的一個問題,這裏是我的代碼:動畫在相對layour重新定位的Android其它視圖

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    new Handler().postDelayed(new Runnable(){ 

     @Override 
     public void run() { 
      Animation anim= new TranslateAnimation(0, 0, 0, -200); 
      anim.setDuration(2000); 
      anim.setFillAfter(true); 
      findViewById(R.id.button1).startAnimation(anim); 

     } 

    }, 2000); 
} 

和主要佈局是:

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

<Button 
    android:id="@+id/button1" 
    android:layout_width="fill_parent" 
    android:text="Button" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:minHeight="120sp"/> 

<Button 
    android:id="@+id/button2" 
    android:layout_width="fill_parent" 
    android:text="Button" 
    android:layout_height="fill_parent" 
    android:layout_below="@id/button1" 
    android:minHeight="120sp"/> 

</RelativeLayout> 

我想BUTTON2與按鈕1一起移動。我知道我可以將相同的動畫應用到兩個按鈕,並且它們一起移動,但是然後button2將移動到底部邊距。

有人知道我該如何解決它嗎?

回答

2

我不確定你真正想要達到什麼。我想你會在動畫之後將button2保留在佈局的頂部。我對嗎?

據我所知,在Android中,視圖在動畫處理其他視圖時不會重新定位自己。將視圖的可見性設置爲GONE時,它們會進行重新定位。

因此,你可以:

  1. 應用不同的動畫順序,你希望它是要移動到按鈕2。
  2. 的Button1設置可見動畫後消失:

    button1.setVisibility(View.GONE); 
    
+0

感謝您的答案。所以,如果我想要按鈕2展開,直到填滿整個窗口,我應該結合翻譯和垂直縮放?然後把按鈕1去右? – Addev

+0

使用你的代碼,如果你只是將button1設置爲GONE,button2擴展並填充整個佈局,因爲android:layout_height =「fill_parent」屬性。所以只需在動畫後將button1設置爲GONE即可。 –

+0

是的,但我想動畫不需要的時刻翻譯按鈕1 – Addev

2

首先,Android的動畫不會影響佈局。你的按鈕沒有確實移動,它看起來像它。其次,如果你想讓兩個按鈕一起移動,就把它們放在一個線性佈局中,然後改變它的動畫效果。

相關問題