Created
July 14, 2020 10:03
-
-
Save vaibhavpandeyvpz/00b3407b892792b62e73166d87971138 to your computer and use it in GitHub Desktop.
This file contains 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.annotation.SuppressLint | |
import android.content.Context | |
import android.util.AttributeSet | |
import android.view.MotionEvent | |
import com.otaliastudios.cameraview.CameraView | |
import kotlin.math.abs | |
class SwipeableCameraView(context: Context, attributes: AttributeSet) : CameraView(context, attributes) { | |
interface OnSwipeListener { | |
fun onSwipeLeftToRight() | |
fun onSwipeRightToLeft() | |
} | |
private companion object { | |
const val SWIPE_THRESHOLD = 200 | |
} | |
var listener: OnSwipeListener? = null | |
private var swipeX1 = 0f | |
private var swipeX2 = 0f | |
@SuppressLint("ClickableViewAccessibility") | |
override fun onTouchEvent(event: MotionEvent): Boolean { | |
when (event.action) { | |
MotionEvent.ACTION_DOWN -> swipeX1 = event.x | |
MotionEvent.ACTION_UP -> { | |
swipeX2 = event.x | |
val deltaX = swipeX2 - swipeX1 | |
if (abs(deltaX) > SWIPE_THRESHOLD) { | |
if (swipeX2 > swipeX1) { | |
listener?.onSwipeLeftToRight() | |
} else { | |
listener?.onSwipeRightToLeft() | |
} | |
} | |
} | |
} | |
return super.onTouchEvent(event) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment