Change position of view after end of animation

前端 未结 3 522
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-04 15:58

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

相关标签:
3条回答
  • 2021-02-04 16:23

    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;

    0 讨论(0)
  • 2021-02-04 16:35

    I did it. Here is the 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.setFillEnabled(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);
        }
    }
    

    Just removed setFillAfter(true) and write setFillEnabled(true) instead of. But in this case I don't understand the logic of setFillEnabled() working, because it provides behavior not like describes in documentation.

    0 讨论(0)
  • 2021-02-04 16:39

    Reverse your animation; Start by giving your view a new position and then animate from the old position to the new position.

    0 讨论(0)
提交回复
热议问题