How to make animation using ObjectAnimator?

454 Views Asked by At

My application is used in landscape orientation. What I want to do is move an element from off the screen at all until it enters the screen and then when pressing a button, it leaves the screen again by scrolling to the left.enter image description here

2

There are 2 best solutions below

2
Rob Gas On

All you need is a TranslationX animation, see below, or check out this link. https://developer.android.com/training/animation/reposition-view

ObjectAnimator translateX = ObjectAnimator.ofFloat(view, "translationX", deltaX);
ObjectAnimator translateY = ObjectAnimator.ofFloat(view, "translationY", deltaY);

If you have any question, please feel free.:)

0
MehranB On

The animation starts from the default position of the view (i.e the position you have specified in your xml file). if you do not what the animation to start from that position, you can either:

  1. change the initial position of the view in the xml file.

  2. or set the x and y coordinates of the view at run time and then start your animation like so:

ObjectAnimator translateX = ObjectAnimator.ofFloat(view, "translationX", deltaX);