1

In my imageview I want to draw a bitmap everytime imageview is touched without erasing the previous bitmap. Below code draws a new bitmap but also erases the previous one. How can I keep the previous bitmap while adding new one? Thanks

imageview.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { int action = event.getAction(); x = event.getX(); y = event.getY(); switch (action) { case MotionEvent.ACTION_DOWN: Bitmap.Config config = bm.getConfig(); int width = bm.getWidth(); int height = bm.getHeight(); Bitmap bm2 = Bitmap.createBitmap(width, height, config); Canvas c = new Canvas(bm2); c.drawBitmap(bm, 0, 0, null); Bitmap repeat = BitmapFactory.decodeResource(getResources(), R.drawable.pic); Bitmap repeat2 = Bitmap.createScaledBitmap(repeat, 50, 50, false); c.drawBitmap(repeat2, x, y, p); imageview.setImageBitmap(bm2); break; return true; } }); } 

Like this image

1
  • how did you fix the issue? can you share here Commented Dec 28, 2017 at 9:19

4 Answers 4

1

If you will use the same bitmap each time...

  1. Make the Bitmap a member variable and initialize it in your onCreate system.
  2. Create an ArrayList as a member variable.
  3. Add a new Point each time you touch the ImageView.
  4. Loop through the Point List and draw the same Bitmap onto your ImageView canvas.
Sign up to request clarification or add additional context in comments.

Comments

0

I think you need to use an array of Bitmaps, then use the onClick to iterate through the array.

Comments

0

You need to have two layers for that. When you change some pixels of a bitmap, it won't be able to handle two layers. So you need to create another layer on image View and set new bitmap over there.

you have to keep track of pixels you parsed through in an arraylist, so that you can you can handle as eraser also by resetting those pixels to transparent.

2 Comments

How to create a layer on imageview? Could you please elaborate with code
Refer: github.com/firebase/AndroidDrawing will eplain you in a better way.
0

Do it like this.

Bitmap bm2 = null; Canvas c = null; imageview.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { int action = event.getAction(); x = event.getX(); y = event.getY(); switch (action) { case MotionEvent.ACTION_DOWN: Bitmap.Config config = bm.getConfig(); int width = bm.getWidth(); int height = bm.getHeight(); if(bm2==null){ bm2 = Bitmap.createBitmap(width, height, config); c = new Canvas(bm2); } c.drawBitmap(bm, 0, 0, null); Bitmap repeat = BitmapFactory.decodeResource(getResources(), R.drawable.pic); Bitmap repeat2 = Bitmap.createScaledBitmap(repeat, 50, 50, false); c.drawBitmap(repeat2, x, y, p); imageview.setImageBitmap(bm2); break; return true; } }); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.