I have bitmap which i mirror using the matrix
mMirror.postScale(-1.0f, 1.0f);
and can "un-mirror" using
mMirror.postScale(1.0f, 1.0f);
However,now when I rotate the bitmap using mRRight.postRotate(90) , the mirroring is lost.Why?
EDIT
Figured a part of the answer for the.When you create a Bitmap object,an Identity matrix is used and created internally which corrects all the mirroring etc.
Now the question changes to how do I Mirror and then rotate a matrix.
Related
I am working on a project where I need to ZoomIn and rotate the image simultaneously on seekbar progress value.
I have tried below code. But this is not working. This is just rotating the image.
Matrix matrix = new Matrix();
matrix.postRotate(curValue);
//matrix.postScale(curValue, curValue);
Bitmap resizedBitmap = Bitmap.createBitmap(bitmaprotate, 0, 0, bmpWidth, bmpHeight, matrix, true);
mainImage.setImageBitmap(resizedBitmap);
For achieving this you better to use custom view and canvas. Using canvas you can easily rotate and scale the images. Please refer this links (resize the bitmap canvas ,Android Scaling Canvas Bitmap , http://www.barebonescoder.com/2010/06/android-development-simple-2d-graphics-part-1/)
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'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.
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?
rotate image on canvas around its center . i tried some example from net but i am failed
if i am using bitmap.creatbitmap with matrix i am getting error
Please suggest me which is better
Thanks in advance
Aswan
You can use a matrix to rotate. First you set the position (I'm using the coordinates of the bitmap's centre). Then apply a rotation. Then draw using your matrix.
Matrix transform = new Matrix();
transform.setTranslate(xOfCentre, yOfCentre);
transform.preRotate(turnDegrees, width/2, height/2);
canvas.drawBitmap(bitmap, transform, null);
If you want your turning to be animated, then see my answer to "animating and rotating an image...".