2012-04-24 93 views
0

我有我的屏幕的以下XML結構。我希望我的Relative Layout with id journeyLandingView在單擊某個按鈕時滑動,並顯示帶有id的RelativeLayout viewRootView,它將從底部向上滑動,因爲journeyLandingView將Fill_Parent設置爲高度和寬度,因此journeyRootView必須低於journeyLandingView,覆蓋整個屏幕。我怎樣才能做到這一點?動畫相關佈局

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

    <RelativeLayout android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:id="@+id/journeyRootView" 
     android:background="@drawable/root_layer"> 

    </RelativeLayout> 
</LinearLayout> 

回答

2

我最終使用RelativeLAYOUT而不是LINEARLAYOUT作爲父元素,並使用以下代碼進行動畫處理。

TranslateAnimation slide = new TranslateAnimation(0, 0, 0, -1*Methods.screenHeight(this)); 
      slide.setDuration(1000); 
      slide.setFillAfter(true); 
      journeyLandingView.startAnimation(slide); 
      slide = new TranslateAnimation(0, 0, Methods.screenHeight(this), 0); 
      slide.setDuration(1000); 
      slide.setFillAfter(true); 
      journeyRootView.startAnimation(slide); 

public static int screenHeight(Context ctx) { 
     DisplayMetrics displaymetrics = new DisplayMetrics(); 
     ((Activity) ctx).getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);     
     return displaymetrics.heightPixels; 
    } 
+0

不錯的人可以說,如何做的幻燈片遺漏和滑向右in.You已完成從上到下 – raja 2013-10-30 09:37:04

7

我相信你想要的那種動畫可以使用ViewFlipper來實現。

您可以將您的RelativeLayouts放入ViewFlipper,並以編程方式在ViewFlipper上設置進出動畫。

示例代碼:

XML(佈局):

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:orientation="vertical"> 
      <Button 
       android:layout_height="wrap_content" 
       android:layout_width="fill_parent" 
       android:text="click me" 
       android:id="@+id/btn" 
       /> 
      <ViewFlipper 
       android:layout_height="fill_parent" 
       android:layout_width="fill_parent" 
       android:id="@+id/viewFlipper" 
       > 
      <RelativeLayout 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:id="@+id/journeyLandingView" 
       android:background="#FFFFFF" 
       > 
      </RelativeLayout> 
      <RelativeLayout 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:id="@+id/journeyRootView" 
       android:background="#FFA500" 
       > 

      </RelativeLayout> 
      </ViewFlipper>   
     </LinearLayout> 

的Java(活動):

ViewFlipper vflipper; 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     vflipper = (ViewFlipper) findViewById(R.id.viewFlipper); 
     vflipper.setInAnimation(this, android.R.anim.slide_in_left); 
     vflipper.setOutAnimation(this, android.R.anim.slide_out_right); 

     Button btn = (Button) findViewById(R.id.btn); 
     btn.setOnClickListener(this); 
    } 

    public void onClick(View v) 
    { 
     vflipper.showNext(); 
    } 

您將使用自定義動畫,而不是「android.R。 anim.slide_in_left「和」android.R.anim.slide_out_right「我在示例代碼中使用了行:

 vflipper.setInAnimation(this, android.R.anim.slide_in_left); 
     vflipper.setOutAnimation(this, android.R.anim.slide_out_right); 

但是,我並不擅長製作自定義動畫,所以我將它留給自己。 真的希望這有助於:-)

+1

真的很好的答案 – 2012-04-24 08:45:17