1

我只想说这不是重复的,这个问题还没有在堆栈溢出时得到解决

我在用户导航的应用程序中有多个片段。我的代码中有支持库,但后来我删除了它,当我使用它时,我正在使用 <translate>. 如果我在往返的值中硬编码,我现在已经将其更改为可以正常工作。由于我的应用程序将用于不同的屏幕密度,我不能这样做。因此,在研究和教程之后,我创建了我的自定义 FrameLayout,如下所示:

public class CustomFrameLayout extends FrameLayout{

    public CustomFrameLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


   public float getXFraction() {
        return getX() / getWidth(); // TODO: guard divide-by-zero
    }

    public void setXFraction(float xFraction) {
        // TODO: cache width
        final int width = getWidth();
        setX((width > 0) ? (xFraction * width) : -9999);
    }
}

我的动画 xml 是:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
        android:interpolator="@android:anim/linear_interpolator"
        android:valueFrom="1.0"
        android:valueTo="0"
        android:propertyName="xFraction"
        android:duration="@android:integer/config_mediumAnimTime" />
</set>

我的主要活动布局是:

<com.axn.test.app.custom.CustomFrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:gravity="top"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

然后我有代码来设置自定义动画:

FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
        ft.setCustomAnimations(R.animator.fragment_slide_in_right, R.animator.fragment_slide_in_right, R.animator.fragment_slide_in_right, R.animator.fragment_slide_out_right);

问题是当我更改片段时没有动画,我在控制台中得到以下内容:

W/PropertyValuesHolder﹕ Method setXFraction() with type float not found on target class class android.widget.RelativeLayout

因此,如果有人过去遇到过此问题或知道如何解决此问题,我将不胜感激。在我的 customFrameLayout中setXFraction(float xFraction)并且 getXFraction()从未被调用,所以我不确定从这里去哪里......所有输入都非常感谢

**编辑 ==== 片段布局 ===== **

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    >

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:layout_marginTop="20dp"
            android:layout_marginLeft="20dp"
            android:id="@+id/show1"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="true">

            <ImageView
                android:id="@+id/poster1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/poster1"/>
            <RelativeLayout
                android:id="@+id/poster1Details"
                android:background="@drawable/reflection1"
                android:layout_below="@+id/poster1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <TextView
                    android:id="@+id/poster1text"
                    android:layout_centerHorizontal="true"
                    android:textColor="@color/white"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:text="@string/poster1Title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/poster1text"
                    android:layout_centerHorizontal="true"
                    android:textColor="@color/white"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text="@string/episode1"/>
            </RelativeLayout>
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/show2"
            android:layout_weight="1"
            android:layout_marginRight="10dp"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="true">

            <ImageView
                android:id="@+id/poster2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/poster2"/>
            <RelativeLayout
                android:id="@+id/poster2Details"
                android:background="@drawable/reflection2"
                android:layout_below="@+id/poster2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <TextView
                    android:id="@+id/poster2text"
                    android:layout_centerHorizontal="true"
                    android:textColor="@color/white"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:text="@string/poster2Title"
                    android:gravity="center"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/poster2text"
                    android:layout_centerHorizontal="true"
                    android:textColor="@color/white"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text="@string/episode1"/>

            </RelativeLayout>
        </RelativeLayout>
    </LinearLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/logos_backgrounds"
      />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/presents"
            android:textColor="@color/white"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="10dp"
            />
    </RelativeLayout>
</RelativeLayout>
4

1 回答 1

-1

问题是您正在尝试为片段设置动画。不是活动。因此,您必须在要设置动画的片段上添加 CustomFrameLayout。一旦您的交易发生,该交易将为您的片段父级而不是您的活动的值设置动画。这就是为什么你得到Method setXFraction() with type float not found on target class class android.widget.RelativeLayout. 因为您的片段的父级是一个 RelativeLayout 并且它没有 setXFraction 方法。

我仍然会将其余的答案留在下面,因为它可能会帮助其他人。

in_from_left.xml

<objectAnimator
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500"
    android:propertyName="x"
    android:valueFrom="1000"
    android:valueTo="0"
    android:valueType="floatType" />

in_from_right.xml

<?xml version="1.0" encoding="utf-8"?>

<objectAnimator
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500"
    android:propertyName="x"
    android:valueFrom="0"
    android:valueTo="1000"
    android:valueType="floatType" />

我的交易看起来像这样:

FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.in_from_left, R.anim.in_from_right, R.anim.in_from_left, R.anim.in_from_right);
transaction.add(R.id.container,myFragment,"tag");
transaction.commit();
于 2014-10-10T15:32:56.707 回答