Android: Animation Position Resets After Complete

Reset view to original position after animation

clearAnimation(); does not reset your animations, it just stops them and removes them from the animation queue. To undo your animations you need to actually undo them. So, for your code block you will need to call rl2.animate().translationX(0).translationY(0).setDuration(2000); to move the view back to its original position.

Android: Animation Position Resets After Complete

The animations are working as expected. Just because you animated something does not mean you actually moved it. An animation only affects drawn pixels during the animation itself, not the configuration of the widgets.

You need to add an AnimationListener, and in the onAnimationEnd() method, do something that makes your move permanent (e.g., removes the view on top from its parent, marks the view on top as having visibility GONE).

Android TranslateAnimation resets after animation

Here is the actual bug related to this issue

This basically states that the onAnimationEnd(...) method doesn't really work well when an AnimationListener is attached to an Animation

The workaround is to listen for the animation events in the view to which you were applying the animation to For example if initially you were attaching the animation listener to the animation like this

mAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationEnd(Animation arg0) {
//Functionality here
}

and then applying to the animation to a ImageView like this

mImageView.startAnimation(mAnimation);

To work around this issue, you must now create a custom ImageView

public Class myImageView extends ImageView {

and then override the onAnimationEnd method of the View class and provide all the functionality there

@Override
protected void onAnimationEnd() {
super.onAnimationEnd();
//Functionality here
}

This is the proper workaround for this issue, provide the functionality in the over-riden View -> onAnimationEnd(...) method as opposed to the onAnimationEnd(...) method of the AnimationListener attached to the Animation.

This works properly and there is no longer any flicker towards the end of the animation. Hope this helps

ImageView resets position after another view changes in RelativeLayout

I'm using an objectanimator which is supposed to keep the position of the object at the end of the animation. The only thing that worked was setting the margins of the view at the end of the animation as you can see in the code below:

@Override
public void onAnimationEnd(Animation animation) {
runOnUiThread(new Runnable() {
public void run() {
submarine.clearAnimation();
LayoutParams lp = new LayoutParams(submarine.getWidth(), submarine.getHeight());
lp.setMargins(submarine.getLeft(), submarine.getTop()+50, 0, 0);
submarine.setLayoutParams(lp);
}
});

If you try positioning the item by using the setTop and setLeft the view will reset to it's original position. Another note is that you need to run the clearAnimation method of the view before positioning it otherwise the view will flicker when the animation ends.

Android Kotlin - reset position after animation

When you call animate on a View, it returns the ViewPropertyAnimator instance associated with that View - which is a singleton, i.e. there's only one and you get that back each time you call animate

This class is not constructed by the caller, but rather by the View whose properties it will animate. Calls to View.animate() will return a reference to the appropriate ViewPropertyAnimator object for that View.

So you call animate and save the ViewPropertyAnimator result as anim, and then set the animation listener on that. Your listener has its onAnimationEnd function, which starts a new animation on the same view, which means it's using the same ViewPropertyAnimator (anim), which has the listener set which starts a new animation when this one ends... see where I'm going here? You got yourself a never ending loop!

Your best bet is probably to use a one-shot end animation, which you can do with that fluent animate syntax anyway! No need for a listener. Try this:

anim = swipe.animate().rotationBy(-30f).setDuration(1000) // SWIPE TO LEFT
.translationX(-distance)
.setInterpolator(AccelerateDecelerateInterpolator())
.withEndAction {
swipe.animate().rotationBy(30f).setDuration(300)
.translationX(distance)

.setInterpolator(AccelerateDecelerateInterpolator()).start()
}

Can't fix the formatting on there (for a site about programming it's really hard to work with code in these answers) but yeah - you could clean it up by making some of it into a function anyway (since it's the same thing, just with negative values)


Also you should probably make anim a lateinit var instead of nullable and doing !! everywhere, that's a bad sign and it always causes troubles in the end!

Android : Get new position of View after animation

try getLocationOnScreen method.

int[] locationOnScreent = new int[2];
yourView.getLocationOnScreen(locationOnScreent);
// locationOnScreent[0] = x
// locationOnScreent[1] = y

Keep animated View at final Animation Position

Please consider setFillAfter(boolean); setting it to true will make the view stay at the animated position.

Animation anim = new TranslateAnimation(0, 0, 0, 100);

// or like this
Animation anim = AnimationUtils.loadAnimation(this, R.anim.animation_name);

anim.setFillAfter(true);
anim.setDuration(1000);

This can be done even easier using the ViewPropertyAnimator, available from API 14 and onwards:

int animationpos = 500;
View.animate().y(animationpos).setDuration(1000); // this will also keep the view at the animated position

Or consider an AnimationListener to get the LayoutParams exactly after the animation:

anim.setAnimationListener(new AnimationListener() {

@Override
public void onAnimationStart(Animation animation) {}

@Override
public void onAnimationRepeat(Animation animation) {}

@Override
public void onAnimationEnd(Animation animation) {
// get layoutparams here and set it in this example your views
// parent is a relative layout, please change that to your desires

RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) View.getLayoutParams();
lp.topMargin = yournewtopmargin // use topmargin for the y-property, left margin for the x-property of your view
View.setLayoutParams(lp);
}
});

View.startAnimation(anim);

UPDATE:

How to know how many pixels the view moved, depending on the
percentage values of the animation?

The percentage value in your .xml animation is relative to the animated View's dimensions (such as width and height). Therefore, you just need to use your View's width and height properties (depending on weather u use x or y animation to get the actual animation distance in pixels).

For example:

Your View has a width of 400 pixels. Your animation animates the x-property from 0% to 75%, meaning that your view will move 300 pixels (400 * 0.75) to the right side. So onAnimationEnd(), set the LayoutParams.leftMargin to 300 and your View will have the desired position.



Related Topics



Leave a reply



Submit