artist
artist

Reputation: 6381

Draw bitmap on imageview with every touch - android

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

Upvotes: 1

Views: 1029

Answers (4)

user4774371
user4774371

Reputation:

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;

            }
        });
    }

Upvotes: 0

Suneel Prakash
Suneel Prakash

Reputation: 389

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.

Upvotes: 0

Knossos
Knossos

Reputation: 16068

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.

Upvotes: 1

matty357
matty357

Reputation: 637

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

Upvotes: 0

Related Questions