Search code examples
androiddrawontouchlistener

Draw a circle each time I touch the screen


My objective is to draw a circle each time I touch the screen, while if I wish to touch again the previous circle would disapear.

What is happening now is that the previous circles do not disappear, so they add up each time i touch the screen. enter image description here

Here is my code:

Kotlin:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)       

    var imageBody: ImageView = findViewById(R.id.imageViewBody)       

    imageBody.isDrawingCacheEnabled = true
    imageBody.buildDrawingCache(true)

    imageBody.setOnTouchListener(OnTouchListener { v, event ->

        if(event != null){
            if (event.action == MotionEvent.ACTION_DOWN) {
                val bitmap: Bitmap = imageBody.drawingCache
                val pixel: Int = bitmap.getPixel(event.getX().toInt(), event.getY().toInt())
                coordX = event.getX()
                coordY = event.getY()

                val Drawable = imageBody.drawable
                val ImageBounds = Drawable.bounds
                val scaledHeight = ImageBounds.height()
                val scaledWidth = ImageBounds.width()

                OrigX = coordX / scaledHeight
                OrigY = coordY / scaledWidth

                when (pixel) {
                    Color.rgb(241,241,241) -> {
                        val canvas = Canvas(bitmap)
                        val paint = Paint()
                        paint.color = Color.rgb(255,128,0)

                        canvas.drawCircle(coordX, coordY, 15F, paint) /**DRAW CIRCLE*/

                        imageBody.setImageBitmap(bitmap)
                        imageBody.Invalidate()
                    }
                }
            }
        }

        false
    })

}

Solution

  • Try this one. Clear the imageView's bitmap inside the action ACTION_UP. Here is the example code:

    First make your bitmap outside the onTouchListener like:

    private var bitmap: Bitmap? = null
    

    Then your onTouchListener will be like:

    imageBody.setOnTouchListener({ v, event ->
    
            if(event != null){
                if (event.action == MotionEvent.ACTION_DOWN) {
                    bitmap = Bitmap.createBitmap(imageBody.drawingCache)
                    val pixel: Int = bitmap.getPixel(event.getX().toInt(), event.getY().toInt())
                    coordX = event.getX()
                    coordY = event.getY()
    
                    val Drawable = imageBody.drawable
                    val ImageBounds = Drawable.bounds
                    val scaledHeight = ImageBounds.height()
                    val scaledWidth = ImageBounds.width()
    
                    OrigX = coordX / scaledHeight
                    OrigY = coordY / scaledWidth
    
                    when (pixel) {
                        Color.rgb(241,241,241) -> {
                            val canvas = Canvas(bitmap)
                            val paint = Paint()
                            paint.color = Color.rgb(255,128,0)
    
                            canvas.drawCircle(coordX, coordY, 15F, paint) /**DRAW CIRCLE*/
    
                            imageBody.setImageBitmap(bitmap)
                            imageBody.Invalidate()
                        }
                    }
                }
    
                if (event.action == MotionEvent.ACTION_UP) {
                    if (bitmap != null && !bitmap!!.isRecycled) {
                        bitmap?.recycle()
                        bitmap = null
                    }
                }
            }
            true
        })