【问题标题】:Prevent RotateAnimation from resetting when cancelled防止 RotateAnimation 在取消时重置
【发布时间】:2012-02-09 09:27:25
【问题描述】:

我有一个简单的RotateAnimation 可以无限期地为ImageView 设置动画,直到我停止它。我的动画设置如下:

    Animation spin = new RotateAnimation(0.0f, 1800.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    spin.setInterpolator(new LinearInterpolator());
    spin.setRepeatCount(Animation.INFINITE);
    spin.setDuration(5000);
    imageView.setAnimation(spin);

当我致电imageView.cancelAnimation() 时,我是否可以将动画“冻结”在它结束的任何帧(或任何角度),而不是让它重置到第一帧?

【问题讨论】:

    标签: android animation frame rotateanimation


    【解决方案1】:

    您最好将图像内容包装在RotateDrawable 中并将其设置为ImageView,而不是使用动画。您仍然可以通过使用简单的 Handler 重复调用 setImageLevel() 来调整旋转角度来为过渡设置动画。

    到了冻结动画的时间,只需停止发布到Handler,图像将保持在其最后设置的级别值。然后,如果您愿意,可以通过恢复到 Handler 的帖子来重新启动。

    另一种选择是创建自定义ViewGroup 并使用getChildStaticTransformation() 将自定义转换应用于子ImageView。您仍将手动处理动画步骤,因此您可以在任何您喜欢的时间点开始/停止,但 Transformation 是动画系统用来为视图设置动画的,因此您可能会获得更接近该效果的结果。

    HTH

    【讨论】:

    • 我想这行得通。唯一的问题是当我尝试将RotateDrawables 用作不确定的进度条时,它们并不那么流畅。你知道如何提高不确定进度条的帧速率或速度吗?
    • 不公开。 ProgressBar 使用 AnimatedRotateDrawable,它具有这些属性,但它是一个隐藏类。我添加了另一个用于转换视图的选项。要获得更平滑的效果,您可能需要手动调用 Interpolator 上的方法,以便了解每一步调整动画的幅度。
    • 有没有使用RotateDrawable的例子?我在 android SDK 中找不到任何东西。
    【解决方案2】:

    我有一个应用程序在达到其确定的角度后必须保持其旋转位置。我搜索了一段时间,终于找到了调用 setFillAfter() 方法。这将停止旋转重置。

        RotateAnimation rotate = new RotateAnimation(startingPoint, value, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        image.setAnimation(rotate);
    
        rotate.setDuration(ROTATION_INTERVAL);
        rotate.setFillAfter(true);
    

    【讨论】:

      猜你喜欢
      • 2017-02-04
      • 1970-01-01
      • 1970-01-01
      • 2014-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-03
      • 2014-06-10
      相关资源
      最近更新 更多