Move ImageView from current position to fixed position using translational animation

I want to transfer the image from the current position to some fixed position on the screen using animation translation. Also I want to know how the translation animation works and what parameters does it accept exactly?

My piece of code ...

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.

+7
android translate-animation
source share
3 answers

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; //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); 

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.

+18
source share

You just need to transfer your opinion from one position to another position. Therefore, to complete your task you must use the code below.

 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(); 
+6
source share

You can use NineOldAndroids . he has examples for translating animations.

+2
source share

All Articles