Android: Expand/collapse animation

后端 未结 30 2649
说谎
说谎 2020-11-22 05:01

Let\'s say I have a vertical linearLayout with :

[v1]
[v2]

By default v1 has visibily = GONE. I would like to show v1 with an expand animat

30条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 05:44

    This is really simple with droidQuery. For starts, consider this layout:

    
        
            
        
        
            
            
        
    
    

    We can animate the height to the desired value - say 100dp - using the following code:

    //convert 100dp to pixel value
    int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics());
    

    Then use droidQuery to animate. The simplest way is with this:

    $.animate("{ height: " + height + "}", new AnimationOptions());
    

    To make the animation more appealing, consider adding an easing:

    $.animate("{ height: " + height + "}", new AnimationOptions().easing($.Easing.BOUNCE));
    

    You can also change the duration on AnimationOptions using the duration() method, or handle what happens when the animation ends. For a complex example, try:

    $.animate("{ height: " + height + "}", new AnimationOptions().easing($.Easing.BOUNCE)
                                                                 .duration(1000)
                                                                 .complete(new Function() {
                                                                     @Override
                                                                     public void invoke($ d, Object... args) {
                                                                         $.toast(context, "finished", Toast.LENGTH_SHORT);
                                                                     }
                                                                 }));
    

提交回复
热议问题