Animate visibility modes, GONE and VISIBLE

后端 未结 8 2099
我寻月下人不归
我寻月下人不归 2021-01-30 01:54

So im trying to animate when i set the visibility of a linearlayout with other widgets, from GONE to VISIBLE and the opposite.Im using togglebuttons to show and hide. Here\'s an

相关标签:
8条回答
  • 2021-01-30 02:45

    Like tomash said before: There's no easy way.

    You might want to take a look at my answer here.
    It explains how to realize a sliding (dimension changing) view.
    In this case it was a left and right view: Left expanding, right disappearing.
    It's might not do exactly what you need but with inventive spirit you can make it work ;)

    0 讨论(0)
  • 2021-01-30 02:45

    This is a quite old question, still comments show, that still people have problems, so here is my solution with following additional features:

    • adjust the animation (speed, type, ...)
    • does not need to extend any class
    • in my case, animateLayoutChanges has problems in the new CoordinatorLayout

    Function - Example (I have this function in an utility class)

    public static void animateViewVisibility(final View view, final int visibility)
    {
        // cancel runnning animations and remove and listeners
        view.animate().cancel();
        view.animate().setListener(null);
    
        // animate making view visible
        if (visibility == View.VISIBLE)
        {
            view.animate().alpha(1f).start();
            view.setVisibility(View.VISIBLE);
        }
        // animate making view hidden (HIDDEN or INVISIBLE)
        else
        {
            view.animate().setListener(new AnimatorListenerAdapter()
            {
                @Override
                public void onAnimationEnd(Animator animation)
                {
                    view.setVisibility(visibility);
                }
            }).alpha(0f).start();
        }
    }
    

    Adjust animation

    After calling view.animate() you can adjust the animation to whatever you want (set duration, set interpolator and more...). You may as well hide a view by scaling it instead of adjusting it's alpha value, just replace the alpha(...) with scaleX(...) or scaleY(...) in the utility method if you want that

    0 讨论(0)
提交回复
热议问题