How to Animate a View in Android and Have It Stay in The New Position/Size

How to animate a view in Android and have it stay in the new position/size?

Make sure you add below attributes to the root element in your animation xml:

android:fillAfter="true" 
android:fillEnabled="true"

How to animate an imageview up to the another view and again back to original position

Finally, I found my answer with the help of all the comments and links provided in that. I don't know whether it is the ideal solution or not but it is working. If anyone has a better solution please provide that.

What I did for getting the position of the button (which was 0 before the solution).

btnScratch.post(new Runnable() {
@Override
public void run() {
float center = (btnScratch.getTop() + btnScratch.getBottom()) / 2;
Log.e("center point", String.valueOf(center));
animateUpToButton(center - imgHand.getTop());
}
});

for getting center of the button I used following line:

float center = (btnScratch.getTop() + btnScratch.getBottom()) / 2;

and for animation :

private void animateUpToButton(final float distance) {

ObjectAnimator imageAnimator = ObjectAnimator.ofFloat(imgHand, "translationY", 0f, distance);
imageAnimator.setDuration(ANIM_DURATION);
imageAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
imageAnimator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {

}

@Override
public void onAnimationEnd(Animator animator) {
animateBackToOrigin(distance);
}

@Override
public void onAnimationCancel(Animator animator) {

}

@Override
public void onAnimationRepeat(Animator animator) {

}
});
imageAnimator.start();
}

And, onAnimationEnd(), I added another animation to get the image back to the initial position.

private void animateBackToOrigin(final float distance) {
ObjectAnimator imageAnimator = ObjectAnimator.ofFloat(imgHand, "translationY", distance, 0f);
imageAnimator.setDuration(ANIM_DURATION);
imageAnimator.setInterpolator(new LinearInterpolator());
imageAnimator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {

}

@Override
public void onAnimationEnd(Animator animator) {
animateUpToButton(distance);
}

@Override
public void onAnimationCancel(Animator animator) {

}

@Override
public void onAnimationRepeat(Animator animator) {

}
});
imageAnimator.start();
}

and again onAnimationEnd(), I added animation to move image towards the button. this way, the animation will continue one after another.

Android - Animate a view and change his real position (and another view follow with behavior dependency)

I finally found the solution:

on my viewDragHelper I overrode the following methods:

        @Override
public int getViewVerticalDragRange(View child) {
return getMeasuredHeight() - child.getMeasuredHeight();
}

@Override
public void onViewReleased(View releasedChild, float xvel, float yvel) {
super.onViewReleased(releasedChild, xvel, yvel);
if(yvel>0) {
mDragHelper.settleCapturedViewAt(releasedChild.getLeft(), getMeasuredHeight()-releasedChild.getMeasuredHeight());
} else {
mDragHelper.settleCapturedViewAt(releasedChild.getLeft(), 0);
}
invalidate();
}

then on my custom view I overrode the following method:

   @Override
public void computeScroll() {
super.computeScroll();
if (mDragHelper.continueSettling(true)) {
ViewCompat.postInvalidateOnAnimation(this);
}
}

And everything worked fine!
I hope this can help someone in future.

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.

Show and hide a View with a slide up/down animation

With the new animation API that was introduced in Android 3.0 (Honeycomb) it is very simple to create such animations.

Sliding a View down by a distance:

view.animate().translationY(distance);

You can later slide the View back to its original position like this:

view.animate().translationY(0);

You can also easily combine multiple animations. The following animation will slide a View down by its height and fade it in at the same time:

// Prepare the View for the animation
view.setVisibility(View.VISIBLE);
view.setAlpha(0.0f);

// Start the animation
view.animate()
.translationY(view.getHeight())
.alpha(1.0f)
.setListener(null);

You can then fade the View back out and slide it back to its original position. We also set an AnimatorListener so we can set the visibility of the View back to GONE once the animation is finished:

view.animate()
.translationY(0)
.alpha(0.0f)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
view.setVisibility(View.GONE);
}
});

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.

How to make zoom in/out stay at final position

AnimationSet has a fillAfter property which applies the ending value after the animation ends.

<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<scale android:duration="1000"
android:fromXScale="0"
android:fromYScale="0"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="10"
android:toYScale="10" />
</set>

Object animator using old view position

You should try putting this in your onAnimationEnd instead:

@Override
public void onAnimationEnd(Animator animation) {
ObjectAnimator translationX = ObjectAnimator.ofFloat(topCard, "translationX",
topCard.getWidth() + 150, - (topCard.getWidth() + 150));
translationX.setDuration(1500);
translationX.start();

Notice that I added topCard.getWidth() + 150, making that the starting position of the topCard. According to the ObjectAnimator.ofFloat Android Docs:

Constructs and returns an ObjectAnimator that animates between float values. A single value implies that that value is the one being animated to. Two values imply starting and ending values.



Related Topics



Leave a reply



Submit