Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I animate the new Floating Action Button between tab/fragment transition?

From the new Design Support Library, I would like to animate the Floating Action Button(FAB) to shrink and expand as the fragment or tab changed.

enter image description here

I tried several kinds of transition for a while but couldn't get as smooth and connected as the example from this so I think there are better approach or a proper way to do this.

like image 207
IndyZa Avatar asked Jul 09 '15 13:07

IndyZa


2 Answers

In the ViewPager.OnPageChangeListener() I will use btn.setScaleY() and btn.setScaleX() depending on the offset of the onPageScrolled() method.

like image 169
Chol Avatar answered Sep 24 '22 12:09

Chol


In you Activity class, add addOnPageChangeListener in your ViewPager. see below :

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            private int state = 0;
            private boolean isFloatButtonHidden = false;
            private int position = 0;

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                if (isFloatButtonHidden == false && state == 1 && positionOffset != 0.0) {
                    isFloatButtonHidden = true;

                    //hide floating action button
                    swappingAway();
                }
            }

            @Override
            public void onPageSelected(int position) {
                //reset floating
                this.position = position;

                if (state == 2) {
                    //have end in selected tab
                    isFloatButtonHidden = false;
                    selectedTabs(position);

                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                //state 0 = nothing happen, state 1 = begining scrolling, state 2 = stop at selected tab.
                this.state = state;
                if (state == 0) {
                    isFloatButtonHidden = false;
                } else if (state == 2 && isFloatButtonHidden) {
                    //this only happen if user is swapping but swap back to current tab (cancel to change tab)
                    selectedTabs(position);
                }

            }
        });

in your private method, you can do custom tasks there.

private void swappingAway()
{
    floatingActionButton.clearAnimation();
    Animation animation = AnimationUtils.loadAnimation(this, R.anim.pop_down);
    floatingActionButton.startAnimation(animation);
}

private void selectedTabs(int tab)
{
    floatingActionButton.show();

    //a bit animation of popping up.
    floatingActionButton.clearAnimation();
    Animation animation = AnimationUtils.loadAnimation(this, R.anim.pop_up);
    floatingActionButton.startAnimation(animation);

    //you can do more task. for example, change color for each tabs, or custom action for each tabs.
}

I intentionally don't use hide() and show(), as it has bugs, when you swap away and go back right away.

here's the anim of pop_down and pop_up

pop_down.xml

<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXScale="1.0"
    android:fromYScale="1.0"
    android:toXScale="0.0"
    android:toYScale="0.0"
    android:duration="100"
    android:pivotX="50%"
    android:pivotY="50%"
    android:shareInterpolator="true"
    android:fillAfter="true">
</scale>

pop_up.xml

<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXScale="0.0"
    android:fromYScale="0.0"
    android:toXScale="1.0"
    android:toYScale="1.0"
    android:duration="500"
    android:pivotX="50%"
    android:pivotY="50%"
    android:startOffset="200"
    android:interpolator="@android:anim/bounce_interpolator"
    android:shareInterpolator="true"
    android:fillAfter="true">
</scale>
like image 25
HelmiB Avatar answered Sep 25 '22 12:09

HelmiB