I have a code that can move an image view using a fling animation based on user input(dragging or swiping).
fun listenPuck(goalAchieved: ()->Unit) {
val gestureListener = object : GestureDetector.SimpleOnGestureListener() {
override fun onDown(e: MotionEvent): Boolean {
return true
}
override fun onFling(
e1: MotionEvent, e2: MotionEvent,
velocityX: Float, velocityY: Float
): Boolean {
flingAnimationX = makeXFlingAnimation(initVelocity = velocityX, goalAchieved)
flingAnimationY = makeYFlingAnimation(initVelocity = velocityY, goalAchieved)
flingAnimationX.start()
flingAnimationY.start()
return true
}
}
val gestureDetector = GestureDetector(puck.context, gestureListener)
puck.setOnTouchListener { _, motionEvent ->
gestureDetector.onTouchEvent(motionEvent)
}
}
}
fun playRound(goalAchieved: () -> Unit) {
border.resetBorderColors()
placePuck()
border.nextGoal()
listenPuck(goalAchieved)
However, I am still trying to figure out how to check the position of the view during or after the fling animation. For example I wanna check at view.x == 50, I want to set view.x =20. How do I do that? Any help is appreciated!
My ultimate goal is to be able to make the view bounce at the opposite direction after it hits a border
EDIT: Figured it out. I need to put the if statement under onFling. Still trying to figure out how to make the view bounce.
I tried.
if (puck.x == puckMaxX) FlingAnimation(puck, DynamicAnimation.X).apply { setStartVelocity(-velocityX)
setMinValue(0f)
start()}
if (puck.x == puckMinX) FlingAnimation(puck, DynamicAnimation.X).apply{ setStartVelocity(-velocityX)
setMinValue(0f)
start()}
if (puck.y == puckMaxY) FlingAnimation(puck, DynamicAnimation.Y).apply { setStartVelocity(-velocityY)
setMinValue(0f)
start()}
if (puck.y == puckMinY) FlingAnimation(puck, DynamicAnimation.Y).apply { setStartVelocity(-velocityY)
setMinValue(0f)
start()}
Still doesnt bounce. Anyone know how to make the view bounce?