简体   繁体   中英

Android alpha animation not repainting during the animation

I have an activity with a slidingdrawer that comes up as a menu. when the activity starts the button is there and fades out to allow a full screen view. when the button on top of the drawer is clicked (it has alpha of 0 at this point) it should fade back in and the menu popup. when clicked again the menu collapses and the button fades back out. to do this I have the following code in the activity.

The initial fadeout works as expected. The activity is created and the button fades out. when I click the button to expand the menu the button does not appear and when I click it again the button is there and does not fade out. The odd thing is that the animation is in fact called and if I move the scrolviews behind the drawer while the animation after the animatin is called the animation does go on normally as long as I keep the views in its background moving. If I stop moving those views the animation will freeze at some alpha value.

Why does the first function normally while the others do not? How do I fix this?

drawer = (SlidingDrawer) this.findViewById(R.id.slidingDrawer1);
    drawer.setOnDrawerCloseListener(new OnDrawerCloseListener(){

        public void onDrawerClosed() {
            doFadeOut();
        }       
    });
    drawer.setOnDrawerOpenListener(new OnDrawerOpenListener(){

        public void onDrawerOpened() {
            doFadeIn();             
        }           
    });


fadeOut = AnimationUtils.loadAnimation(this, R.anim.buttonfadeout);
    fadeOut.setFillAfter(true);
    fadeIn = AnimationUtils.loadAnimation(this, R.anim.buttonfadein);
    fadeIn.setFillAfter(true);
    doFadeOut(); //this is the end of onCreate and fades when the activity is created

and these are methods in the activity

public void doFadeOut(){
    fadeOut.reset();
    menuButton.clearAnimation();
    menuButton.startAnimation(fadeOut);
}

public void doFadeIn(){
    fadeIn.reset();
    menuButton.clearAnimation();
    menuButton.startAnimation(fadeIn);
}

Have you implemented AnimationListener ?

You can use onAnimationEnd() . There you can put another listener which will trigger another alpha animation, for your button to fade back when you close the drawer.

I learned from my work with animations, that if you have more than one animation, you should always implement AnimationListener, because android cannot decide what to do with many of them on it's own.

Check out this documentation on Android Developers:

Animation Listener Documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM