Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smooth Rotation patch #1

Merged
merged 1 commit into from
Sep 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ dependencies {
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
// Google Map dependency
implementation 'com.google.android.gms:play-services-maps:17.0.0'
implementation 'com.google.maps.android:maps-utils-ktx:3.2.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package com.mindorks.example.ubercaranimation
import android.graphics.Color
import android.os.Bundle
import android.os.Handler
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.OnMapReadyCallback
import com.google.android.gms.maps.SupportMapFragment
import com.google.android.gms.maps.model.*
import com.google.maps.android.SphericalUtil
import com.mindorks.example.ubercaranimation.util.AnimationUtils
import com.mindorks.example.ubercaranimation.util.MapUtils

Expand All @@ -23,6 +25,7 @@ class MainActivity : AppCompatActivity(), OnMapReadyCallback {
private var blackPolyline: Polyline? = null
private var movingCabMarker: Marker? = null
private var previousLatLng: LatLng? = null
private var prevBearing: Double? = null
private var currentLatLng: LatLng? = null
private lateinit var handler: Handler
private lateinit var runnable: Runnable
Expand Down Expand Up @@ -114,8 +117,10 @@ class MainActivity : AppCompatActivity(), OnMapReadyCallback {
movingCabMarker?.setAnchor(0.5f, 0.5f)
animateCamera(currentLatLng!!)
} else {
prevBearing = SphericalUtil.computeHeading(previousLatLng!!, currentLatLng!!)
previousLatLng = currentLatLng
currentLatLng = latLng
Log.wtf("PrevBearing:", "$prevBearing")
val valueAnimator = AnimationUtils.carAnimator()
valueAnimator.addUpdateListener { va ->
if (currentLatLng != null && previousLatLng != null) {
Expand All @@ -125,9 +130,19 @@ class MainActivity : AppCompatActivity(), OnMapReadyCallback {
multiplier * currentLatLng!!.longitude + (1 - multiplier) * previousLatLng!!.longitude
)
movingCabMarker?.position = nextLocation
val rotation = MapUtils.getRotation(previousLatLng!!, nextLocation)

/*** Calculating the difference between bearings and only animating the diff value rotation, which removes the jerk
* Just make sure the next location update doesn't come before this valueAnimator ends, adjust the
* valueAnimator's duration less than the location update (animationDuration < UpdateInterval) */
val offsetBearing = (SphericalUtil.computeHeading(previousLatLng!!,nextLocation) - prevBearing!!)
val rotation = prevBearing!! + multiplier * offsetBearing
Log.wtf("animated_bearing:", "$rotation")

/***Non animated rotation but Jerk free, Uncomment below line & comment out the above 3 lines to use this */
//val rotation = SphericalUtil.computeHeading(previousLatLng!!,currentLatLng)

if (!rotation.isNaN()) {
movingCabMarker?.rotation = rotation
movingCabMarker?.rotation = rotation.toFloat()
}
movingCabMarker?.setAnchor(0.5f, 0.5f)
animateCamera(nextLocation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ object AnimationUtils {

fun carAnimator(): ValueAnimator {
val valueAnimator = ValueAnimator.ofFloat(0f, 1f)
valueAnimator.duration = 3000
valueAnimator.duration = 2000
valueAnimator.interpolator = LinearInterpolator()
return valueAnimator
}
Expand Down
10 changes: 4 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.3.71'
ext.kotlin_version = '1.5.30'
repositories {
google()
jcenter()

mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.1'
classpath 'com.android.tools.build:gradle:7.0.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

// NOTE: Do not place your application dependencies here; they belong
Expand All @@ -19,8 +18,7 @@ buildscript {
allprojects {
repositories {
google()
jcenter()

mavenCentral()
}
}

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-all.zip