Skip to content

Commit 078ef17

Browse files
committed
Ability to set animation interpolator
1 parent 3884fbb commit 078ef17

File tree

4 files changed

+38
-23
lines changed

4 files changed

+38
-23
lines changed

Readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[ ![Download](https://api.bintray.com/packages/peterlaurence/maven/mapview/images/download.svg?version=2.0.9) ](https://bintray.com/peterlaurence/maven/mapview/2.0.9/link)
1+
[ ![Download](https://api.bintray.com/packages/peterlaurence/maven/mapview/images/download.svg?version=2.0.10) ](https://bintray.com/peterlaurence/maven/mapview/2.0.10/link)
22

33
# MapView
44

@@ -60,7 +60,7 @@ There are some breaking changes, although most of them are just package refactor
6060

6161
Add this to your module's build.gradle
6262
```groovy
63-
implementation 'com.peterlaurence:mapview:2.0.9'
63+
implementation 'com.peterlaurence:mapview:2.0.10'
6464
```
6565

6666
## Origin and motivation

mapview/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ apply plugin: 'kotlin-android'
55
apply plugin: 'kotlin-android-extensions'
66
apply plugin: 'kotlinx-serialization'
77

8-
def versionTag = "2.0.9"
8+
def versionTag = "2.0.10"
99

1010
androidExtensions {
1111
experimental = true

mapview/src/main/java/com/peterlaurence/mapview/layout/GestureLayout.kt

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package com.peterlaurence.mapview.layout
33
import android.content.Context
44
import android.util.AttributeSet
55
import android.view.*
6+
import android.view.animation.AccelerateDecelerateInterpolator
7+
import android.view.animation.DecelerateInterpolator
8+
import android.view.animation.Interpolator
69
import android.widget.Scroller
710
import androidx.core.view.ViewCompat
811
import com.peterlaurence.mapview.layout.animators.ZoomPanAnimator
@@ -25,9 +28,11 @@ abstract class GestureLayout @JvmOverloads constructor(context: Context, attrs:
2528
TouchUpGestureDetector.OnTouchUpListener, RotationGestureDetector.OnRotationGestureListener,
2629
GestureController.Controllable {
2730

28-
/* Controllers */
2931
internal val gestureController: GestureController by lazy { GestureController(this) }
3032

33+
private val defaultInterpolator: Interpolator = AccelerateDecelerateInterpolator()
34+
private val fastInterpolator: Interpolator = DecelerateInterpolator(2f)
35+
3136
override fun onMinScaleUpdateRequest() {
3237
gestureController.calculateMinimumScaleToFit(width, height)
3338
}
@@ -220,20 +225,22 @@ abstract class GestureLayout @JvmOverloads constructor(context: Context, attrs:
220225
*
221226
* @param x Horizontal destination point.
222227
* @param y Vertical destination point.
228+
* @param interpolator The [Interpolator] the animation should use.
223229
*/
224-
fun slideTo(x: Int, y: Int) {
225-
animator.animatePan(x, y)
230+
fun slideTo(x: Int, y: Int, interpolator: Interpolator = defaultInterpolator) {
231+
animator.animatePan(x, y, interpolator)
226232
}
227233

228234
/**
229235
* Scrolls and centers the [GestureLayout] to the x and y values provided using scrolling animation.
230236
*
231237
* @param x Horizontal destination point.
232238
* @param y Vertical destination point.
239+
* @param interpolator The [Interpolator] the animation should use.
233240
*/
234241
@Suppress("unused")
235-
fun slideToAndCenter(x: Int, y: Int) {
236-
slideTo(x - halfWidth, y - halfHeight)
242+
fun slideToAndCenter(x: Int, y: Int, interpolator: Interpolator = defaultInterpolator) {
243+
slideTo(x - halfWidth, y - halfHeight, interpolator)
237244
}
238245

239246
/**
@@ -243,18 +250,21 @@ abstract class GestureLayout @JvmOverloads constructor(context: Context, attrs:
243250
* @param x Horizontal destination point.
244251
* @param y Vertical destination point.
245252
* @param scale The final scale value the layout should animate to.
253+
* @param interpolator The [Interpolator] the animation should use.
246254
*/
247-
fun slideToAndCenterWithScale(x: Int, y: Int, scale: Float) {
248-
animator.animateZoomPan(x - halfWidth, y - halfHeight, scale)
255+
fun slideToAndCenterWithScale(x: Int, y: Int, scale: Float, interpolator: Interpolator = defaultInterpolator) {
256+
animator.animateZoomPan(x - halfWidth, y - halfHeight, scale, interpolator)
249257
}
250258

251259
/**
252260
* Scales the [GestureLayout] with animated progress, without maintaining scroll position.
253261
*
254262
* @param destination The final scale value the layout should animate to.
263+
* @param interpolator The [Interpolator] the animation should use.
255264
*/
256-
fun smoothScaleTo(destination: Float) {
257-
animator.animateZoom(destination)
265+
@Suppress("unused")
266+
fun smoothScaleTo(destination: Float, interpolator: Interpolator = defaultInterpolator) {
267+
animator.animateZoom(destination, interpolator)
258268
}
259269

260270
/**
@@ -264,22 +274,25 @@ abstract class GestureLayout @JvmOverloads constructor(context: Context, attrs:
264274
* @param focusX The horizontal focal point to maintain, relative to the screen (as supplied by MotionEvent.getX).
265275
* @param focusY The vertical focal point to maintain, relative to the screen (as supplied by MotionEvent.getY).
266276
* @param scale The final scale value the layout should animate to.
277+
* @param interpolator The [Interpolator] the animation should use.
267278
*/
268-
fun smoothScaleFromFocalPoint(focusX: Int, focusY: Int, scale: Float) {
279+
fun smoothScaleFromFocalPoint(focusX: Int, focusY: Int, scale: Float, interpolator: Interpolator = defaultInterpolator) {
269280
val (x, y, scaleCst) = gestureController.getOffsetDestination(focusX, focusY, scale)
270281
if (scaleCst == gestureController.scale) {
271282
return
272283
}
273-
animator.animateZoomPan(x, y, scaleCst)
284+
animator.animateZoomPan(x, y, scaleCst, interpolator)
274285
}
275286

276287
/**
277288
* Animate the scale of the [GestureLayout] while maintaining the current center point.
278289
*
279290
* @param scale The final scale value the layout should animate to.
291+
* @param interpolator The [Interpolator] the animation should use.
280292
*/
281-
fun smoothScaleFromCenter(scale: Float) {
282-
smoothScaleFromFocalPoint(halfWidth, halfHeight, scale)
293+
@Suppress("unused")
294+
fun smoothScaleFromCenter(scale: Float, interpolator: Interpolator = defaultInterpolator) {
295+
smoothScaleFromFocalPoint(halfWidth, halfHeight, scale, interpolator)
283296
}
284297

285298
override fun constrainScrollToLimits() {
@@ -404,14 +417,14 @@ abstract class GestureLayout @JvmOverloads constructor(context: Context, attrs:
404417
gestureController.scale)
405418

406419
if (gestureController.angle == 0f) {
407-
smoothScaleFromFocalPoint(event.x.toInt(), event.y.toInt(), scaleCst)
420+
smoothScaleFromFocalPoint(event.x.toInt(), event.y.toInt(), scaleCst, fastInterpolator)
408421
} else {
409422
val angleRad = -gestureController.angle.toRad()
410423
val eventRx = (height / 2 * sin(angleRad) + width / 2 * (1 - cos(angleRad)) +
411424
event.x * cos(angleRad) - event.y * sin(angleRad)).toInt()
412425
val eventRy = (height / 2 * (1 - cos(angleRad)) - width / 2 * sin(angleRad) +
413426
event.x * sin(angleRad) + event.y * cos(angleRad)).toInt()
414-
smoothScaleFromFocalPoint(eventRx, eventRy, scaleCst)
427+
smoothScaleFromFocalPoint(eventRx, eventRy, scaleCst, fastInterpolator)
415428
}
416429

417430
return true

mapview/src/main/java/com/peterlaurence/mapview/layout/animators/ZoomPanAnimator.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package com.peterlaurence.mapview.layout.animators
22

33
import android.animation.Animator
44
import android.animation.ValueAnimator
5-
import android.view.animation.AccelerateInterpolator
5+
import android.view.animation.Interpolator
66

77
class ZoomPanAnimator(private val listener: OnZoomPanAnimationListener) : ValueAnimator(),
88
ValueAnimator.AnimatorUpdateListener, Animator.AnimatorListener {
@@ -16,7 +16,6 @@ class ZoomPanAnimator(private val listener: OnZoomPanAnimationListener) : ValueA
1616
addUpdateListener(this)
1717
addListener(this)
1818
setFloatValues(0f, 1f)
19-
interpolator = AccelerateInterpolator()
2019
}
2120

2221
private fun setupPanAnimation(x: Int, y: Int): Boolean {
@@ -33,24 +32,27 @@ class ZoomPanAnimator(private val listener: OnZoomPanAnimationListener) : ValueA
3332
return startState.scale != endState.scale
3433
}
3534

36-
fun animateZoomPan(x: Int, y: Int, scale: Float) {
35+
fun animateZoomPan(x: Int, y: Int, scale: Float, interpolator: Interpolator) {
3736
hasPendingZoomUpdates = setupZoomAnimation(scale)
3837
hasPendingPanUpdates = setupPanAnimation(x, y)
3938
if (hasPendingPanUpdates || hasPendingZoomUpdates) {
39+
this.interpolator = interpolator
4040
start()
4141
}
4242
}
4343

44-
fun animateZoom(scale: Float) {
44+
fun animateZoom(scale: Float, interpolator: Interpolator) {
4545
hasPendingZoomUpdates = setupZoomAnimation(scale)
4646
if (hasPendingZoomUpdates) {
47+
this.interpolator = interpolator
4748
start()
4849
}
4950
}
5051

51-
fun animatePan(x: Int, y: Int) {
52+
fun animatePan(x: Int, y: Int, interpolator: Interpolator) {
5253
hasPendingPanUpdates = setupPanAnimation(x, y)
5354
if (hasPendingPanUpdates) {
55+
this.interpolator = interpolator
5456
start()
5557
}
5658
}

0 commit comments

Comments
 (0)