Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Android TouchSlopDelegate
import android.content.Context
import android.view.MotionEvent
import android.view.MotionEvent.*
import android.view.ViewConfiguration
class TouchSlopDelegate(context: Context) {
private val targetTouchSlop = ViewConfiguration.get(context).scaledTouchSlop
private var touchSlop: Int = 0
private var lastTouchX: Float = 0f
private var lastTouchY: Float = 0f
val pastThreshold: Boolean
get() = touchSlop > targetTouchSlop
fun trackTouchEvent(event: MotionEvent) {
when (event.action) {
ACTION_DOWN -> {
lastTouchX = event.x
lastTouchY = event.y
}
ACTION_MOVE -> {
val deltaX = Math.abs(event.x - lastTouchX)
val deltaY = Math.abs(event.y - lastTouchY)
touchSlop = Math.max(deltaX, deltaY).toInt()
}
ACTION_UP -> {
touchSlop = 0
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment