Rotate An Image Multiple Times But Keep The Corners Aligned
Solution 1:
I tried your code and after some rotation it crashed with the OutOfMemory exception cause each time a new bitmap is created which is very resource intensive. You should never never! use createBitMap() in iteration. I made some modification to your image rotation code and now it's running as expected.
Here is the code:
private void addListeners() {
this.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Matrix matrix = new Matrix();
//copying the image matrix(source) to this matrix
matrix.set(imageView.getImageMatrix());
matrix.postRotate(10, imageView.getWidth()/2, imageView.getHeight()/2);
imageView.setImageMatrix(matrix);
//checking the size of the image
Drawable d = imageView.getDrawable();
Bitmap bmp = ((BitmapDrawable)d).getBitmap();
imageInfo(bmp);
}
});
}
also set the scale type of the imageView to matrix
<ImageView
android:id="@+id/Image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#336699"
android:scaleType="matrix"
android:padding="2px"
android:src="@drawable/m" />
And if we want to get the rotated bitmap from ImageView, do this:
private Bitmap getBitmapFromView() {
// this is the important code :)
// Without it the view will have a dimension of 0,0 and the bitmap will be null
imageView.setDrawingCacheEnabled(true);
imageView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
imageView.layout(0, 0, imageView.getMeasuredWidth(), imageView.getMeasuredHeight());
imageView.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(imageView.getDrawingCache());
imageView.setDrawingCacheEnabled(false); // clear drawing cache
return b;
}
I hope this helps.
Solution 2:
I can't seem to see your images, yet I had the same problem, each time I tried to rotate an image it seemed to resize its self.
I managed to write some code that could successfully turn my image. My first tip to you is that when you attempt to create an animated spinning picture you should not create a new Bitmap every time, since this will drive the GC crazy and reduce performance speed!
Here's the code that works for me, to spin an image without resizing it:
public static Matrix rotateMatrix(Bitmap bitmap, Shape shape, int rotation) {
float scaleWidth = ((float) shape.getWidth()) / bitmap.getWidth();
float scaleHeight = ((float) shape.getHeight()) / bitmap.getHeight();
Matrix rotateMatrix = new Matrix();
rotateMatrix.postScale(scaleWidth, scaleHeight);
rotateMatrix.postRotate(rotation, shape.getWidth()/2, shape.getHeight()/2);
rotateMatrix.postTranslate(shape.getX(), shape.getY());
return rotateMatrix;
}
Note: The shape object just contains the real dimensions of the object you are trying to spin, eg 100x100.
I hope this helps.
Solution 3:
You must rotate the first image. Save the first image into a variable and then create a copy. When you rotate, always make a fresh copy from the original and rotate that one.
EDIT
Change your code to this:
@Override
public void onClick(View view) {
Matrix matrix = new Matrix();
matrix.setRotate(10);
Bitmap copy = image;
Bitmap newImage = Bitmap.createBitmap(copy, 0, 0, image.getWidth(), image.getHeight(), matrix, true);
setNewImage(newImage);
}
Post a Comment for "Rotate An Image Multiple Times But Keep The Corners Aligned"