TranslateAnimation无法正常工作

时间:2018-07-19 04:26:11

标签: android

部分代码可以正常工作。但是,在第二个中,动画发生了,但是没有适当的方式。

应滑动的视图跳到各自的位置(它们将移至正确的位置,但动画不正确)。

有人能解决这种不良行为吗?

显示问题的GIF enter image description here

代码:

 public void animateCollapse(){

        final int count = getChildCount();
        final int tWidth = getMeasuredWidth();
        int widthSum = 0;
        for (int i = 0; i < count; i++) {
            final RelativeLayout child = (RelativeLayout) getChildAt(i);
            widthSum += child.getWidth();
            child.clearAnimation();
        }
       if(!mTagLayout.isCollapsed()){

         //Removed piece of code, that works. 
        }
       } else{
           int currentPosX = 0;
           if(tWidth> widthSum) return;

           for (int i = 0; i < count; i++) {
               final View child = getChildAt(i);

               int lastpos = tWidth - child.getMeasuredWidth();

               float value =currentPosX - child.getX();
                       TranslateAnimation anim = new TranslateAnimation(0, value, 0, 0);
               anim.setDuration(1000);

               anim.setAnimationListener(new TranslateAnimation.AnimationListener() {

                   @Override
                   public void onAnimationStart(Animation animation) { }

                   @Override
                   public void onAnimationRepeat(Animation animation) { }

                   @Override
                   public void onAnimationEnd(Animation animation)
                   {
                   isAnimating= false;
                   }
               });

               anim.setFillAfter(true);
               child.startAnimation(anim);
               currentPosX += child.getMeasuredWidth();

               mTagLayout.setCollapsed(false);

           }
       }

@编辑 动画期间视图不会被破坏和重建

1 个答案:

答案 0 :(得分:0)

我通过使用反向插值器进行了修正

 if (mTagLayout.isCollapsed()) {
      anim.setInterpolator(new ReverseInterpolator());
      mTagLayout.setCollapsed(false);
 } else {
      mTagLayout.setCollapsed(true);
 }


public class ReverseInterpolator implements Interpolator {
    @Override
    public float getInterpolation(float paramFloat) {
        return Math.abs(paramFloat - 1f);
    }
}