0

I searched through this site to find out how to rotate a image back and forth and came up with my own with the help of other post on this site, it works great, the only problem is now I got it working so it rotates back and forth the way I like it, how the heck do I now position it on the screen? heres the code so far:

 private Bitmap ray;
    private  int mRot = -33;
    private boolean goRight = true;

    void onRotDraw(Canvas canvas)
    {
        int width = ray.getWidth();
        int height = ray.getHeight();

        if (mRot > 30){
            goRight = false;
        }
        if (mRot < -30){
            goRight = true;
        }
        if (goRight){
            mRot += 1;
        }else
        {
            mRot -= 1;
        }
               canvas.rotate(mRot,width / 2,height / 2);
               this.Draw_Sprite(canvas, 0, mRot ); 

    }

    public void Draw_Sprite(Canvas c, int whatever, int rot) {  
        //rotating image back and forth
        int width = ray.getWidth();
        int height = ray.getHeight();
         Rect src = new Rect(width - width, height - height, width, height);
         Rect dst = new Rect(width - width, height - height,   width,  height); 
         c.drawBitmap(this.ray, src, dst, null);
    }

Usually with drawBitmap I use it to position my images but this one uses the src and dst instead for the size of the rectangle, so now it works great but how would I change the position on the screen from 0,0 to where I want it to go?

this was taken partially or mostly from this post: Sprite Rotation in Android using Canvas.DrawBitmap. I am close, what am I doing wrong?

Which gave me all I needed to get this going except how to set the x and y position on the screen, any help with this would be greatly appreciated, I've been wrecking my brains out trying to find a way to get this set the x and y positions with no luck and its probably something simple. Thanks in advance.

Community
  • 1
  • 1
user870286
  • 67
  • 2
  • 12

1 Answers1

1

This is a piece of code from that other post you referenced:

Rect src = new Rect(0, 0, width, height);
Rect dst = new Rect(x, y, x + width, y + height);

It's the x and y in the dst Rect that determine the location of the bitmap.

Your code:

Rect src = new Rect(width - width, height - height, width, height);
Rect dst = new Rect(width - width, height - height,   width,  height);

Is going to always draw it at 0, 0 because width - width will be 0 and height - height will be 0.

Joe
  • 3,059
  • 2
  • 22
  • 28
  • that was stupid of me, ok I got that part, thanks for this but now I came up against another issue, when I set the dst and add 50 to everything it doesn't rotate properly now, even if I add the same numbers to both src and dst it comes out rotating all wonky (if thats a word) but if I have them all like I had it it rotates beautifully, is this a scaling issue? I tried dst = newRect(50,50,50+width,50+height) – user870286 Mar 13 '12 at 00:50
  • never mind I'm a moron lol I forgot to add the 50 to other part as well canvas.rotate(mRot,50 + (width / 2),50 + (height / 2)); it all works great now, thank you for answering this simple one for me, I appreciate the time you took for this. – user870286 Mar 13 '12 at 01:13