Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android, setVisibility/animation issue

I have a linearLayout that disappears when I hit a button, it comes back when I press the button again. But it does it so fast, it doesn't look nice. I do this via:

disappearView.setVisibility(View.GONE);

I would like to add some animation... If I just set visibity to invisible the space where the layout was is still there. So I tried this:

if (disappearView.getVisibility() == View.VISIBLE){
            Animation out = AnimationUtils.makeOutAnimation(this, true);
            disappearView.startAnimation(out);
            disappearView.setVisibility(View.INVISIBLE);
            disappearView.setVisibility(View.GONE);

        }
        else {
            Animation in = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
            disappearView.startAnimation(in);
            disappearView.setVisibility(View.VISIBLE);      
        }

This does the animation too fast and disappears. You can't see it at all. Do I need to use a thread to start gone after invisible is set...or a delay? Or is there a better way of doing all this?

like image 954
Paul Avatar asked Jan 15 '13 16:01

Paul


People also ask

What is crossfade animation?

Crossfade animations (also know as dissolve) gradually fade out one UI component while simultaneously fading in another. This animation is useful for situations where you want to switch content or views in your app. Crossfades are very subtle and short but offer a fluid transition from one screen to the next.

Is animation possible in Android?

On Android 4.4 (API level 19) and higher, you can use the transition framework to create animations when you swap the layout within the current activity or fragment. All you need to do is specify the starting and ending layout, and what type of animation you want to use.

How do I animate a view in Android?

You can use the view animation system to perform tweened animation on Views. Tween animation calculates the animation with information such as the start point, end point, size, rotation, and other common aspects of an animation.


2 Answers

I'm not sure exactly what you're trying to accomplish...do you want the LinearLayout to fade out over a little bit of time rather than instantly disappear? And then once it fades out be removed from the parent via View.GONE?

If so, you can use an AlphaAnimation for the fade out and then attach a listener like EvZ posted:

AlphaAnimation fadeOutAnimation = new AlphaAnimation(1, 0); // start alpha, end alpha
fadeOutAnimation.setDuration(1000); // time for animation in milliseconds
fadeOutAnimation.setFillAfter(true); // make the transformation persist
fadeOutAnimation.setAnimationListener(new AnimationListener() {         
    @Override
    public void onAnimationEnd(Animation animation) {
        linearLayout.setVisibility(View.GONE);
    }

    @Override
    public void onAnimationRepeat(Animation animation) { }

    @Override
    public void onAnimationStart(Animation animation) { }
});

linearLayout.setAnimation(fadeOutAnimation);
like image 100
Ben Jakuben Avatar answered Oct 07 '22 09:10

Ben Jakuben


You can try to use onAnimationEnd : Animation.AnimationListener

like image 43
EvZ Avatar answered Oct 07 '22 09:10

EvZ