Skip to content

Instantly share code, notes, and snippets.

@AbhinavChauhan97
Last active July 1, 2022 05:27
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 AbhinavChauhan97/fac4354cf9c46a51532113c7b65db408 to your computer and use it in GitHub Desktop.
Save AbhinavChauhan97/fac4354cf9c46a51532113c7b65db408 to your computer and use it in GitHub Desktop.
private fun flyPlane(plane: ImageView, pathMeasure: PathMeasure) {
var distance = 0f
val tan = floatArrayOf(0f,0f)
val pos = floatArrayOf(0f, 0f)
val valueAnimator = ValueAnimator.ofFloat(0f, 1f)
// fly planes with different speed so that they look separately on the screen
valueAnimator.duration = (pathMeasure.length * random.nextInt(3,8)).toLong()
valueAnimator.interpolator = AccelerateDecelerateInterpolator()
valueAnimator.addUpdateListener {
distance = it.animatedValue as Float
pathMeasure.getPosTan(distance * pathMeasure.length, pos, tan)
val planeX = pos[0]
val planeY = pos[1]
plane.x = planeX
plane.y = planeY
val degrees = atan2(tan[1], tan[0]) * 180.0 / Math.PI
plane.rotation = degrees.toFloat() - 180
}
valueAnimator.doOnEnd {
if (pathMeasure.nextContour()) {
flyPlane(plane, pathMeasure)
}
}
valueAnimator.start()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment