Skip to content

Instantly share code, notes, and snippets.

@meoyawn
Last active April 5, 2019 09:08
Show Gist options
  • Save meoyawn/ec84f0aafd0b763629f282eb249059d8 to your computer and use it in GitHub Desktop.
Save meoyawn/ec84f0aafd0b763629f282eb249059d8 to your computer and use it in GitHub Desktop.
Android multitouch helper
import android.view.MotionEvent
typealias PxF = Float
typealias X = PxF
typealias Y = PxF
typealias PointerId = Int
inline fun MotionEvent.multiTouch(
down: (PointerId, X, Y) -> Unit,
move: (PointerId, X, Y) -> Unit,
up: (PointerId, X, Y) -> Unit
) {
val action = actionMasked
for (i in 0 until pointerCount) {
val pointer = getPointerId(i)
val x = getX(i)
val y = getY(i)
when (action) {
MotionEvent.ACTION_DOWN, MotionEvent.ACTION_POINTER_DOWN ->
down(pointer, x, y)
MotionEvent.ACTION_MOVE ->
move(pointer, x, y)
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL, MotionEvent.ACTION_POINTER_UP ->
up(pointer, x, y)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment