JozeRi
JozeRi

Reputation: 3429

Android fragment animation first time stuck

I am doing an animation inside a fragment.

I have 2 views on top of each other, one of them set on View.GONE. when I press a button I want my 2nd fragment to translate animation from the bottom to top. I am doing it fine and it's working great, the problem is that in my first run, the xml view is gone, but he is in the same Y he is suppose to be. so the first animation I do isn't doing anything, just switch from GONE to VISIBLE, after that, I press dismiss and the fragment goes away and comes back just like I want too. my problem is just the first run. how can I set my view Y to be 100% below my screen?

here's the code I use :

private void moreCustomAnimation() {

    int yOffset = moreMenuFrameLayout.getMeasuredHeight();
    TranslateAnimation moveAnim = new TranslateAnimation(0, 0, yOffset, 0);
    moveAnim.setDuration(500);
    moveAnim.setFillAfter(true);
    blackView.setVisibility(View.VISIBLE);
    moreMenuFrameLayout.setVisibility(View.VISIBLE);
    moreMenuFrameLayout.startAnimation(moveAnim);

    moveAnim.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

        }
        @Override
        public void onAnimationEnd(Animation animation) {
        }
        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });
}

on the way out of the screen I use the same code just switch the yOffset to the other Y integer, and set the view to GONE at animation end.

thanks a lot in advance for any help !

Upvotes: 1

Views: 869

Answers (2)

Fox
Fox

Reputation: 2561

You can use the onGlobalLayout event to set the position of the view. Like this:

moreMenuFrameLayout.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        moreMenuFrameLayout.setTranslationY(moreMenuFrameLayout.getMeasuredHeight());
        moreMenuFrameLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
    }
});

This event happens when your views get their actual size and position before being drawn to the screen. However, they happen every time the view is drawn so you must remember to remove the listener right after the first time.

HTH

Upvotes: 1

droid kid
droid kid

Reputation: 7609

First time you can add the 'yOffset' value to the view orginal points

 moreMenuFrameLayout.setY(currentYpostition + yOffset) 

and this will place the view to the bottom of the screen. You can enable the visibility when the animation starts.

Upvotes: 0

Related Questions