简体   繁体   English

从上至下对片段进行动画处理

[英]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.

相关问题 对活动中的imageview进行动画处理,然后从底部将片段带入活动中:Android - Animate an imageview out of the activity and bring a fragment into the activity from the bottom: Android 从片段到上一个片段的后退箭头按钮 - Back arrow button from fragment to previous fragment 如何在旧片段上滑动时为 Android 导航架构片段设置动画? - How to animate Android Navigation Architecture fragment as sliding over old fragment? 从片段内部打开片段而没有出现以前的片段UI - Open fragment from inside fragment without previous fragment UI appearing 从活动移至上一个片段 - Moving to previous fragment from activity 通过底部导航视图通过活动将价值从一个片段传递到另一个片段 - Pass value from fragment to fragment through activity with bottom navigation view 如何从底部工作表导航片段移动到其他片段 - How to move to other fragment from Bottom Sheet Navigation Fragment 如何从底部导航栏打开本身在片段中的片段? - How to open a fragment from bottom navigation bar which is itself in a fragment? 从以前的片段视图切换到片段视图时,片段视图为空白 - fragment view is blank when I switch to from previous fragment view 如何从片段返回到前一个片段? - How do I get back from a fragment to a previous fragment?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM