Moondustt
Moondustt

Reputation: 884

Bitmap Matrix postRotate changes X and Y axis direction

I'm trying to rotate my Bitmap using a readymade solution I found somewhere. The code is below:

      public void onDraw(Canvas canvas) {
                float x = ship.Position.left;
                float y = ship.Position.top;
                canvas.drawBitmap(ship.ship, x,y,null);
                invalidate();
      }

However, when I do it, the X and Y axii change their direction - if I increase the Y the image goes towards the top of the screen, not towards the bottom. Same happens to X if I rotate by 90 degrees.

I need to rotate it but without changing the Y and X axii directions.

Even rotated, I still want the Bitmap to go towards the bottom if I increase Y and to the right if I increase the X.

     public void update()
     {
       if(!moving)
         {
           fall();
         }
       else //moving
         {
           move();
          faceDirection();
         }
       Position.top += Speed;
      }

private void move() {
   if(Speed < MAXSPEED)
        Speed -= 0.5f;  
 }
private void fall() {
    if(Speed > MAXSPEED*-1)
    Speed += 0.2f;  
 }
private void faceDirection() {

    double OldDiretion = Direction;
    Direction = DirectionHelper.FaceObject(Position, ClickedDiretion);
    if (Direction != OldDiretion)
    {
        Matrix matrix = new Matrix();
        matrix.postRotate((float)Direction);
        ship = Bitmap.createBitmap(ship, 0, 0, ship.getWidth(),ship.getHeight(), matrix, false);
    }

I tried the code above, but it's still changing the Y direction, It's going to bottom of the BitMap, not bottom of the screen.

Here is the project: https://docs.google.com/file/d/0B8V9oTk0eiOKOUZJMWtsSmUtV3M/edit?usp=sharing

Upvotes: 1

Views: 2230

Answers (2)

WarrenFaith
WarrenFaith

Reputation: 57702

You should first rotate, than translate:

matrix.postTranslate(x, y);
matrix.postRotate(degree);

alternative would be to try to use preRotate() instead of postRotate().

I also strongly recommend to translate/rotate the original while drawing. So your createBitmap() call shouldn't modify the orientation. At least not when you change it dynamically on user interaction. Otherwise you would create a lot of bitmaps to represent rotations over and over again which would impact the performance.

Upvotes: 1

Shade
Shade

Reputation: 10011

The problem is that you don't actually rotate the bitmap - you just draw it rotated. So, the next time you redraw it, you first push it towards the bottom or right by incrementing x/y and then rotate it.

You have to actually rotate the bitmap itself. You could use the following code:

ship.ship = Bitmap.createBitmap(ship.ship, 0, 0, ship.ship.getWidth(), ship.ship.getHeight(), matrix, false);

Here you create a new rotated bitmap and set your reference to point to it.

Note! You must do this only once! So you can't do it in the onDraw method, since then it will get rotated every time it's redrawn. You have to do it somewhere else and then draw it as usual in onDraw (without the matrix rotations).

Upvotes: 0

Related Questions