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
})
}