-->

support-v4:27.1.0 fragment custom animations do no

2019-04-06 11:12发布

问题:

Fragment animations do not work properly with support-v4:27.1.0

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

enter animation

<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" />

leave animation

<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" />

回答1:

I just hit the same problem. Support library 27.1.0 seems to have a problem with anim transitions that use the alpha property.

My impression is that the transition engine does not correctly implement a "fill-after", so that the fragment alpha quickly bounces back to 1 before the fragment is removed. This causes a blinking effect where the replaced fragment is briefly visible and then disappears.

I resolved the issue switching to animator transitions.

I.e. replaced my /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"
    />

with an analogous /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"
    />

I did the same for the fade_out transition.



回答2:

The blinking effect was fixed in the latest support library version 27.1.1. (see issue 74051124)



回答3:

I have the exact same problem after upgrading the support library from 27.0.2 to 27.1.0. Instead of fading smoothly, the fragments blink several times.

It seems that all animators work as expected, except alpha animators.

However, I have found a workaround for this bug: If you disable the enter animation, the transition still fades. It does not fade in the exact same way as before, but it looks good (or even better) in my opinion.

The new enter animation (which I have named nothing.xml) is:

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