@@ -3,6 +3,9 @@ package com.peterlaurence.mapview.layout
3
3
import android.content.Context
4
4
import android.util.AttributeSet
5
5
import android.view.*
6
+ import android.view.animation.AccelerateDecelerateInterpolator
7
+ import android.view.animation.DecelerateInterpolator
8
+ import android.view.animation.Interpolator
6
9
import android.widget.Scroller
7
10
import androidx.core.view.ViewCompat
8
11
import com.peterlaurence.mapview.layout.animators.ZoomPanAnimator
@@ -25,9 +28,11 @@ abstract class GestureLayout @JvmOverloads constructor(context: Context, attrs:
25
28
TouchUpGestureDetector .OnTouchUpListener , RotationGestureDetector .OnRotationGestureListener ,
26
29
GestureController .Controllable {
27
30
28
- /* Controllers */
29
31
internal val gestureController: GestureController by lazy { GestureController (this ) }
30
32
33
+ private val defaultInterpolator: Interpolator = AccelerateDecelerateInterpolator ()
34
+ private val fastInterpolator: Interpolator = DecelerateInterpolator (2f )
35
+
31
36
override fun onMinScaleUpdateRequest () {
32
37
gestureController.calculateMinimumScaleToFit(width, height)
33
38
}
@@ -220,20 +225,22 @@ abstract class GestureLayout @JvmOverloads constructor(context: Context, attrs:
220
225
*
221
226
* @param x Horizontal destination point.
222
227
* @param y Vertical destination point.
228
+ * @param interpolator The [Interpolator] the animation should use.
223
229
*/
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 )
226
232
}
227
233
228
234
/* *
229
235
* Scrolls and centers the [GestureLayout] to the x and y values provided using scrolling animation.
230
236
*
231
237
* @param x Horizontal destination point.
232
238
* @param y Vertical destination point.
239
+ * @param interpolator The [Interpolator] the animation should use.
233
240
*/
234
241
@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 )
237
244
}
238
245
239
246
/* *
@@ -243,18 +250,21 @@ abstract class GestureLayout @JvmOverloads constructor(context: Context, attrs:
243
250
* @param x Horizontal destination point.
244
251
* @param y Vertical destination point.
245
252
* @param scale The final scale value the layout should animate to.
253
+ * @param interpolator The [Interpolator] the animation should use.
246
254
*/
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 )
249
257
}
250
258
251
259
/* *
252
260
* Scales the [GestureLayout] with animated progress, without maintaining scroll position.
253
261
*
254
262
* @param destination The final scale value the layout should animate to.
263
+ * @param interpolator The [Interpolator] the animation should use.
255
264
*/
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)
258
268
}
259
269
260
270
/* *
@@ -264,22 +274,25 @@ abstract class GestureLayout @JvmOverloads constructor(context: Context, attrs:
264
274
* @param focusX The horizontal focal point to maintain, relative to the screen (as supplied by MotionEvent.getX).
265
275
* @param focusY The vertical focal point to maintain, relative to the screen (as supplied by MotionEvent.getY).
266
276
* @param scale The final scale value the layout should animate to.
277
+ * @param interpolator The [Interpolator] the animation should use.
267
278
*/
268
- fun smoothScaleFromFocalPoint (focusX : Int , focusY : Int , scale : Float ) {
279
+ fun smoothScaleFromFocalPoint (focusX : Int , focusY : Int , scale : Float , interpolator : Interpolator = defaultInterpolator ) {
269
280
val (x, y, scaleCst) = gestureController.getOffsetDestination(focusX, focusY, scale)
270
281
if (scaleCst == gestureController.scale) {
271
282
return
272
283
}
273
- animator.animateZoomPan(x, y, scaleCst)
284
+ animator.animateZoomPan(x, y, scaleCst, interpolator )
274
285
}
275
286
276
287
/* *
277
288
* Animate the scale of the [GestureLayout] while maintaining the current center point.
278
289
*
279
290
* @param scale The final scale value the layout should animate to.
291
+ * @param interpolator The [Interpolator] the animation should use.
280
292
*/
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)
283
296
}
284
297
285
298
override fun constrainScrollToLimits () {
@@ -404,14 +417,14 @@ abstract class GestureLayout @JvmOverloads constructor(context: Context, attrs:
404
417
gestureController.scale)
405
418
406
419
if (gestureController.angle == 0f ) {
407
- smoothScaleFromFocalPoint(event.x.toInt(), event.y.toInt(), scaleCst)
420
+ smoothScaleFromFocalPoint(event.x.toInt(), event.y.toInt(), scaleCst, fastInterpolator )
408
421
} else {
409
422
val angleRad = - gestureController.angle.toRad()
410
423
val eventRx = (height / 2 * sin(angleRad) + width / 2 * (1 - cos(angleRad)) +
411
424
event.x * cos(angleRad) - event.y * sin(angleRad)).toInt()
412
425
val eventRy = (height / 2 * (1 - cos(angleRad)) - width / 2 * sin(angleRad) +
413
426
event.x * sin(angleRad) + event.y * cos(angleRad)).toInt()
414
- smoothScaleFromFocalPoint(eventRx, eventRy, scaleCst)
427
+ smoothScaleFromFocalPoint(eventRx, eventRy, scaleCst, fastInterpolator )
415
428
}
416
429
417
430
return true
0 commit comments