I have been programming an app in kotlin. This app allows a user to swipe through an imageView and plays a sound when a user taps the screen. The onSwipeTouchListener class below is the onTouchListener for my imageView. I am using the onSwipeTouchListener to also listen for taps so that the app can play a sound when the user taps the screen. The issue is when a user swipes, the program detects a tap as well as a swipe. This results in the app playing a sound when it is not supposed to.
If someone could help me with this bug I'd much appreciate it.
here is my code:
package com.example.articulate_r
import android.animation.Animator
import android.content.Context
import android.media.MediaPlayer
import android.net.Uri
import android.view.GestureDetector
import android.view.MotionEvent
import android.view.View
import android.widget.ImageView
import android.widget.TextView
open class OnSwipeTouchListener(
private val ctx: Context,
private val imageView: ImageView,
private val textView: TextView,
private val firstAnimatorRight: Animator,
private val secondAnimatorRight: Animator,
private val firstAnimatorLeft: Animator,
private val secondAnimatorLeft: Animator
) : View.OnTouchListener {
private val gestureDetector: GestureDetector
companion object {
private val SWIPE_THRESHOLD = 100
private val SWIPE_VELOCITY_THRESHOLD = 100
}
init {
gestureDetector = GestureDetector(ctx, GestureListener())
}
override fun onTouch(v: View, event: MotionEvent): Boolean {
return gestureDetector.onTouchEvent(event)
}
private inner class GestureListener : GestureDetector.SimpleOnGestureListener() {
lateinit private var audioPlayer1 : MediaPlayer
override fun onDown(e: MotionEvent): Boolean {
playAudio(audioFiles[counter])
return true
}
override fun onFling(
e1: MotionEvent,
e2: MotionEvent,
velocityX: Float,
velocityY: Float
): Boolean {
var result = false
try {
val diffX = e2.x - e1.x
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight()
} else {
onSwipeLeft()
}
result = true
}
} catch (exception: Exception) {
exception.printStackTrace()
}
return result
}
}
var images = arrayOf(R.drawable.minecow, R.drawable.minepig,R.drawable.minechick)
var imageNames = arrayOf("Cows", "Pigs", "Chickens")
var audioFiles = arrayOf(R.raw.cows_and, R.raw.pigs_and, R.raw.chickens_and)
var counter = 0
open fun onSwipeRight() {
println("swiped right")
if (counter == 0) {
counter = images.size - 1
}
else {
counter--
}
animateRight(images[counter])
textView.text = imageNames[counter]
println(counter)
}
open fun onSwipeLeft() {
println("swiped left")
if (counter == images.size - 1) {
counter = 0
}
else {
counter++
}
animateLeft(images[counter])
textView.text = imageNames[counter]
println(counter)
}
private fun playAudio(audioFile: Int) {
var audioPlayer = MediaPlayer.create(ctx, audioFile)
audioPlayer.setVolume(100f,100f)
audioPlayer.start()
}
private fun animateLeft(drawableImage: Int) {
firstAnimatorLeft.setTarget(imageView)
secondAnimatorLeft.setTarget(imageView)
firstAnimatorLeft.setDuration(250)
secondAnimatorLeft.setDuration(250)
firstAnimatorLeft.start()
firstAnimatorLeft.addListener(
object : Animator.AnimatorListener {
override fun onAnimationStart(animation: Animator) {
}
override fun onAnimationEnd(animation: Animator) {
imageView.setImageDrawable(ctx.getDrawable(drawableImage))
secondAnimatorLeft.start()
}
override fun onAnimationCancel(animation: Animator) {}
override fun onAnimationRepeat(p0: Animator?) {}
}
)
}
private fun animateRight(drawableImage: Int) {
firstAnimatorRight.setTarget(imageView)
secondAnimatorRight.setTarget(imageView)
firstAnimatorRight.setDuration(250)
secondAnimatorRight.setDuration(250)
firstAnimatorRight.start()
firstAnimatorRight.addListener(
object : Animator.AnimatorListener {
override fun onAnimationStart(animation: Animator) {
}
override fun onAnimationEnd(animation: Animator) {
imageView.setImageDrawable(ctx.getDrawable(drawableImage))
secondAnimatorRight.start()
}
override fun onAnimationCancel(animation: Animator) {}
override fun onAnimationRepeat(p0: Animator?) {}
}
)
}
}