1
votes

i have a problem with rotating when i touch the rotate button and swipe right and left like normal rotate it will work fine but if you move your finger inside the layout container it will keep rotating as a fan i have been trying to fix this problem since 5 days but i got nothing if you have any solution would be great enter image description here here is the code

    btnrot.setOnTouchListener(OnTouchListener { v, event ->
        if (!freeze) {
            layoutParams = layGroup.layoutParams as LayoutParams
            layBg = v.parent as RelativeLayout
            val arrayOfInt = IntArray(2)
            layBg!!.getLocationInWindow(arrayOfInt)

            var mX = event.rawX.toInt() - arrayOfInt[0]
            var mY = event.rawY.toInt() - arrayOfInt[1]

            when (event.action) {
                MotionEvent.ACTION_DOWN -> {
                    
                    layGroup.invalidate()
                    startDegree = layGroup.rotation

                    pivx = layoutParams.leftMargin + v.width / 2
                    pivy = layoutParams.topMargin + v.height / 2

                    basex = mX - pivx
                    basey = pivy - mY

                }

                MotionEvent.ACTION_MOVE -> {
                    val k = pivx
                    val m = pivy
                    mY = (Math.toDegrees(
                        atan2(
                            basey.toDouble(),
                            basex.toDouble()
                        )
                    ) - Math.toDegrees(atan2(m - mY.toDouble(), mX - k.toDouble()))).toInt()
                    mX = mY
                    if (mY < 0) {
                        mX = mY + 360
                    }
                    val km = event.y.toInt()

                   // if (km in 0..1000) {
                        layGroup.rotation = (startDegree + mX) % 360f
                   // }
                }
            }
            return@OnTouchListener true
        }
        freeze
    })