我现在正在开发一个小应用程序来测试运动和触摸事件。我面临的问题是我的 Listener 类必须被抽象,但我无法从该侦听器创建一个对象,我需要将其放在 imageView 的“setOnTouchListener”方法上。
监听类:
abstract class GestureListener(directionDisplayer: TextView) : View.OnTouchListener,` GestureDetector.OnGestureListener {
private var directionDisplayer: TextView = directionDisplayer
override fun onTouch(v: View?, event: MotionEvent?): Boolean {
val gestureDetector = GestureDetector(this)
gestureDetector.onTouchEvent(event)
return true
}
override fun onFling(
downEvent: MotionEvent?,
moveEvent: MotionEvent?,
velocityX: Float,
velocityY: Float
): Boolean {
var result = false
if (downEvent != null && moveEvent != null) {
var diffY: Float = moveEvent.y - downEvent.y
var diffX: Float = moveEvent.x - downEvent.x
val SWIPE_MIN = 100
val SWIPE_Velocity = 100 //TODO WIDTH
if (Math.abs(diffX) > Math.abs(diffY)) {
//RIGHT OR LEFT
if (Math.abs(diffX) > SWIPE_MIN && Math.abs(velocityX) > SWIPE_Velocity) {
if (diffX > 0) {
swipeRight()
} else {
swipeLeft()
}
result = true
}
} else {
//UP OR DOWN
if(Math.abs(diffY) > SWIPE_MIN && Math.abs(velocityY) > SWIPE_Velocity) {
if(diffY > 0) {
swipeUp()
} else {
swipeDown()
}
result = true
}
}
}
return result
}
private fun swipeDown() {
directionDisplayer.text = "Direction: DOWN"
}
private fun swipeUp() {
directionDisplayer.text = "Direction: UP"
}
有人知道如何解决这个问题吗?
斯蒂芬大帝
相关分类