Last active
April 24, 2018 02:30
-
-
Save alexfu/7f7d0aae081ef78387a6d1cff5415b8f to your computer and use it in GitHub Desktop.
Android TouchSlopDelegate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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