Translation Animation controls the location and layout of a layout or button, or any view that uses animation. It can move an object either in the x direction or in the y direction.
Syntax:
TranslateAnimation transAnimation= new TranslateAnimation(fromXposition, toXPosition, fromYPosition, toYPosition);
fromXposition - x coordinate with which animation should begin
toXPosition - x coordinate at which the animation ends
fromYPosition - the y coordinate from which the animation should start.
toYPosition - the y coordinate at which the animation ends.
1) If we want to translate only in X direction , then we set fromYPosition and toYPosition to zero.
2) If we want to translate only in Y direction , then we set fromXPosition and toXPosition to zero.
There is another method in which we create an animation folder in the res folder. In this folder we add our animated xml. We use the translate tag in which we specify the attribute values.
In the below xml
android:duration determines animation run time
android:repeatCount indicates the value no. once the animation has to be repeated
android:fromYDelta determines the y coordinate from which the animation should start
android:toYDelta determines the y coordinate at which the animation ends.
line_translate.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="300β³ android:repeatCount="1 android:fromYDelta="0.0β³ android:toYDelta="174.0β³ />
The code:
Animation lineTranslate;
Translation Animations can change the visual appearance of an object, but they cannot change the objects themselves. That is, if you apply the translation animation to the view, it will move to a new position, but click events will not be triggered, while click events will still be fired at the previous position. This is because the view is still in its original position.
To overcome this, we can use ObjectAnimation , which actually moves the object. Object animation is the only animation that actually moves an object. You can create cast animations using ObjectAnimator .
ObjectAnimator transAnimation= ObjectAnimator.ofFloat(view, propertyName, fromX, toX); transAnimation.setDuration(3000);//set duration transAnimation.start();//start animation
view is the view on which animation should be applied
propertyName - Animation of a property.
FromX, toX . The set of values ββthat the animation will animate between them.
Hope this will give you a good understanding.