Skip to content

Instantly share code, notes, and snippets.

@pavan5208
Last active Jul 8, 2021
Embed
What would you like to do?
private fun addOnTouchListener(params: WindowManager.LayoutParams) {
//Add touch listerner to floating controls view to move/close/expand the controls
floatingControlView?.setOnTouchListener(object : View.OnTouchListener {
private var initialTouchX = 0f
private var initialTouchY = 0f
private var initialX = 0
private var initialY = 0
override fun onTouch(view: View?, motionevent: MotionEvent): Boolean {
val flag3: Boolean
flag3 = true
var flag = false
when (motionevent.action) {
MotionEvent.ACTION_DOWN -> {
params.alpha = 1.0f
initialX = params.x
initialY = params.y
initialTouchX = motionevent.rawX
initialTouchY = motionevent.rawY
Log.d(
"OnTouchListener",
java.lang.StringBuilder("POS: x = ")
.append(initialX).append(" y = ")
.append(initialY).append(" tx = ")
.append(initialTouchX)
.append(" ty = ")
.append(initialTouchY).toString()
)
return true
}
MotionEvent.ACTION_UP -> {
flag = flag3
if (Math.abs(initialTouchX - motionevent.rawX) >= 25f) {
return flag
} else {
flag = flag3
if (Math.abs(
initialTouchY
- motionevent.rawY
) >= 25f
) {
return flag
} else {
return true
}
}
}
MotionEvent.ACTION_MOVE -> {
initialX = params.x
initialY = params.y
if ((motionevent.rawX < (initialX - iconWidth / 2).toFloat()) || (motionevent.rawY < (initialY - iconHeight / 2).toFloat()) || (motionevent.rawX
.toDouble() > initialX.toDouble() + iconWidth.toDouble() * 1.2)
) {
}
params.x = (motionevent.rawX - (iconWidth / 2).toFloat()).toInt()
params.y = (motionevent.rawY - iconHeight.toFloat()).toInt()
try {
windowManager?.updateViewLayout(floatingControlView, params)
} // Misplaced declaration of an exception
// variable
catch (e: java.lang.Exception) {
e.printStackTrace()
// ExceptionHandling(e)
return true
}
return true
}
else -> {
}
}
return flag
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment