Search code examples
androidrotationdrawbitmap

android positioning a drawBitmap that uses rect


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.


Solution

  • 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.