【问题标题】:Reset view to original position after animation动画后将视图重置为原始位置
【发布时间】:2019-04-12 08:46:36
【问题描述】:

我正在为视图设置动画,我想在动画结束后将视图重置为原始位置。

这就是我所拥有的:

rl2 是一个相对布局

rl2.animate().translationX(-60).translationY(117).setDuration(2000);

我试过设置,但它不起作用:

rl2.clearAnimation();

【问题讨论】:

    标签: android


    【解决方案1】:

    我知道很久以前就有人问过这个问题,但有人可能仍在寻找答案。 我使用Animation 类来处理这种事情;这个类中有一个setFillAfter方法,所以你可以设置动画结束后是否应用它的变换。

    Animation anim = new ScaleAnimation(
                1f, 1f, // Start and end values for the X axis scaling
                1f, 2f, // Start and end values for the Y axis scaling
                Animation.RELATIVE_TO_SELF, 0f, // Pivot point of X scaling
                Animation.RELATIVE_TO_SELF, 1f); // Pivot point of Y scaling
    anim.setFillAfter(false); // no needed to keep the result of the animation
    anim.setDuration(1000);
    anim.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
    
        }
    
        @Override
        public void onAnimationEnd(Animation animation) {
                        
        }
    
        @Override
        public void onAnimationRepeat(Animation animation) {
    
        }
    });
    gangsterImage.startAnimation(anim);
    

    【讨论】:

      【解决方案2】:

      clearAnimation(); 不会重置您的动画,它只是停止它们并将它们从动画队列中移除。要撤消动画,您需要实际撤消它们。因此,对于您的代码块,您需要调用rl2.animate().translationX(0).translationY(0).setDuration(2000); 将视图移回其原始位置。

      【讨论】:

        【解决方案3】:

        正如@Chris Stillwell在他的answer中提到的,但是你可以在

        翻译动画后将View移回原来的位置
        rl2.animate().translationX(0).translationY(0);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-03-03
          • 2014-11-04
          • 1970-01-01
          • 2015-12-24
          • 1970-01-01
          • 2011-12-08
          • 1970-01-01
          • 2013-05-02
          相关资源
          最近更新 更多