Hello All, By using simple onTouch event we make an DraggableTextView widget.
Here is the code sample for you.
Here is the full video tutorial.
If you are really interested in this code, then please share this post with your friends and share your feedback as comments.
Thank You
Here is the code sample for you.
override fun onTouch(v: View?, event: MotionEvent?): Boolean {
val x = event?.rawX?.toInt()!!
val y = event.rawY.toInt()
when (event.action) {
MotionEvent.ACTION_DOWN -> {
val layoutParams = layoutParams as ConstraintLayout.LayoutParams
xDelta = x - layoutParams.leftMargin
yDelta = y - layoutParams.topMargin
rect.set(0, 0, width, height)
isDragging = true
}
MotionEvent.ACTION_MOVE -> {
val layoutParams = layoutParams as ConstraintLayout.LayoutParams
layoutParams.leftMargin = x - xDelta
layoutParams.topMargin = y - yDelta
setLayoutParams(layoutParams)
}
MotionEvent.ACTION_UP -> {
isDragging = false
}
}
invalidate()
return true
}
Here is the full video tutorial.
Interesting right?
If you are really interested in this code, then please share this post with your friends and share your feedback as comments.
Thank You
Comments
Post a Comment