如何制作固定屏幕和新屏幕的幻灯片动画? [英] How to make slide animation with a fixed screen and new screen?
问题描述
我为过渡制作了三个 xml 文件.
I made three xml files for the transition.
enter_from_right.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@android:integer/config_mediumAnimTime"
android:fromXDelta="0%"
android:fromYDelta="0%"
android:toXDelta="0%"
android:toYDelta="0%" />
</set>
none.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@android:integer/config_mediumAnimTime"
android:fromXDelta="100%"
android:fromYDelta="0%"
android:toXDelta="0%"
android:toYDelta="0%" />
</set>
exit_to_right.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@android:integer/config_mediumAnimTime"
android:fromXDelta="0%"
android:fromYDelta="0%"
android:toXDelta="100%"
android:toYDelta="0%" />
</set>
但是,它在 Activities
中工作正常,而不在 Fragments
中.
However, It just works fine in Activities
not in Fragments
.
当我单击后退按钮时,向后过渡(pop)工作正常.但是当我调用新片段时它不能正常工作.它只是在屏幕变化时闪烁.
When I click the back button, the backward transition(pop) works fine. But it doesn't work properly when I call the new fragment. It just blinks when the screen changes.
我尝试将 none.xml
的持续时间更改为 50.我看到新屏幕从右侧进入.也试过10000.但它只是延迟了更改时间.
I tried changing the duration to 50 of none.xml
. And I see the new screen comes in from the right side. And also tried with 10000. But it just delays the changing time.
我正在使用导航组件.我是这样定义的:
I am using navigation component. And I defined like this:
<action
android:id="@+id/action_initFragment_to_settingFragment"
app:destination="@id/settingFragment"
app:enterAnim="@anim/enter_from_right"
app:exitAnim="@anim/none"
app:popExitAnim="@anim/exit_to_right"
app:popEnterAnim="@anim/none"/>
这是怎么回事?我认为这是因为 Z 索引.有没有办法给Z索引属性?
What's wrong with it? I think this is because of Z index. Is there any way to give Z index attribute?
推荐答案
发生这种情况是因为 z 索引.活动有不同的深度.但是,我猜 Fragments 具有相同的深度.因此,当 A Fragment 切换到 B Fragment 时,它们处于相同的深度并且过渡显示不正确.
This happened because of the z index. Activities have different depth. However, I guess Fragments have the same depth. So, When A Fragment is switched to B Fragment, They are on the same depth and transition is not shown properly.
解决方案是以编程方式为屏幕提供 Z 索引,如下所示:
The solution is giving Z index to the screen programmatically like below:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
ViewCompat.setTranslationZ(view, 1F)
}
1F
这里是深度的索引.
有趣的是,更高的值会产生更大的阴影.如果你给 100F,那么它会在下面产生巨大的阴影.当值为 1F 时,我看不到任何明显的阴影.
The interesting thing is the higher value creates the bigger shadow. If you give 100F, then it creates huge shadow underneath. And I can't see any shadows visibly when the value is 1F.
这篇关于如何制作固定屏幕和新屏幕的幻灯片动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!