首页 > fragment设置关于translationY属性的objectAnimator属性动画

fragment设置关于translationY属性的objectAnimator属性动画

在给fragment设置关于translationY属性的objectAnimator属性动画时,如果valueFrom的值大于等于fragment的高度时(下面代码中的第一个动画),fragment的这个动画不能在activity上显示出来,当valueFrom的值小于fragment的高度时,fragment的这个动画就能在activity上正常显示。请问为何会出现这种情况。
fragment设置动画的代码:

@Override
    public Animator onCreateAnimator(int transit, boolean enter, int nextAnim) {
        Animator anim;
        if (enter) {
            anim = ObjectAnimator.ofFloat(mRootView, "translationY", mRootView.getHeight(), 0).setDuration(2000);
        } else {
            anim = ObjectAnimator.ofFloat(mRootView, "translationY", 0, mRootView.getHeight()-1).setDuration(2000);
        }

        return anim;
    }

fragment的mRootView的layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:background="@drawable/bg_white"
              android:layout_width="match_parent"
              android:layout_height="wrap_content">
    <ImageView
        android:layout_marginLeft="30dp"
        android:layout_marginTop="15dp"
        android:layout_marginBottom="15dp"
        android:layout_marginRight="40dp"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:src="@drawable/icon_navigation"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="@string/app_name"
        android:id="@+id/location_txv"
        android:gravity="center_vertical"
        android:textSize="16sp"/>
</LinearLayout>

你把translationY换成y看看,


目前找到了一个解决方法,出现这个问题好像和activity的layout有关。
activity的layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/root_view"
                android:gravity="top|left"
                android:orientation="vertical">

    <LinearLayout
        android:id="@+id/title_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <Button
            android:id="@+id/title_btn"
            android:layout_width="fill_parent"
            android:layout_height="48dp"/>
    </LinearLayout>

    <ImageView
        android:id="@+id/location_view"
        android:layout_width="55dp"
        android:layout_height="55dp"
        android:src="@drawable/icon_location"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="25dp"
        android:layout_marginBottom="85dp"/>
    
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/fragment_bottom"
        android:layout_alignParentBottom="true">
    </FrameLayout>
</RelativeLayout>

在activity的layout里我定好高度的有button、imageView,在imageView下面到屏幕底也就是ayout_marginBottom="85dp"的空间里可以说是空的(这样的一个意思,不好表达)。我现在的解决方式是在这个空间的底部加一个1dp高的view:

<ImageView
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_alignParentBottom="true"/>

加了这个,即使valueFrom的值大于等于高度,动画也可以正常显示,但是我不知道为什么会发生这种情况,感觉如果我不加这个view的话,存放fragment的frameLayout就在上述所说那个“空”的空间里,所以显示不出来。我不清楚这个过程源码里是如何绘制的,希望得到解答。

【热门文章】
【热门文章】