支持库v4:27.1.0 片段自定义动画效果不如预期

9

使用support-v4:27.1.0时,Fragment动画无法正常工作。

getSupportFragmentManager()
       .beginTransaction()
       .setCustomAnimations(ENTER_ANIM , LEAVE_ANIM)
       .replace(R.id.main_activity_fragment_place_holder, fragment)
       .addToBackStack(tag)
       .commitAllowingStateLoss();

进入动画

<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="500" />

动画效果的离开
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="1.0" android:toAlpha="0.0"
android:duration="500" />

使用setCustomAnimations()设置进入和退出动画。 - Abdelstar Ahmed
3个回答

18

我刚刚遇到了同样的问题。支持库27.1.0似乎在使用alpha属性的anim转换方面存在问题。

我的印象是,转换引擎没有正确实现“fill-after”功能,因此片段alpha在片段被移除之前会迅速反弹回1。这会导致一个闪烁的效果,其中替换的片段短暂可见然后消失。

我解决了这个问题,切换到了animator转换。

即替换了我的/res/anim/fade_in.xml

<?xml version="1.0" encoding="utf-8"?>
<alpha
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="0"
    android:toAlpha="1"
    android:duration="500"
    />

使用类似的/res/animator/fade_in.xml

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:propertyName="alpha"
    android:valueFrom="0"
    android:valueTo="1"
    android:duration="500"
    />

我对淡出动画也做了同样的处理。


5
最新的支持库版本27.1.1已经修复了闪烁效果问题。(详见问题74051124)

1
应该是被接受的答案。我已经测试过了,在支持v27.1.1上不存在这个错误。 - Metehan Toksoy

3

在将支持库从27.0.2升级到27.1.0后,我遇到了完全相同的问题。与其平滑地淡化,片段会闪烁几次。

看起来所有动画器都能正常工作,除了alpha动画器。

然而,我已经找到了解决这个bug的方法:如果禁用进入动画,则转换仍然会淡化。它的淡化方式不完全与以前相同,但在我看来看起来很好(甚至更好)。

新的enter动画(我将其命名为nothing.xml)是:

<?xml version="1.0" encoding="utf-8"?>
<set/>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接