How to animate FloatingActionButton of new Design Support Library

后端 未结 5 1642
梦谈多话
梦谈多话 2020-12-28 09:06

I am using a TabLayout with 5 different fragments. On 3 of these fragments a android.support.design.widget.FloatingActionButton should appear. Right now I simpl

5条回答
  •  粉色の甜心
    2020-12-28 09:41

    enter image description here

    You want something like this? But instead of animating it on onScrollListener you can animate it on onCreateView or onCreate method. Follow this --> Implement Floating Action Button – Part 2

    Basically the code sums up only to this

    Animate to Hide

    FloatingActionButton floatingActionButton = (FloatingActionButton) getActivity().findViewById(R.id.fab);
                    floatingActionButton.animate().translationY(floatingActionButton.getHeight() + 16).setInterpolator(new AccelerateInterpolator(2)).start();
    

    and

    Animate back to Show

    FloatingActionButton floatingActionButton = (FloatingActionButton) getActivity().findViewById(R.id.fab);
                    floatingActionButton.animate().translationY(0).setInterpolator(new DecelerateInterpolator(2)).start();
    

    but we dont want it to animate just to hide it so, 'Animate to Hide' will just be something like this

    FloatingActionButton floatingActionButton = (FloatingActionButton) getActivity().findViewById(R.id.fab);
                    floatingActionButton.setTranslationY(floatingActionButton.getHeight() + 16);
    

    On 'Animate to Hide' put that on the onCreateView or onCreate method so that on your FAB is hidden when you create this fragment and you could then add a handler and runnable that will trigger 'Animate back to Show' after a second or two to show your animation

    or you could use a time for short animations

    int mShortAnimationDuration = getResources().getInteger(
                android.R.integer.config_shortAnimTime);
    

    I tried this one on onScroll but haven't tried on onCreateView or onCreate but I guess it should work

    --EDIT--

    Try this code ;)

    public class DummyFragment extends Fragment {
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            int mShortAnimationDuration = getResources().getInteger(
                    android.R.integer.config_shortAnimTime);
    
            FloatingActionButton floatingActionButton = (FloatingActionButton) getActivity().findViewById(R.id.fab);
            floatingActionButton.setTranslationY(floatingActionButton.getHeight() + 16);
    
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    FloatingActionButton floatingActionButton = (FloatingActionButton) getActivity().findViewById(R.id.fab);
                    floatingActionButton.animate().translationY(0).setInterpolator(new DecelerateInterpolator(2)).start();
    
                }, mShortAnimationDuration);
            }
        }
    }
    

提交回复
热议问题