MCR
MCR

Reputation: 1643

Android: Change Bitmap image displayed on Canvas

I am drawing a bitmap in my onDraw and want to be able to change that image when a user clicks on a certain part of the screen. I display the image like this...

protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawColor(Color.WHITE);
    canvas.drawBitmap(mallMap, 0, 0, null);
    canvas.drawLines(pts, paint);}

then in my onTouch method I'd like to be able to redraw a different bitmap

public boolean onTouchEvent(MotionEvent event) {
    final int action = event.getAction();
    final int x = (int) event.getX();
    final int y = (int) event.getY();

    if (action == MotionEvent.ACTION_DOWN) {
    Canvas.drawBitmap(lowerLeft, 0, 0, null);
    return false;}

is something like this possible? I need to stick with a canvas and am not sure how I would got about doing this.

Thanks.

Upvotes: 2

Views: 2421

Answers (1)

Bobbake4
Bobbake4

Reputation: 24847

You can do this by changing the value of mallMap to a new Bitmap in the onTouchEvent and then calling invalidate() on the view to cause the onDraw to be run again. You should not have to reference the canvas outside of the onDraw using this method.

Upvotes: 2

Related Questions