Skip to content

Instantly share code, notes, and snippets.

@blipinsk
Created April 24, 2019 22:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blipinsk/234c24f4898b9128ecd9d493052b85f0 to your computer and use it in GitHub Desktop.
Save blipinsk/234c24f4898b9128ecd9d493052b85f0 to your computer and use it in GitHub Desktop.
Scaled delays, for synchronising coroutines with Android animations
import android.app.Activity
import android.os.Build
import android.provider.Settings
import kotlinx.coroutines.delay
public suspend fun transitionDelay(timeMillis: Long, activity: Activity) {
val transitionAnimationScale =
if (Build.VERSION.SDK_INT >= 17) {
Settings.Global.getFloat(
activity.contentResolver,
Settings.Global.TRANSITION_ANIMATION_SCALE,
1f
)
} else { // assuming 1f for older devices
1f
}
return delay((timeMillis * transitionAnimationScale).toLong())
}
public suspend fun animatorDelay(timeMillis: Long, activity: Activity) {
val animatorDurationScale =
if (Build.VERSION.SDK_INT >= 17) {
Settings.Global.getFloat(
activity.contentResolver,
Settings.Global.ANIMATOR_DURATION_SCALE,
1f
)
} else { // assuming 1f for older devices
1f
}
return delay((timeMillis * animatorDurationScale).toLong())
}
public suspend fun windowAnimationDelay(timeMillis: Long, activity: Activity) {
val windowAnimationScale =
if (Build.VERSION.SDK_INT >= 17) {
Settings.Global.getFloat(
activity.contentResolver,
Settings.Global.WINDOW_ANIMATION_SCALE,
1f
)
} else { // assuming 1f for older devices
1f
}
return delay((timeMillis * windowAnimationScale).toLong())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment