Rotate an image around center - android

How can I rotate an image around it's center point? This rotates it but also moves it:
Matrix mat = new Matrix();
mat.postRotate(45);
Bitmap bMapRotate = Bitmap.createBitmap(dialBM, 0, 0, dialBM.getWidth(),dialBM.getHeight(), mat, true);
dial.setImageBitmap(bMapRotate);
I've checked other examples on this site, but they either fail to work or use canvas, I do not wish to use canvas.

The second and third arguments to postRotate is the x and y pivot point.
mat.postRotate(45, dialBM.getWidth()/2, dialBM.getHeight()/2);

Probably because your matrix rotates around (0,0), and not the middle of your bitmap. You should declare two additional matrices - one for moving the bitmap's center to (0,0) (shift by -getWidth()/2, -getHeight(2)) and one to move the bitmap's center back to (0,0). Multiply the three matrices, and then the result.

Related

How to find center position of a bitmap that rotated and scaled with a matrix?

I want to calculate center of a bitmap that is drawn on a canvas with a matrix, it can be rotated, scaled or translated with a arbitrary value. What is the easiest way to find center of the this bitmap on canvas?
You need to apply the matrix to the coordinates of the center of bitmap.
If you use a canvas that has a transformation matrix, you can get the final matrix through Canvas.getMatrix()
If you draw the Bitmap on the Canvas with a Matrix : drawBitmap(bitmap, matrix, paint), then you you need to use that Matrix, or concatenate it to that of the Canvas (in the case it has one).
Then you can finally apply that matrix to the center of the matrix using Matrix.mapPoints.
Something like :
canvas.drawBitmap(bitmap, bitmap_transform, paint);
Matrix full_transform = new Matrix(canvas.getMatrix());
full_transform.postConcat(bitmap_transform);
float[] center = new float[] {bitmap.getHeight()/2.0, bitmap.getWidth()/2.0};
full_transform.mapPoints(center);
Alternatively, if you apply transformations to your bitmap without a matrix, you can use the full_transform.postRotate, full_transform.postScale, etc with the same values. In particular, if you draw your bitmap with drawBitmap(bitmap, left, top, paint) then you need to do a full_transform.postTranslate(left, top).
If you're looking for the "easiest" way then just sticking with the translate rotate and scale functions would solve the problem. The reason that those were developed was so developers wouldn't have to do vector calculus for simple animations. Also the only value you would have to actually calculate in that sense it is the translate value after you take into account the original coordinates.

How do I rotate bitmap on canvas around center x y in android

I am drawing a number of shapes on a canvas. One such shape is a bitmap. Say the center of the bitmap on the canvas is at center xCenter, yCenter such that to draw I call
xLeft = xCenter+mBitmap.getWidth()/2;
yTop = yCenter+mBitmap.getHeight()/2;
canvas.drawBitmap(mBitmap, x,y, mFilledPaint);
So far everything works fine. The next step is to rotate the bitmap around the center xCenter, yCenter. My code below is not doing it. It moves the bitmap all over the place, whereas all I want is for the image to rotate in place around its own center. How can I fix the code below? I already looked at Android: How to rotate a bitmap on a center point and at best I don't understand the responses there.
My code
xLeft = xCenter+mBitmap.getWidth()/2;
yTop = yCenter+mBitmap.getHeight()/2;
matrix.postRotate(30f, xLeft, yTop);
canvas.drawBitmap(mBitmap, matrix, mFilledPaint);
also replacing xLeft and yTop with 0 does not seem to work.. any idea ??
Edit:
xCenter, yCenter is not the center of the canvas. It's just the point where the center of the bitmap lands.
i guess you mean, you can not understand following code:
this.matrix.reset();
this.matrix.setTranslate(this.floatXpos, this.floatYpos);
this.matrix.postRotate((float)this.direction, this.getCenterX(), this.getCenterY());
I think i could give some explanation about this, since i done this task in my project before:
the first line to rest it to identity, which means do thing, because any thing times identity matrix get itself.
the second line, move the original point to the target position,this is actually specify xLeft and yTop to which the bitmap will draw in canvas.
the thrid line, rotate around center point (xLeft+bitmapWidth/2,yTop+Height/2)
To understand this, you need to know how the matrix is used.basically, you bitmap (the points in bitmap is a matrix actually) times the matrix you created here, then it get a new matrix, canvas just draw that new matrix.

Possible to calculate the width/height of a rotated matrix?

If I use this:
mCamera.rotateY(45);
mCamera.getMatrix(mMatrix);
mMatrix.preTranslate(-pivotX, -pivotY);
mMatrix.postTranslate(pivotX + centerX, pivotY + centerY);
And do:
canvas.drawBitmap(bitmap, mMatrix, null);
Then the drawn picture will be taller and slimmer and the matrix will be nicely applied to the whole picture. Now, is there any way to calculate the rotated size? I want to fit the image by scaling it and now when I rotate it some of the top and bottom is clipped because of the parent's constraints. Thanks!
EDIT:
What I am going to do is spin the image and the range for rotateY will be from 0 to 90.
Well, you could easily let the matrix map the corners of your bitmap and then calculate the bounds, as the mapped corners will be the max / min for x and y coordinate. Maybe you can do it without too many if clauses :)
Edit:
Check out
RectF r = new RectF(/*your bitmap's corners*/);
matrix.mapRect(r);
That way you should get r's new size.
If you stick to 45° then Pythagoras is the key

Android Rotation around alternate axis using matrix

I'm using the matrix.rotate(deg, fx,fy) method to rotate an image.
The image has an alpha channel and only has something visual in the right-hand corner. I want to rotate the canvas, create the new image with that matrix and have the visual part be the only part that is rotating.
At the moment the image rotates but doesn't rotate around the desired axis. It always rotates around the original width of the object.
Here is the code I use:
Matrix matrix = new Matrix();
matrix.setRotate(rotation, xPivot, yPivot);
Bitmap pic1a = Bitmap.createBitmap(pic1, 0, 0, pic1.getWidth(), pic1.getHeight(), matrix, true);
This rotates the image correctly but only seems to rotate within the same location of the original canvas.
What I want is for the point that the image rotates around (xPivot, yPivot) becoming the centre location on the display.

Adding image to screen according to user's slide

I would like to display a transperent PNG of a "light" line shape according to user's sliding path.
I'd like to make similar effect like Fruit Ninja has, and leave a track after user slides his finger.
I already have the x,y points of his finger - using onTouch method, and checking the x,y on MotionEvent.ACTION_DOWN and MotionEvent.ACTION_UP but how do i draw an image that will be tilted and displayed at those positions? all i know is to add padding/margin to an image, not how to place it using x,y, or how to rotate it..
For positioning and rotating use a canvas (getSurfaceHolder().lockCanvas()) and draw inside it using drawBitmap.
public void drawBitmap (Bitmap bitmap, Matrix mtx, Rect dst, Paint paint)
the matrix can include the rotation:
Matrix mtx = new Matrix();
mtx.postRotate(90);
You might want to se the code from API demos, FingerPaint.

Categories

Resources