Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change position of view after end of animation

I develop a widget based on ViewGroup and my problem is that I need to save position of items after the end of animation. I called setFillAfter(true) in my animation object I created AnimationListener and in it's onAnimationEnd method call View.layout(l,t,r,b) to set the position after animation, because I want animation to start from new item's position next time. But in this case it looks like items are layouted twice. If I don't use View.layout(l,t,r,b) at the end of animation, next animation starts from previous position. Here is my code:

private void scrollUp() {
    for(int i = 0; i < getChildCount(); i++) {
        final View child = getChildAt(i);
        final int index = i; 
        final int newleft = child.getLeft() + mOffsetBetweenItems;
        final int newTop = child.getTop() - mOffsetBetweenItems;
        TranslateAnimation scrollUp = new TranslateAnimation(0, mOffsetBetweenItems, 0, -mOffsetBetweenItems);          
        scrollUp.setDuration(1500);
        scrollUp.setFillAfter(true);        
        scrollUp.setAnimationListener(
            new AnimationListener() {

                @Override
                public void onAnimationStart(Animation animation) {}

                @Override
                public void onAnimationRepeat(Animation animation) {}

                @Override
                public void onAnimationEnd(Animation animation) {
                    child.layout(newleft, newTop, newleft + child.getMeasuredWidth(), newTop + child.getMeasuredHeight() );
                }
            }
        );

        child.startAnimation(scrollUp);
    }
}

Please give me an advice how should I reset view's position accordingly to the end position of animation?

like image 944
teoREtik Avatar asked Dec 13 '11 13:12

teoREtik


People also ask

How do you move the view on Android?

If you want o move a view on Android, you should implemts OnTouchListener. In your layout you can define a view, in this case we use ImageView. But You can use any view.


1 Answers

You must use ObjectAnimator , it works from API 11 level . It changes View position automatic,

here is the example

ObjectAnimator objectAnimator= ObjectAnimator.ofFloat(mContent_container, "translationX", startX, endX);
objectAnimator.setDuration(1000);
objectAnimator.start();

Thanks JUL for his answer

If your app not found object animator, change the API level from Project -> properties -> Android , than import android.animation.ObjectAnimator;

like image 56
Hayk Nahapetyan Avatar answered Sep 20 '22 01:09

Hayk Nahapetyan