Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

support-v4:27.1.0 fragment custom animations do not work as expected

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" />
like image 379
Abdelstar Ahmed Avatar asked Mar 08 '18 18:03

Abdelstar Ahmed


3 Answers

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.

like image 86
Alessandro Mulloni Avatar answered Oct 17 '22 07:10

Alessandro Mulloni


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

like image 24
Thomas Vos Avatar answered Oct 17 '22 07:10

Thomas Vos


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/>
like image 3
Eirik W Avatar answered Oct 17 '22 09:10

Eirik W