40

This seems very simple: I'm trying to set the interpolator to be used for an animation in my App. I need to set this in the Java code, not the XML (It will change according to various things).

From the android website, I've found setInterpolator()

However, I'm not sure how to use this.

I've tried just feeding in the interpolar name (ie. BounceIterpolator), but this did nothing.

I tried R.anim.BounceIterpolator, but Intellisense said anim cannot be resolved or is not a field.

So how can I set the interpolator from the Java?

ACarter
  • 5,688
  • 9
  • 39
  • 56

4 Answers4

87
setInterpolator(new BounceInterpolator());
hsz
  • 148,279
  • 62
  • 259
  • 315
Reuben Scratton
  • 38,595
  • 9
  • 77
  • 86
15
anim.setInterpolator(context, android.R.anim.bounce_interpolator);
Zsolt Safrany
  • 13,290
  • 6
  • 50
  • 62
11

Update:

Android now supports real spring and physics within animations. Its part of a backward compatible lib. Link

For custom interpolators:

  1. Create a custom cubic bezier curve using this awesome site. And get the control points for the curve.
  2. Interpolator customInterpolator = PathInterpolatorCompat.create(cpX1,cpX2,cpY1,cpY2)
  3. Add this customInterpolator to any of your animation.

Added a gist. Some more here.

amalBit
  • 12,041
  • 6
  • 77
  • 94
0

Thanks to amalBit I wrote an interpolator with a PathInterpolator for a ProgressBar in Kotlin.

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()

See more examples at https://www.programcreek.com/java-api-examples/index.php?api=android.view.animation.PathInterpolator.

CoolMind
  • 26,736
  • 15
  • 188
  • 224