当使用TransitionManager.go()改变场景时,看不到动画。

问题描述 投票:-1回答:1

试图创建一个中间有logo的闪屏,然后向上移动,让屏幕上出现EditText和Start按钮。

我设置了场景A和场景B,分别提供开始和结束状态。场景之间唯一的区别是,在场景A中,溅射标志被限制在父级底部,而在场景B中则限制在EditText顶部。

这里是主活动的代码。


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ViewGroup sceneRoot = findViewById(R.id.scene_root);
        Scene sceneA = Scene.getSceneForLayout(sceneRoot, R.layout.scene_a, this);
        Scene sceneB = Scene.getSceneForLayout(sceneRoot, R.layout.scene_b, this);

        TransitionManager.go(sceneB);

        editText = findViewById(R.id.editText);
        button = findViewById(R.id.button);

        editText.setVisibility(View.VISIBLE);
        editText.setAlpha(0f);
        editText.animate()
                .alpha(1f)
                .setDuration(animDuration)
                .setListener(null);

        button.setVisibility(View.VISIBLE);
        button.setAlpha(0f);
        button.animate()
                .alpha(1f)
                .setDuration(animDuration)
                .setListener(null);
    }

在这段代码的基础上,应用程序将以其最终位置启动,没有运动的动画。editText和Button的可见性动画按照预期工作。

为什么场景转换动画没有触发?我试着将我自己的过渡动画传递给TransitionManager,但没有任何变化。似乎动画没有触发,即使场景变化似乎正在发生。

此外,奇怪的是,如果我这样做。

TransitionManager.go(sceneA);
TransitionManager.go(sceneB);

最后的结果是场景A的布局。反之,如果我交换函数的顺序(最终结果是场景B)。这里到底发生了什么?似乎第一种方法阻止了第二种方法,尽管我没有看到任何转换发生。

使用的附加文件。

Layout主文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mainMenuMasterLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:animateLayoutChanges="true"
    tools:context=".main">

    <FrameLayout
        android:id="@+id/scene_root"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include layout="@layout/scene_a" />
    </FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

sceneA:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/mainSceneContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/splashLogo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/logo_transparent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="invisible"
        app:layout_constraintTop_toBottomOf="@+id/splashLogo"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/button"
        />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="invisible"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/editText" />

</androidx.constraintlayout.widget.ConstraintLayout>

sceneB:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/mainSceneContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/splashLogo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/logo_transparent"
        app:layout_constraintBottom_toTopOf="@id/editText"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="invisible"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="invisible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/editText" />

</androidx.constraintlayout.widget.ConstraintLayout>
android android-animation android-transitions
1个回答
0
投票

在这个代码原封不动的情况下,应用程序将在所有东西都在其最终位置上启动,没有移动的动画。

发生这种情况是因为你过早地改变了场景。在 onCreate 办法 view (我的意思是 R.layout.main)还没有测量和显示,安卓系统会晚很多。你需要等到安卓系统创建并显示主视图(用 sceneA),之后才开始过渡到 sceneB.有 OnPreDrawListener 当视图被测量并即将绘制时调用。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...

    View view = LayoutInflater.from(this).inflate(R.layout.main, null);
    setContentView(view);

    view.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            view.getViewTreeObserver().removeOnPreDrawListener(this);

            TransitionManager.go(sceneB);

            return true;
        }
    });
}
© www.soinside.com 2019 - 2024. All rights reserved.