使用 TransitionManager.go() 更改场景时看不到动画
Not seeing animations when using TransitionManager.go() to change scenes
尝试创建一个中间带有徽标的启动画面,然后向上移动以允许在屏幕上显示 EditText 和“开始”按钮。
我设置了 sceneA 和 sceneB 来分别提供开始和结束状态。场景之间的唯一区别是启动徽标在场景 A 中被限制在父级底部,而在场景 B 中被限制在 EditText 顶部。
这是主要代码 Activity。
@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的布局。如果我交换函数的顺序,则反之亦然(最终结果是 sceneB)。这里发生了什么?似乎第一种方法阻止了第二种方法,即使我没有看到任何转换发生。
使用的其他文件:
主布局文件:
<?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>
场景A:
<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>
场景B:
<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>
With this code in place as-is, the app will launch with everything in
its final position, no animation for the movement.
是因为你换戏太早了。在 onCreate
方法中,您的 view
(我的意思是 R.layout.main
)尚未测量和显示,android 稍后再做。您需要等到 android 创建并显示主视图(使用 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;
}
});
}
尝试创建一个中间带有徽标的启动画面,然后向上移动以允许在屏幕上显示 EditText 和“开始”按钮。
我设置了 sceneA 和 sceneB 来分别提供开始和结束状态。场景之间的唯一区别是启动徽标在场景 A 中被限制在父级底部,而在场景 B 中被限制在 EditText 顶部。
这是主要代码 Activity。
@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的布局。如果我交换函数的顺序,则反之亦然(最终结果是 sceneB)。这里发生了什么?似乎第一种方法阻止了第二种方法,即使我没有看到任何转换发生。
使用的其他文件:
主布局文件:
<?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>
场景A:
<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>
场景B:
<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>
With this code in place as-is, the app will launch with everything in its final position, no animation for the movement.
是因为你换戏太早了。在 onCreate
方法中,您的 view
(我的意思是 R.layout.main
)尚未测量和显示,android 稍后再做。您需要等到 android 创建并显示主视图(使用 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;
}
});
}