android - fragment设置关于translationY属性的objectAnimator属性动画
PHP中文网
PHP中文网 2017-04-17 17:24:39
0
2
820

在给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>
PHP中文网
PHP中文网

认证高级PHP讲师

reply all(2)
大家讲道理

Replace translationY with y and see,

Ty80

A solution has been found so far. This problem seems to be related to the layout of the activity.
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>

In the layout of the activity, I have set the heights of buttons and imageView. The space from below the imageView to the bottom of the screen, which is ayout_marginBottom="85dp", can be said to be empty (such a meaning is difficult to express). My current solution is to add a 1dp high view at the bottom of this space:

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

After adding this, even if the value of valueFrom is greater than or equal to the height, the animation can be displayed normally, but I don’t know why this happens. I feel that if I don’t add this view, the frameLayout that stores the fragment will be in the above mentioned " "empty" space, so it cannot be displayed. I don't know how this process is drawn in the source code, and I hope to get an answer.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template