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

在给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讲师

全部回覆(2)
大家讲道理

你把translationY換成y看看,

Ty80

目前找到了一個解決方法,出現這個問題好像跟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就在上述所說那個“空」的空間裡,所以顯示不出來。我不清楚這個過程原始碼裡是如何繪製的,希望得到解答。

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!