0

How can I inverse the image drawed into a Canvas?

I has the following:

        canvas.save();
        canvas.drawBitmap(image.current(), null, currentBounds(), Paints.BLANK);
        canvas.restore();

How can I make the current image be drawed fliped on x-axis into the currentBounds()?

I already found some answers indicating usage of Matrix, but I wan't to know if there's a easier way? Such a Paint with some flag turned on.

EDIT:

Following is my try with Matrix transformations:

    Rect currentBounds = currentBounds();


    currentBounds.offset((int)offset.x(), (int)offset.y());

    float scale = image.current().getWidth() / currentBounds.width();

    Matrix matrix = new Matrix();
    matrix.setScale(- scale - 1, scale + 1);
    matrix.postTranslate(currentBounds.left, currentBounds.top);

    canvas.drawBitmap(image.current(), matrix, Paints.BLANK);
    canvas.drawRect(currentBounds, Paints.STROKE_BLUE);

The following is the result of this draw:

https://dl.dropbox.com/u/28683814/game.png

As can be seen, the sprite is being drawed from 0,0 to left and it's not fully completes the currentBounds(), what I'm doing wrong?

Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167

3 Answers3

0

Use

canvas.scale(-1,0,width/2,height/2);

as mentioned by my own answer here.

Although, I would point out that it will take you about a minute to understand Matrices, and it'll make you a better Android programmer.

Community
  • 1
  • 1
you786
  • 3,659
  • 5
  • 48
  • 74
  • Actually, this make my draw disappear I understand matrices, I was just wondering what I would do with the scale applied on the game (bounds are not the original bitmap size), but it's possible to do it trought the matrix too. I'll give it a try. – Marcos Vasconcelos Aug 07 '12 at 19:50
0

Use this. I think this is quite easy.

        canvas.save();
        canvas.rotate(180, x, y);
        canvas.drawBitmap(bitmap, x, y, null);
        canvas.restore();
Alex
  • 847
  • 3
  • 15
  • 23
0

Well, I solved this way:

    Rect currentBounds = currentBounds();
    currentBounds.offset((int)offset.x(), (int)offset.y());

    float scale = (float) currentBounds.width() / (float) image.current().getWidth();

    boolean leftMovement = movingLeft();

    Matrix matrix = new Matrix();
    matrix.setScale(leftMovement ? -scale : scale, scale);
    matrix.postTranslate(leftMovement ? currentBounds.right : currentBounds.left, currentBounds.top);

    canvas.drawBitmap(image.current(), matrix, Paints.BLANK);

But this "leftMovement ? currentBounds.right : currentBounds.left" does not looks right

Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167