【问题标题】:Set the interpolator for android animations in Java在 Java 中设置 android 动画的插值器
【发布时间】:2012-03-07 21:33:56
【问题描述】:

这看起来很简单:我正在尝试将插值器设置为用于我的应用程序中的动画。我需要在 Java 代码中设置它,而不是 XML(它会根据各种情况而变化)。

从安卓网站,我找到了setInterpolator()

但是,我不确定如何使用它。

我试过只输入极地名称(即BounceIterpolator),但这没有任何作用。

我试过R.anim.BounceIterpolator,但Intellisense说anim cannot be resolved or is not a field。

那么如何从 Java 中设置插值器?

【问题讨论】:

标签: java android android-animation


【解决方案1】:
setInterpolator(new BounceInterpolator());

【讨论】:

    【解决方案2】:
    anim.setInterpolator(context, android.R.anim.bounce_interpolator);
    

    【讨论】:

      【解决方案3】:

      更新:

      Android 现在在动画中支持真正的弹簧和物理。它是向后兼容库的一部分。 Link

      对于自定义插值器:

      1. 使用此awesome site 创建自定义三次贝塞尔曲线。并获取曲线的控制点。
      2. Interpolator customInterpolator = PathInterpolatorCompat.create(cpX1,cpX2,cpY1,cpY2)
      3. 将此 customInterpolator 添加到您的任何动画中。

      添加了gist。 一些more here。

      【讨论】:

      • 哇。我在整个互联网上搜索了这个答案。谢谢!
      【解决方案4】:

      感谢 amalBit 我在 Kotlin 中为 ProgressBar 编写了一个带有 PathInterpolator 的插值器。

      val path = Path()
      path.lineTo(0.1f, 0.2f)
      path.lineTo(0.7f, 0.9f)
      path.lineTo(1f, 1f)
      val animation = ObjectAnimator.ofInt(progress_bar, "progress", 0, 100)
      animation.duration = 2000
      animation.interpolator = PathInterpolatorCompat.create(path)
      animation.addListener(object : Animator.AnimatorListener {
          override fun onAnimationStart(animator: Animator) {}
      
          override fun onAnimationEnd(animator: Animator) {
              finish()
          }
      
          override fun onAnimationCancel(animator: Animator) {}
      
          override fun onAnimationRepeat(animator: Animator) {}
      })
      animation.start()
      

      在https://www.programcreek.com/java-api-examples/index.php?api=android.view.animation.PathInterpolator查看更多示例。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-08-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多