7

I want to move my image view from its current position to some fixed position on screen using translate animation. Also I want to know how translate animation works and what parameters it accepts exactly?

My piece of code is...

       RelativeLayout.LayoutParams lParams = (LayoutParams) spreadImage
                .getLayoutParams();
       TranslateAnimation ta

        ta = new TranslateAnimation(lParams.leftMargin,
                randomLeftMarginsList.get(currentSpreadIndex),
                lParams.topMargin,

        ta.setAnimationListener(this);
        ta.setDuration(ApplicationConstant.PUZZLE_GAME_IMAGE_SPREADING_TIME);
        spreadImage.startAnimation(ta);

Thanks in advance.

3 Answers 3

18

From Androidexterous blog (2012):

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.

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

2)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.

Hope this will give you nice understanding.

3
  • Cool thing I read was that if you don't specify a fromX value, it'll use the current position of the view as the starting position. developer.android.com/guide/topics/graphics/…
    – marienke
    Feb 19, 2015 at 9:41
  • I have noticed that calling the view's getTop() method after doing the ObjectAnimation still returns the initial position of the view... The view is moved, the OnClickListener attached to it works correctly after the translation, but the layout params appear to be unchanged. Any idea why? How can I change the actual layout params after the object animation completes?
    – h_k
    Jan 27, 2016 at 15:42
  • Thank you for the note on the ObjectAnimator! I was using OnTouchListener and so, just Animation was not working; I was supposed to use ObjectAnimator. Jun 8, 2017 at 6:02
7

You just need to translate your view one position to another position. So need to use below code to achieve your task.

imgHeart.animate()
    .scaleXBy(-6f)
    .scaleYBy(-6f)
    .alpha(.1f)
    .translationX((heigthAndWidth[0] / 2) - minusWidth) // trying to make my location
    .translationY(-((heigthAndWidth[1] / 2) - minusHeight))
    .setDuration(1000)
    .start();
2

You could use NineOldAndroids. it has examples on translate animations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.