问题
I need help to animate a RelativeLayout. The next image explain how is my aplication:

Now, when I touch a marker of a map, it displays a RelativeLayout (call Marker Title in the picture) and the animation to slide up works well, however, my code to slide down doesn't work well because it get down from top of the screen. My code is this:
This is the code where it hides the 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); } }
Animation xml file, now it's work wrong:
<set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="0" android:fromYDelta="-1000" android:duration="1000" /> </set>
回答1:
You'd have to figure out a way to calculate the height of your map and reduce that from the animation start position. That way, the animation starts at its current location, vs the top of the screen.
Another way to do this, is a clever layout trick.
Below, is some sample code of how you would layer a LinearLayout over your Map view using a FrameLayout. You set the transparent view weight to 1, so that when you toggle the TextView to GONE, it expands to fill the space. In order to animate this change, add animateLayoutChanges="true"
to the enclosing views.
Example:
<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>
来源:https://stackoverflow.com/questions/21270665/animation-in-relativelayout-to-slide-down