@@ -3,6 +3,7 @@ package com.algolia.instantsearch.voice.ui
33import android.animation.AnimatorSet
44import android.content.Context
55import android.graphics.Canvas
6+ import android.os.Build
67import android.util.AttributeSet
78import android.view.View
89
@@ -25,6 +26,8 @@ class RippleView : View {
2526 init (attrs)
2627 }
2728
29+ private val fps = 1000L / 60L
30+
2831 /* * All circles used in this RippleView, generated during [init]. */
2932 private var circles = listOf<DrawableSprite >()
3033 /* * Current animations, filled when [State.Playing] triggers [animation] and emptied when [State.Stopped]. */
@@ -35,7 +38,7 @@ class RippleView : View {
3538
3639 override fun run () {
3740 invalidate()
38- postDelayed (this , 1000 / 60 )
41+ postRunnableFps (this , fps )
3942 }
4043 }
4144
@@ -47,7 +50,7 @@ class RippleView : View {
4750 override fun run () {
4851 animations[index] = circles[index].circleAnimation().also { it.start() }
4952 index = if (index == circles.lastIndex) 0 else index + 1
50- postDelayed (this , delay )
53+ postRunnableAnimation (this )
5154 }
5255 }
5356
@@ -64,7 +67,7 @@ class RippleView : View {
6467 State .Playing -> {
6568 animations.values.forEach { it.end() }
6669 animations.clear()
67- post (runnableAnimation)
70+ postRunnableAnimation (runnableAnimation)
6871 }
6972 State .Stopped -> removeCallbacks(runnableAnimation)
7073 State .None -> Unit
@@ -73,7 +76,7 @@ class RippleView : View {
7376
7477 override fun onAttachedToWindow () {
7578 super .onAttachedToWindow()
76- post (runnableFps)
79+ postRunnableFps (runnableFps, fps )
7780 }
7881
7982 override fun onDetachedFromWindow () {
@@ -95,6 +98,22 @@ class RippleView : View {
9598 }.recycle()
9699 }
97100
101+ private fun postRunnableAnimation (runnable : Runnable ) {
102+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .JELLY_BEAN ) {
103+ postOnAnimationDelayed(runnable, delay)
104+ } else {
105+ postDelayed(runnable, delay)
106+ }
107+ }
108+
109+ private fun postRunnableFps (runnable : Runnable , fps : Long ) {
110+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .JELLY_BEAN ) {
111+ postOnAnimation(runnable)
112+ } else {
113+ postDelayed(runnable, fps)
114+ }
115+ }
116+
98117 private fun DrawableSprite.circleAnimation (): AnimatorSet =
99118 AnimatorSet ().also {
100119 it.duration = duration
0 commit comments