Animation.Setfillafter/Before - Do They Work/What Are They For

Animation.setFillAfter/Before - Do they work/What are they for?

The answer is yes, they do work, just probably not for what you expect - for instance, the description for setFillAfter(boolean fillAfter) says

If fillAfter is true, the
transformation that this animation
performed will persist when it is
finished.

and when set to true it does do this.

However, unintuitively an animation on Android does not actually animate the View itself, rather it animates a bitmap representation of the View.

The issue you are probably having is that after an animation the View goes back to being how it was before the animation - setFillAfter and setFillBefore cannot help you with that, because in that situation what you really want to do is set the properties of the View to be same as the animated representation (they are separate things), and setFillAfter and setFillBefore only apply to animation properties, not View properties.

The reason they exist is for chaining animations. Suppose you had a translate animation followed by a fade out. If you did not set setFillAfter(true) on the translate animation, then the View would translate, jump back to it's original position and then fade out. With setFillAfter(true) set on the translate animation, the view will move and then fade out at it's current spot.

Translation animation for hiding View

Finally i find the answer and it is very simple modification in co-ordinate values. And the code is

Animation animation = new TranslateAnimation(0,-200,0, 0);
animation.setDuration(2000);
animation.setFillAfter(true);
listView1.startAnimation(animation);
listView1.setVisibility(0);

Here am setting negative value at second co-ordinate cause from o it is moving twowards negative side which means the view is moving twowards inner left side.

Why my animation does not stop in the to-x given value?

You need to use

mSlideAnimation.setFillAfter(true);

to make it not revert back to the start.

If that doesn't work you might have to follow the suggestion on Animation.setFillAfter/Before - Do they work/What are they for?

Translated Buttons not giving correct results

I have re-generated your case and it seems like only 50% of the button shows the selected color. Is that right ? Try this :

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sampleButton = (Button)findViewById(R.id.btn);
sampleButton.animate().translationX(somevalue).setDuration(someduration);
}

Android: Is there any way to get the latest position of View after TranslateAnimation?

Assuming you're using Android < 3.0 then your question may be in a similar vein to mine I asked here. Basically Animations are separate from the View itself i.e. Android animates a copy of your View. That is why getLocationOnScreen() always returns 0. It's not the view that has moved (animated) it was the copy that moved (animated). If you see the answers to my question this issue has been addressed in later versions of Android.

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.

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



Related Topics



Leave a reply



Submit