2014-01-21 42 views
0

我需要幫助來動畫RelativeLayout。接下來的圖像解釋如何爲我的aplication:RelativeLayout中的動畫滑落

enter image description here

現在,當我觸摸到地圖的標記,它顯示的RelativeLayout(呼叫標記標題中的圖片),並向上滑動效果很好的動畫,不過,我滑下的代碼並不能正常工作,因爲它從屏幕頂部下來。我的代碼是這樣的:

  • 這是它隱藏的RelativeLayout代碼:

    public void hideSlideUp() { 
        RelativeLayout slideLayout; 
        slideLayout = (RelativeLayout) findViewById(R.id.sliding_up); 
    
        if (slideLayout.getVisibility() != View.INVISIBLE) { 
         Animation slide = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_down); 
         slideLayout.startAnimation(slide); 
         slideLayout.setVisibility(View.INVISIBLE); 
        } 
    } 
    
  • 動畫XML文件,現在它的工作錯誤:

    <set xmlns:android="http://schemas.android.com/apk/res/android"> 
        <translate 
         android:fromXDelta="0" 
         android:fromYDelta="-1000" 
         android:duration="1000" /> 
    </set> 
    

回答

5

你必須找出一種方法來計算地圖的高度,並從動畫開始位置減少。這樣,動畫就從當前位置開始,相對於屏幕的頂部。

做到這一點的另一種方法是巧妙的佈局技巧。

下面是一些示例代碼,介紹如何使用FrameLayout將LinearLayout覆蓋在Map視圖上。您將透明視圖權重設置爲1,以便當您將TextView切換到GONE時,它將展開以填充空間。爲了動畫這個變化,將animateLayoutChanges="true"添加到封閉的視圖中。

實施例:

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
    <Map 
     ... 
    /> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:animateLayoutChanges="true" > 
     <View 
      android:id="@+id/transparentViewThatExpandsWhenTextIsGone" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" /> 
     <TextView 
      android:id="@+id/textViewToToggle" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:visibility="gone" /> 
    </LinearLayout> 
</FrameLayout> 
+1

我使用的第二選項和它工作得很好。非常感謝!! :D – Yeray

+0

很高興它的工作! –