0

I want to have myView move deltaX, deltaY pixels from current position. But it moves two times longer than I want. This is caused by setLayoutParams in onAnimationEnd. Without that, myView's real positon is not changed. How can I change myView's real positon and animate normally?

class MyLayout extends RelativeLayout {
    Animation animation;
    MyView myView;

    animation = new TranslateAnimation(0, deltaX, 0, deltaY);
    animation.setDuration(100);
    animation.setFillAfter(true);
    myView.startAnimation(animation);           
}

class MyView extends ImageView {
    protected void onAnimationEnd() {
        super.onAnimationEnd();
        LayoutParams params = (LayoutParams)getLayoutParams();
        params.leftMargin += deltaX;
        params.topMargin += deltaY;
        setLayoutParams(params);
    }
}   
user1301568
  • 2,103
  • 4
  • 25
  • 32

1 Answers1

0

If you want to avoid doing the desired translation of the view twice as you are doing it now, you have to set the "setFillAfter" flag for the animation to false. As the documentation says "When setFillAfter is set to true, the animation transformation is applied after the animation is over."

On the other hand if you leave the "setFillAfter" flag to true, the view should translate to the desired location and no LayoutParams modification is needed in onAnimationEnd method. So it might be that something is blocking the transformation, if you could provide further information about the layout that the view is part of, it would be mostly helpful.

Another thing is that setting the margin of the layout params to move the view is not entirely the best idea. Take it more like a hack. Rather try to set the position of the view directly or try to play with weights in your parent view. That way you will get the same effect on multiple different screens.

Martin Rajniak
  • 630
  • 8
  • 26
  • Are you are sure that no modification is needed when setFillAfter is true? Please click http://stackoverflow.com/questions/9067778/button-is-not-clickable-after-translateanimation . I have the same problem with this link. – user1301568 Aug 18 '12 at 16:39
  • Another question is: How to set the positon of view directly? Do you mean layout() method? – user1301568 Aug 18 '12 at 16:43
  • When you just use the animation to translate the view (setting the fill after to true) then the animation will always move from the initial position to the one moved by delta. So if you want a functionality where hitting button will cause that the view will always be moved by delta, then don't set fill after to true. Instead as you have done move the view just in the onAnimationEnd method. For your second comment, the layout method is the best solution, and you even have an example in the thread that you posted before. – Martin Rajniak Aug 18 '12 at 18:09