无法使用 ConstraintSet 设置动画:plop.xml plop_alt.xml

Not able to animate with ConstraintSet : plop.xml plop_alt.xml

我是 ConstraintLayout 的新手,我正在尝试制作一个简单的动画:

一个按钮,一个文本,当我点击按钮时,文本从屏幕顶部转到屏幕底部。如果你再次点击相同的东西但相反。请不要关心重构的可能性,我只是想做一些有用的事情。似乎没有动画效果。

我的XML:

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorWhite"
    android:clickable="true"
    android:focusable="true"
    android:id="@+id/merde">

    <android.support.v7.widget.AppCompatTextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="60dp"
        android:text="Hello"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.v7.widget.AppCompatButton
        android:id="@+id/plop_button"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

还有我的替补XML:

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorWhite"
    android:clickable="true"
    android:focusable="true"
    android:id="@+id/merde">

    <android.support.v7.widget.AppCompatTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="60dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:text="Hello"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:id="@+id/textview"/>

    <android.support.v7.widget.AppCompatButton
        android:id="@+id/plop_button"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

还有我的控制器:

public class Plop extends Controller {

    @BindView(R.id.plop_button)
    AppCompatButton button;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.plop, container, false);
        ButterKnife.bind(this , view);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    ChangeBounds transition = new ChangeBounds();
                    ConstraintLayout controllersLoadingRootConstaintLayout = (ConstraintLayout) getLayoutInflater().inflate(R.layout.plop,null);
                    ConstraintSet constraintSetAlt = new ConstraintSet();

                    transition.setDuration(1000);

                    TransitionManager.beginDelayedTransition(controllersLoadingRootConstaintLayout, transition);

                    ConstraintLayout c1 = (ConstraintLayout) getLayoutInflater().inflate(R.layout.plop_alt,null);
                    constraintSetAlt.clone(c1);
                    constraintSetAlt.applyTo(controllersLoadingRootConstaintLayout);

            }
        });

        return view;
    }

    @Override
    public void save() {

    }

    @Override
    public void loadData() {

    }

    @Override
    public void updateView() {

    }
}

我做错了什么?

编辑:由于 plaskoff,上述问题已解决。

我现在正在尝试 activity :

在我的 onCreated 中:

protected void onCreate(@Nullable final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    setContentView(R.layout.controllers_loading_root);
    ChangeBounds transition = new ChangeBounds();
    ConstraintLayout controllersLoadingRootConstaintLayout = (ConstraintLayout)findViewById(R.id.controllers_loading_root_constraint_layout);
    ConstraintSet constraintSetAlt = new ConstraintSet();
    transition.setDuration(1000);TransitionManager.beginDelayedTransition(controllersLoadingRootConstaintLayout, transition);
    ConstraintLayout c1 = (ConstraintLayout) getLayoutInflater().inflate(R.layout.controllers_loading_root_alt,null);
    constraintSetAlt.clone(c1);
    constraintSetAlt.applyTo(controllersLoadingRootConstaintLayout);
    ...
}

我的 activity 出现在没有动画完成的最终状态。

仍然没有足够的代表发表评论,所以作为答案 (hmpf):
首先,alt 布局中的 TextView 没有 ID,不确定这是否是问题的一部分,只是想指出来。
其次,您将克隆内容放在 onClick 中。您应该按照 official doc 中的说明将其放入 onCreateView(例如),然后在 onClick 中使用 applyTo。

问题是您使用了错误的 ConstraintLayout 实例来制作动画:

ConstraintLayout controllersLoadingRootConstaintLayout = (ConstraintLayout) getLayoutInflater().inflate(R.layout.plop,null)

您正在膨胀一个新的 ConstraintLayout 对象,该对象与当前显示的对象不同。所以你应该做的是将上面的行替换为:

ConstraintLayout controllersLoadingRootConstaintLayout = (ConstraintLayout) view.findViewById(R.id.merde);

附带说明一下,您不必实例化新的 ConstraintLayout 对象来克隆约束。您可以使用 void clone (Context context, int constraintLayoutId) 从布局文件中克隆它们。

编辑以回答后续问题:

如果您想在显示 activity 时立即启动动画,您可以将动画代码移至 activity 中的 onWindowFocusChanged() 方法:

@Override
protected void onCreate(@Nullable final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.controllers_loading_root);
}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    if(hasFocus){
        ChangeBounds transition = new ChangeBounds();
        ConstraintLayout controllersLoadingRootConstaintLayout = (ConstraintLayout)findViewById(R.id.controllers_loading_root_constraint_layout);
        ConstraintSet constraintSetAlt = new ConstraintSet();
        transition.setDuration(1000);
        TransitionManager.beginDelayedTransition(controllersLoadingRootConstaintLayout, transition);
        ConstraintLayout c1 = (ConstraintLayout) getLayoutInflater().inflate(R.layout.controllers_loading_root_alt,null);
        constraintSetAlt.clone(c1);
        constraintSetAlt.applyTo(controllersLoadingRootConstaintLayout);
    }
}