Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· OpenAIAi2 WildChat-4.8M (AI2)

can you write a android kotlin class following with the below requirement? This class is…

  1. User

    can you write a android kotlin class following with the below requirement? This class is inherits from base class that show automatic interate show image in the images list. This class allow to rotate interact with touch event to allow user to rotate the picture.

  2. Assistant

    Here's a class written in Kotlin that extends a base class (BaseImageView) and allows the user to rotate the image by interacting with touch events:

    import android.content.Context
    import android.graphics.Matrix
    import android.graphics.PointF
    import android.view.MotionEvent
    import android.view.View
    import android.widget.ImageView
    import kotlin.math.atan2
    import kotlin.math.sqrt
    
    open class RotatableImageView : BaseImageView, View.OnTouchListener {
    
        private var start = PointF()
        private var mid = PointF()
        private var oldDist = 0f
        private val matrix = Matrix()
        private val savedMatrix = Matrix()
        private var mode = NONE
    
        constructor(context: Context) : super(context) {
            this.setOnTouchListener(this)
        }
    
        override fun onTouch(v: View, event: MotionEvent): Boolean {
            val view = v as ImageView
            when (event.action and MotionEvent.ACTION_MASK) {
                MotionEvent.ACTION_DOWN -> {
                    savedMatrix.set(matrix)
                    start.set(event.x, event.y)
                    mode = DRAG
                }
                MotionEvent.ACTION_POINTER_DOWN -> {
                    oldDist = spacing(event)
                    if (oldDist > 10f) {
                        savedMatrix.set(matrix)
                        midPoint(mid, event)
                        mode = ROTATE
                    }
                }
                MotionEvent.ACTION_UP, MotionEvent.ACTION_POINTER_UP -> mode = NONE
                MotionEvent.ACTION_MOVE -> if (mode == ROTATE) {
                    val newDist = spacing(event)
                    if (newDist > 10f) {
                        val scale = newDist / oldDist
                        matrix.set(savedMatrix)
                        val rotation = rotation(event)
                        matrix.postRotate(rotation, mid.x, mid.y)
                        matrix.postScale(scale, scale, mid.x, mid.y)
                    }
                }
            }
    
            view.imageMatrix = matrix
            return true
        }
    
        private fun rotation(event: MotionEvent): Float {
            val deltaX = event.getX(0) - event.getX(1)
            val deltaY = event.getY(0) - event.getY(1)
            val radians = atan2(deltaY.toDouble(), deltaX.toDouble()).toFloat()
    
            return Math.toDegrees(radians.toDouble()).toFloat()
        }
    
        private fun spacing(event: MotionEvent): Float {
            val x = event.getX(0) - event.getX(1)
            val y = event.getY(0) - event.getY(1)
            return sqrt(x * x + y * y.toDouble()).toFloat()
        }
    
        private fun midPoint(point: PointF, event: MotionEvent) {
            val x = event.getX(0) + event.getX(1)
            val y = event.getY(0) + event.getY(1)
            point.set(x / 2, y / 2)
        }
    
        companion object {
            private const val NONE = 0
            private const val DRAG = 1
            private const val ROTATE = 2
        }
    }
    

    This class assumes that you have a BaseImageView class that takes care of automatically iterating and showing images. It extends that class and adds the functionality to rotate the image based on touch events. Note that you should replace BaseImageView with the actual base class you're using in your project.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.