I'm trying to move the view in the floating window to move all the time on the screen borders like it should start from bottom and start moving clockwise on all side like bottom ->left->top->right->bottom and this will be a Loop.how can i do so? here's my code but it only moves when user moves it himself
@SuppressLint("ClickableViewAccessibility")
class OverlayView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : LifecycleView(context, attrs) {
private var binding: MShemjiLayoutBinding
private var xInView = 0.0f
private var yInView = 0.0f
private var xInScreen = 0.0f
private var yInScreen = 0.0f
var params = MutableLiveData<WindowManager.LayoutParams>()
var direction = MutableLiveData<Direction>() // Add this line
private var isMovingClockwise = true
private val windowManager by lazy { context.getSystemService(Context.WINDOW_SERVICE) as WindowManager }
init {
params.observe(this) { paramss ->
windowManager.updateViewLayout(this, paramss)
updateDirection()
}
binding = MShemjiLayoutBinding.inflate(LayoutInflater.from(context), this, true)
}
override fun onTouchEvent(event: MotionEvent?): Boolean {
when (event?.action) {
MotionEvent.ACTION_DOWN -> {
xInView = event.x
yInView = event.y
}
MotionEvent.ACTION_MOVE -> {
xInScreen = event.rawX
yInScreen = event.rawY
if (ScreenUtils.isPortrait()) {
yInScreen -= ScreenUtils.getStatusBarHeight()
} else {
xInScreen -= ScreenUtils.getStatusBarHeight()
}
updateViewPosition()
}
MotionEvent.ACTION_UP -> {
}
}
return super.onTouchEvent(event)
}
private fun updateViewPosition() {
params.value?.x = (xInScreen - xInView).toInt()
params.value?.y = (yInScreen - yInView).toInt()
updateViewLayout()
}
fun updateViewLayout() {
params.postValue(params.value)
}
Additonly i want functions that will tell me that now my lottie in the view is on left border or any other border so that i could do some functionilty there also i want to make some operations that will move the view to my speciifed posioiton slowly like not instant move Below is the code i tried but didnt get the proper func
private fun updateDirection() {
val viewLeft = params.value?.x ?: 0
val viewTop = params.value?.y ?: 0
val viewRight = viewLeft + width
val viewBottom = viewTop + height
val screenWidth = ScreenUtils.getScreenWidth()
val screenHeight = ScreenUtils.getScreenHeight()
val tolerance = 5f // Adjust the tolerance as needed
val centerX = screenWidth / 2
val centerY = screenHeight / 2
val directions = when {
viewLeft <= tolerance -> Direction.LEFT // View's left edge is touching the left side of the screen
screenWidth - viewRight <= tolerance -> Direction.RIGHT // View's right edge is touching the right side of the screen
viewTop <= tolerance -> Direction.UP // View's top edge is touching the top of the screen
screenHeight - viewBottom <= tolerance -> Direction.DOWN // View's bottom edge is touching the bottom of the screen
Math.abs(viewLeft + (viewRight - viewLeft) / 2 - centerX) <= tolerance && Math.abs(
viewTop + (viewBottom - viewTop) / 2 - centerY
) <= tolerance -> Direction.CENTER // View is precisely centered
else -> Direction.UNKNOWN
}
direction.postValue(directions)
}
this works well but not much accurate MAIN MOTIVE:the view should continously move on the corners of screen and if user himself take it and make near left right or top then it should be attached to that border and start moving there