Current time: 08-29-2014, 10:04 PM Hello There, Guest! (LoginRegister)


Post Reply 
 
Thread Rating:
  • 2 Votes - 4.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Translate animation in android example
09-25-2012, 08:01 PM (This post was last modified: 12-17-2012 03:37 PM by techsavvy.)
Post: #1
Translate animation in android example
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.
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: 2 Guest(s)

Contact Us | CreatioSoft Solutions Private Limited | Return to Top | Return to Content | Lite (Archive) Mode | RSS Syndication

We are everything you need to make your App idea Great!

Drop us your requirement/query and one of our business development expert will get in touch with you in 24 hours.

     Business :   +91120-4162752
    Career :     +91120- 4117620
    Sales :     sales@creatiosoft.com