Skip to content

Instantly share code, notes, and snippets.

@husaynhakeem
Created April 16, 2020 04:41
Show Gist options
  • Save husaynhakeem/d80fd43e42d75625d36778bf31cfaa2a to your computer and use it in GitHub Desktop.
Save husaynhakeem/d80fd43e42d75625d36778bf31cfaa2a to your computer and use it in GitHub Desktop.
// Implementation details omitted. Check out full sample
class DrawingCanvas: View(context) {
// When the user pinches the view, zoom in, when they reverse pinch the view, zoom out
private val scaleGestureDetector = ScaleGestureDetector(context, object :
ScaleGestureDetector.SimpleOnScaleGestureListener() {
override fun onScale(detector: ScaleGestureDetector?): Boolean {
detector?.let {
scaleX *= detector.scaleFactor
scaleY *= detector.scaleFactor
}
return true
}
})
override fun onTouchEvent(event: MotionEvent?): Boolean {
// Handle the different types of actions
when (event?.actionMasked) {
// ...
}
// Let the scale gesture detector analyze the event. If a scale gesture is detected, `onScale()` is invoked
scaleGestureDetector.onTouchEvent(event)
invalidate()
return true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment