0

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

}
InsaneCat
  • 2,115
  • 5
  • 21
  • 40
RAL
  • 17
  • 4

2 Answers2

0
public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new SfView(this));
    }

    class SfView extends SurfaceView {

        private final SurfaceHolder surfaceHolder;
        private final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

        public DrawingView(Context context) {
            super(context);
            surfaceHolder = getHolder();
            paint.setColor(Color.RED);
            paint.setStyle(Style.FILL);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            if(event.getAction() == MotionEvent.ACTION_DOWN) {
                if (surfaceHolder.getSurface().isValid()) {
                    Canvas canvas = surfaceHolder.lockCanvas();
                    canvas.drawColor(Color.BLACK);
                    canvas.drawCircle(event.getX(), event.getY(), 50, paint);
                    surfaceHolder.unlockCanvasAndPost(canvas);
                }
            }
            return false;
        }
    }
}
Rohit Sharma
  • 1,384
  • 12
  • 19
0

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
    })
Tepits
  • 374
  • 6
  • 19