Rotate a bitmap in android about a point without animation of view
I found some good resources for rotating a bitmap in android here and elsewher eon the net. I am close to getting my code to work but apparently I don't fully understand how the Matrix Translations work. The following is three main functions from my sp开发者_如何学编程rite class. THey worked great before I added things to facilitate the matrix rotation (namely in the onDraw I called the draw with just x,y and no matrix). I wrote some test code to add a sprite, and rotate it from 0 to 360 and back to 0 again repeatedly. It results it in rotating about like its orbiting some odd point. In reality I want it to just sit there and spin:
public void Rotate_Sprite(int transform, int deg)
{
int spriteCenterX = x+(width/2);
int spriteCenterY = y+(height/2);
mMatrix.setRotate(deg, spriteCenterX, spriteCenterY);
}
public void Draw_Sprite(Canvas c) {
//c.drawBitmap(images[curr_frame], x, y, null); //this worked great esp in move sprite
c.drawBitmap(images[curr_frame], mMatrix, null);
}
public void Create_Sprite(blah blah) {
...
...
mMatrix = new Matrix();
mMatrix.reset();
}
public int Move_Sprite() {
//with the matrix stuff, I assume I need a translate. But it does't work right
//for me at all.
int lastx=this.x;
int lasty=this.y;
this.x+=this.vx;
this.y+=this.vy;
mMatrix.postTranslate(lastX-x,lastY-y); //doesn't work at all
}
I did find this J2me like reference here. Though it seems to have all my sprites I call rotate on in orbit around one point.
Try this:
mMatrix.setTranslate(objectX,objectY);
mMatrix.postRotate(Degrees, objectXcenter, objectYcenter);
Basically, you need to translate your bitmap to the location you want it first, set it there, then rotate it N Degrees around its center.
I haven't worked on the android, and It's been a while since I last worked with matrices, but it sounds like you're rotation is working properly and you just forgot to translate so the point to rotate around is at 0,0. What you're going to want to do if this is in fact the problem is translate the sprite so that it's world location is 0,0; rotate the sprite; then translate it BACK to wherever it was before. This should all occur before drawing that frame, so the translation itself will never be seen.
Hope this helps.
For anybody who finds this and is trying to do the same sort of thing:
I am rotating my images like this:
//create all your canvases and bitmaps and get sizes first
Bitmap minBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.minute);
minCanvas..setBitmap(minBitmap);
int height = min.getHeight();
int width = min.getWidth();
//Bitmap minBitmap = Bitmap(width, height, Bitmap.Config.ARGB_8888); //not using in example
//The basically applies the commands to the source bitmap and creates a new bitmpa. Check the order of width and height, mine was a square.
minMatrix.setRotate(minDegrees, width/2, height/2);
Bitmap newMin = Bitmap.createBitmap(minBitmap, 0, 0, (int) width, (int) height, minMatrix, true);
//apply this to a canvas. the reason for this is that rotating an image using Matrix changes the size of the image and this will trim it and center it based on new demensions.
minCanvas2.drawBitmap(newMin, width/2 - newMin.getWidth()/2, height/2 - newMin.getHeight()/2, null);
//Then you can use it the way you want, but I create a bitmap from the canvas
minCanvas2.setBitmap(minBitmap);
I have not run this code, this was typed out while looking at my code that I actually do run.
HTH
精彩评论