[英]Animate fragment from bottom up, over top previous fragment
I'd like to animate in a new fragment from the bottom up, over top of the previous fragment. 我想从上至下在上一个片段的上方制作一个新的片段。
Unfortunately, my current implementation pushes the previous fragment up, as the new fragment is animating in, like this: 不幸的是,随着新片段的动画化,我当前的实现将上一个片段向上推,如下所示:
https://i.imgur.com/IgMUamp.png https://i.imgur.com/IgMUamp.png
What I would like it to do is have the new fragment animate over top the previous fragment, like so: 我想要做的是使新片段在之前的片段上方进行动画处理 ,如下所示:
https://i.imgur.com/DTrEfcm.png https://i.imgur.com/DTrEfcm.png
Here is what my code looks like now: 这是我的代码现在的样子:
private void pushFragment(String fragmentTag, Fragment fragment) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.anim_in_test, R.anim.anim_out_test);
fragmentTransaction.replace(
R.id.fragment_container,
fragment,
fragmentTag);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commitAllowingStateLoss();
}
Here is anim_in_test.xml
: 这是
anim_in_test.xml
:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator">
<translate
android:duration="@android:integer/config_longAnimTime"
android:fromYDelta="100%p"
android:toYDelta="0" />
</set>
And here is anim_out_test.xml
: 这是
anim_out_test.xml
:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@android:integer/config_longAnimTime"
android:fromYDelta="0%p"
android:toYDelta="-100%p"
/>
</set>
What do I need to change to get this working right? 我需要更改什么才能使其正常工作?
setCustomAnimations(int enter, int exit)
Exit animation should be empty (because you need old fragment stay on screen while new one appears), but if you pass 0 to second param view dissapear without animation. 退出动画应该为空(因为您需要在显示新片段时将旧片段留在屏幕上),但是如果您将0传递给第二个参数视图,则不显示动画。 Thats why I add empty alpha animation which changes alpha from 1 to 1.
那就是为什么我添加空的Alpha动画,它将Alpha从1更改为1。
fragmentTransaction.setCustomAnimations(R.anim.anim_in_test, R.anim.anim_out_test);
anim_out_test.xml anim_out_test.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:duration="@android:integer/config_longAnimTime"
android:fromAlpha="1"
android:toAlpha="1" />
</set>
But at that point it doesnt' work because android draw and animate new fragment BELOW old fragment. 但是在那一点上,它是行不通的,因为android在旧片段的下方绘制和动画化新片段。 You can see it if you make fragment backgrounds transparent.
如果使片段背景透明,则可以看到它。
To resolve this issue I use setZ(z)
, setElevation(z)
or setTranslationZ(z)
methods on root view of fragment. 要解决此问题,我在片段的根视图上使用
setZ(z)
, setElevation(z)
或setTranslationZ(z)
方法。 It looks like all of them are working. 似乎所有人都在工作。
Z
value of new fragment should be more than old fragment Z
. 新片段的
Z
值应大于旧片段Z
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.view, container, false);
view.setZ(value);
Here is result: 结果如下:
It works but I dont know side effects of this solution. 它有效,但我不知道此解决方案的副作用。 On android 4 I think it can be done via custom
ViewGroup
as fragment container, which draws children in reverse order. 在Android 4上,我认为可以通过自定义
ViewGroup
作为片段容器来完成,它以相反的顺序绘制子代。
只需使用添加片段而不是替换片段
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.