Last active
April 17, 2024 01:56
-
-
Save keithics/e860c55de09d73794c7854d5a8d9f9b3 to your computer and use it in GitHub Desktop.
Animated Drop Marker Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private fun startDropMarkerAnimation(marker: Marker) { | |
val target = marker.position | |
val handler = Handler() | |
val start = SystemClock.uptimeMillis() | |
val proj = mMap!!.projection | |
val targetPoint = proj.toScreenLocation(target) | |
val duration = (200 + targetPoint.y * 0.6) as Double | |
val startPoint = proj.toScreenLocation(marker.position) | |
startPoint.y = 0 | |
val startLatLng = proj.fromScreenLocation(startPoint) | |
val interpolator = LinearOutSlowInInterpolator() | |
handler.post(object : Runnable { | |
override fun run() { | |
val elapsed = SystemClock.uptimeMillis() - start | |
val t = interpolator.getInterpolation((elapsed.toFloat() / duration).toFloat()) | |
val lng = t * target.longitude + (1 - t) * startLatLng.longitude | |
val lat = t * target.latitude + (1 - t) * startLatLng.latitude | |
marker.setPosition(LatLng(lat, lng)) | |
if (t < 1.0) { | |
// Post again 16ms later == 60 frames per second | |
handler.postDelayed(this, 16) | |
} | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment