Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Crossfade animation between fragments or Activities

I would like to implement Crossfade animation between the fragments (Not fade-out/fade-in animation because it allows underlying Activity/fragment to be seen briefly between animation)

Android developers there are guides how to Crossfade views "Crossfading Two Views" but I'm not getting how that could be implemented to Crossfade between fragments or activities.

Somehow make custom animation and override the onCreateAnimator function or is it even possible??

like image 373
Juge Avatar asked Jan 08 '18 09:01

Juge


Video Answer


2 Answers

Try this for Activities(this can be put into some BaseActivity)

    @Override public void startActivity(Intent intent) {
        super.startActivity(intent);
        overridePendingTransition(R.anim.fade_in, R.anim.fade_out)
    }

    @Override public void finish() {
        super.finish();
        overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
    }

Where fade_in:

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
       android:duration="@integer/activity_transition_duration"
       android:fromAlpha="0.0"
       android:interpolator="@android:anim/accelerate_interpolator"
       android:toAlpha="1.0" />

and fade_out:

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
       android:duration="@integer/activity_transition_duration"
       android:fillAfter="true"
       android:fromAlpha="1.0"
       android:interpolator="@android:anim/accelerate_interpolator"
       android:toAlpha="0.0" />

For Fragments:

    FragmentTransaction transaction = supportFragmentManager.beginTransaction()

    transaction.setCustomAnimations(R.anim.fade_in, R.anim.fade_out,
                R.anim.fade_in, R.anim.fade_out)
like image 117
Demonick Avatar answered Oct 19 '22 00:10

Demonick


It's a little old question but I will add my version of the cross fading animation for fragments. @Demonick answer for fragments didn't work for me because I use a ViewPager for my fragments, and using FragmentTransaction is a lot more work from what I've seen.

private inner class FadeBetweenFragments : ViewPager.PageTransformer {

    private val MIN_SCALE = 0.1f
    private val MIN_ALPHA = 0.4f

    override fun transformPage(view: View, position: Float) {
        view.apply {
            alpha = when {
                position < -1 -> {
                    0f
                }
                position <= 1 -> {
                    val scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position))
                    (MIN_ALPHA +
                            (((scaleFactor - MIN_SCALE) / (1 - MIN_SCALE)) * (1 - MIN_ALPHA)))
                }
                else -> {
                    0f
                }
            }
        }
    }
}

I've added the below class in my activity where I have my viewPager. After implementing the class you need to add the following when you set your ViewPager :

viewPager.setPageTransformer(true, FadeBetweenFragments())
like image 26
HartWoom Avatar answered Oct 18 '22 22:10

HartWoom