在给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>
Replace translationY with y and see,
A solution has been found so far. This problem seems to be related to the layout of the activity.
activity layout:
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:
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.