【问题标题】:How to animate View while quickly changing its position along Y axis?如何在快速更改其沿 Y 轴的位置时为视图设置动画?
【发布时间】:2021-04-05 03:26:00
【问题描述】:

允许用户使用滑块沿 Y 轴更改屏幕上的按钮位置。发生这种情况时,我想为我的 Button 设置动画。例如,View 位于 (0,0),用户突然将其位置设置在 (0, 420)。

按钮应首先淡出(在 400 毫秒内从 alpha 1.0 到 alpha 0.0),同时位于当前位置 (0,0)。

然后,Button 位置应更改为 (0,420),并且应在位于更改的位置 (0, 420) 时淡入(在 400 毫秒内从 alpha 0.0 到 alpha 1.0)。

由于按钮位置可以很快改变,所以我得到的是一个华丽的动画效果,这是我不想要的。 这是我的代码:

        /* Alpha from 1 to 0 */
        ObjectAnimator animation = ObjectAnimator.ofFloat(myButton, "alpha", 1, 0);
        animation.setDuration(400);
        animation.setRepeatCount(1);
        animation.setInterpolator(new LinearInterpolator());
        animation.start();

        /* Alpha from 0 back to 1 */
        animation = ObjectAnimator.ofFloat(myButton, "alpha", 0, 1);
        animation.setDuration(400);
        animation.setRepeatCount(1);
        animation.setInterpolator(new LinearInterpolator());
        animation.start();

【问题讨论】:

    标签: android animation objectanimator


    【解决方案1】:

    我使用 ValueAnimator 找到了解决方案:

    ValueAnimator animator = ValueAnimator.ofInt((int) oldY, newY);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                params.y = (Integer) animation.getAnimatedValue();
                mWindowManager.updateViewLayout(myView, params);
            }
        });
        animator.start();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多