Android: Expand/collapse animation

后端 未结 30 2510
说谎
说谎 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条回答
  •  滥情空心
    2020-11-22 05:41

    This was my solution, my ImageView grows from 100% to 200% and return to his original size, using two animation files inside res/anim/ folder

    anim_grow.xml

    
    
     
    
    

    anim_shrink.xml

    
    
     
    
    

    Send an ImageView to my method setAnimationGrowShrink()

    ImageView img1 = (ImageView)findViewById(R.id.image1);
    setAnimationGrowShrink(img1);
    

    setAnimationGrowShrink() method:

    private void setAnimationGrowShrink(final ImageView imgV){
        final Animation animationEnlarge = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.anim_grow);
        final Animation animationShrink = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.anim_shrink);
    
        imgV.startAnimation(animationEnlarge);
    
        animationEnlarge.setAnimationListener(new AnimationListener() {         
            @Override
            public void onAnimationStart(Animation animation) {}
    
            @Override
            public void onAnimationRepeat(Animation animation) {}
    
            @Override
            public void onAnimationEnd(Animation animation) {
                imgV.startAnimation(animationShrink);
            }
        });
    
        animationShrink.setAnimationListener(new AnimationListener() {          
            @Override
            public void onAnimationStart(Animation animation) {}
    
            @Override
            public void onAnimationRepeat(Animation animation) {}
    
            @Override
            public void onAnimationEnd(Animation animation) {
                imgV.startAnimation(animationEnlarge);
            }
        });
    
    }
    

提交回复
热议问题