translate animation in android example

September 25, 2012 | Rishabh Agrawal | General

Translate Animation controls the position and location of a layout or button or any view on which animation is being applied. It can move an object either in x direction or y direction.

Syntax :

TranslateAnimation transAnimation= new TranslateAnimation(fromXposition, toXPosition, fromYPosition, toYPosition);

fromXposition- x coordinate from  where animation should start

toXPosition- x coordinate at which animation would end

fromYPosition- y coordinate from where animation should start.

toYPosition- y coordinate at which animation would end.

 

If we want to translate only in X direction then we set fromYPosition and toYPosition as zero.

If we want to translate only in Y direction then we set fromXPosition and toXPosition as zero.

There is  another method in which we ,create an anim folder in the res folder. In this folder  we add our animation xml .We use a translate tag  in which we specify the attribute values.

In the below xml

android:duration defines the time of execution of animation

android:repeatCount specifies the no. of times the animation should be repeated ,

android:fromYDelta defines y coordinate from  where animation should start

android:toYDelta defines y coordinate at which animation would end.

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″ />

</set>

 

Code:

Animation lineTranslate;

//loading xml from anim folder

Animation localAnimation = AnimationUtils.loadAnimation(this, R.anim.line_translate);

//You can now apply the animation to a view

view.startAnimation(transAnimation);

 

Translate Animation can change the visual appearance of an object, but they cannot change the objects themselves. That is, if you apply a translate animation to a view, it would move to a new position but its click events would not get fired whereas the click events would still get fired at its previous position. This happens because the view is still at its original position. In order to overcome this, we can use ObjectAnimation which actually moves an object. Object Animation is the only animation which actually moves an object. You can create Translate animation using ObjectAnimator.

 

ObjectAnimator transAnimation= ObjectAnimator.ofFloat(view, propertyName, fromX, toX);

transAnimation.setDuration(3000);//set duration

transAnimation.start();//start animation

 

view  -this is the view on which animation is to be applied

propertyName-The property being animated.

FromX,toX-A set of values that the animation will animate between over time.


THE AUTHORRishabh Agrawal

Rishabh Agrawal is the founder of Creatiosoft, a company focused on creating high-quality software for the iGaming industry, specialising in poker and card games. With years of experience, Rishabh is dedicated to delivering engaging and user-friendly gaming experiences. Through this blog, he shares his passion and insights to help readers understand the latest trends and advancements in iGaming.

Recent Posts

The Future of Mobile Gaming: What to Expect in the Next 5 Years

Over the years, mobile gaming has seen amazing development and innovation utilized by the game development company in India. It…
06 Jun 2023 Rishabh Agrawal

How Do I Get Started With NFT Gaming?

The popularity of the NFT game has created a different fan base. There was a time when games were a…
18 Jul 2022 Rishabh Agrawal

What Are The Top Trends in NFT Marketplace 2022?

The NFTs are the new engaging and revolutionary technology across the globe. Though, these non-fungible tokens appeared for the first…
09 Jun 2022 Rishabh Agrawal