Search code examples
androidanimationandroid-animationtranslate

Android View animation to top parent


My target is to animate current view from it's current position(center), to top border of it's parent. Is that possible? I have already tried next code, but the problem is that my view disappears after animation:

Animation bottomUp = AnimationUtils.loadAnimation(context, R.anim.bottom_up);
popup.startAnimation(bottomUp);

and

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromYDelta="100%"
        android:toYDelta="0%"
        android:fillAfter="true"
        android:duration="500"/>
</set>

Solution

  • That can easily be done in Java with ViewPropertyAnimator

    popup.animate().translationY(0).setDuration(500); // 0 is top of Y axis
    

    The animation always starts at the current position.

    Check the ViewPropertyAnimator docs for future animations, it's very easy to use and can save you a lot of code.