Animation in RelativeLayout to slide down

让人想犯罪 __ 提交于 2019-12-11 17:52:23

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!