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.
Related
I have one png image.
Then, I want to flip this triangle as on the next image.
Red region it is our original image which is reflected and turned. And then I want to rotate new region (red region) on different angles.
How can i do it in android?
Thanks in advance!
To rotate an image in android you should look here
Rotate image in android
Or here
Android: Rotate image in imageview by an angle
To get a mirror image of your PNG, do this (taken from the link below, from Dalmas's answer)
Matrix matrix = new Matrix();
matrix.preScale(-1.0f, 1.0f);
Bitmap mirroredBitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.width(), bmp.height(), matrix, false);
How to mirror an image file? (2.2+)
To rotate a vector look here
Rotating a vector using Matrix.rotateM
I have a Canvas and a Bitmap.I have to draw bitmap twice on canvas and rotate them.This is my code:
Bitmap bitmap = ... ;
Matrix matrix = new Matrix();
Camera camera = new Camera();
camera.save();
// First drawing:
camera.rotateX(x_rotate_angle);
camera.rotateY(y_rotate_angle);
camera.getMatrix(matrix);
matrix.postTranslate(0, 0);
matrix.postScale(3, 3);
canvas.drawBitmap(bitmap, matrix, null);
camera.restore();
// Second drawing:
camera.rotateX(x_rotate_angle);
camera.rotateY(y_rotate_angle);
camera.getMatrix(matrix);
matrix.postTranslate(10, 10 + bitmap.getHeight());
matrix.postScale(3, 3);
canvas.drawBitmap(bitmap, matrix, null);
Result before rotation:
When bitmaps rotate only axis X,all things are fine.In this case result looks like this:
In both above images you can see that all edges are parallel.But when bitmaps rotate axis Y,edges will not be parallel(bottom edge of top bitmap crosses top edge of bottom bitmap):
Did I do any thing wrong?
You're not doing anything wrong, it's just that you haven't understood the results you were going to get from your code. The camera you're using is a 3D camera and you're seeing the results of moving the camera in 3D space. When you rotate a camera, parallel lines do not in general remain parallel. You noticed the artifact when rotating about the y-axis, but it's also present in your illustration for the x-axis rotation. Notice that for the x-axis, the left and right sides are not parallel; the left side is closer to vertical than the right.
The Android canvas object has scale, skew, translate, and rotate operations. If it's 2D operations that you need, you can ditch the camera object entirely.
I'm writing an android application.By using it,a user can crop the bitmap image.But I want to add more function,that is to rotate the image before cropping.
I followed the below steps to accomplish this purpose.
built a custom view.
initialized BitmapDrawable() and a cropping rectangle(Rect()) [the rectangle determines where to cop in the bitmap]
override onTouch(MotionEvent) method for two functions
a. user can move the bitmap over the screen
b. user can transform the cropping rectangle
override onDraw(Canvas) method to update the screen.This code is for showing bitmap.
bitmap_drawable.draw(canvas,_bitmap_paint);
Problem:1
This step ,that I don't know how to do, is to show a rotated properly. I used this code to rotate the canvas in the onDraw(Canvas) method.
canvas.rotate(_angle);
But it makes all the things on the screen (including the cropping rectangle) to rotate.
Question:2
Is it a wise way to initialize my own Canvas() object to draw the bitmap on?
Note:
Here,the rotation that I want,is very accurate up to 1 degree and is performed by two-finger rotation gesture so that performance is very important and the following code(using matrix) is bad.
Matrix mat = new Matrix();
mat.preRotate(_degree, _one_finger_x, _one_finger_y);
Bitmap _new_bitmap = Bitmap.createBitmap(_old_bitmap, 0, 0, bmWidth, bmHeight, mat, true);
Therefore,I think initializing a separate Canvas() and using canvas.rotate(_degree) is very appropriate.However,this makes controlling touch events quite difficult.Any ideas?Please help.
Problem:2
Suppose that I can now rotate the bitmap separately from all objects,i.e. the cropping rectangle,etc.This step is to crop the bitmap and show the cropped result to the user efficiently.When rotate function is not included, I simply copy the color bytes within the cropping rectangle on the old bitmap as follow.
Bitmap _new_bitmap = Bitmap.createBitmap( _old_bitmap, _cropRectx, _cropRecty, _cropRectWidth, _cropRectHeight, null);
But here,from a rotated bitmap, how can I clone the color bytes through a crossing crop rectangle?
Question:2
How can I crop a rotated bitmap?
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.
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.