Android: image rotation not desire to resize - android

I have a code for rotate an ImageView, and good good.
The problem is that the size of image change in the rotate process and I desire that not happen.
The xml code of image
<ImageView
android:id="#+id/imgBallesta"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="#drawable/imgballesta"
android:contentDescription="#string/imageString"
/>
The code that creates the bitmap, I put this code in the declaration of class
Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.imgObjectImage);
And the code for rotate the image, this code is inluded and onTouchEvent
Matrix mat = new Matrix();
matrix.postRotate((float) (angle*180/Math.PI), image.getWidth(),image.getHeight());
Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 0, bMap.getWidth(), bMap.getHeight(), matrix, true);
image.setImageBitmap(bMapRotate);
Thanks for the help, and for read my question

Try with my method for rotate picture :
public static Bitmap rotate(Bitmap src, float degree) {
// create new matrix
Matrix matrix = new Matrix();
// setup rotation degree
matrix.postRotate(degree);
// return new bitmap rotated using matrix
return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
}
Good luck!!

I found a posoble solution to my problem. First I modify the size and height of my image and create the respective image for all drawable folders.
Then I use the next code for to animate the image.
Matrix matrix=new Matrix();
img.setScaleType(ScaleType.MATRIX); //required
matrix.postRotate((float) (a*180/Math.PI) , img.getWidth()/2, img.getHeight()/2);
img.setImageMatrix(matrix);
Thanks for response and read this question.

Related

Android: What is the correct way to rotate a bitmap?

The problem is when the bitmap gets rotated "R0" degrees, it looks like if it changed the pivot point. What I want is for it to rotate around the middle of the bitmap, but I don't know how.
Bitmap bitmap = (Bitmap) BitmapFactory.decodeResource(getResources(), R.drawable.base);
Matrix matrix = new Matrix();
matrix.reset();
matrix.setScale(bitmap.getWidth(), bitmap.getHeight());
matrix.setTranslate(-bitmap.getHeight()/2, -bitmap.getHeight()/2);
matrix.postRotate(R0);
Bitmap b = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
canvas.drawBitmap(b, x-24, y-40, null);
Thanks very much.
Try using matrix.postTranslate, instead of setTranslate, the latter will reset your matrix and setScale call will make no effect.

Get degree from the line that draw on bitmap

I have a project that will draw a line on a bitmap based on pixel coordinate and preview it with ImageView. The purpose of the line is to get degree from the line and then the degree will be use to rotate that bitmap. Is there any way to get the degree from the line that has been draw? Please give me suggestion how to do this.
Best Regards
This might help you, by this you can rotate your bitmap to 90 degrees actually
Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmapOrg,width,height,true);
Bitmap rotatedBitmap = Bitmap.createBitmap(scaledBitmap , 0, 0, scaledBitmap .getWidth(), scaledBitmap .getHeight(), matrix, true);
then you can set the rotated image as
imageView.setImageBitmap(rotatedBitmap);
You can use similar technique to get degree of bitmap

Set ImageView orientation directly in Android?

I understand that I can rotate an ImageView in Android by
matrix.postRotate(finalOrientation - initOrientation, width, height);
But I want to set it as finalOrientation directly instead of rotating it by the difference, finalOrientation - initOrientation.
So I am expecting something like:
matrix.setOrientation(finalOrientation);
How can I do that?
Perfect! thanks #Bette Devine!
I made a function just for convenience.
public Bitmap rotateBmp(Bitmap bmp){
Matrix matrix = new Matrix();
//set image rotation value to 90 degrees in matrix.
matrix.postRotate(90);
//supply the original width and height, if you don't want to change the height and width of bitmap.
bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(),bmp.getHeight(), matrix, true);
return bmp;
}
I had to do this with a downsized image. I got a "java.lang.OutOfMemoryError" when I tried to do this with a full size image. : (
Matrix matrix = new Matrix();
//set image rotation value to 90 degrees in matrix.
matrix.postRotate(90);
//supply the original width and height, if you don't want to change the height and width of bitmap.
Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 0, newWidth, newHeight, matrix, true);
Please refer the following link:
http://docs.xamarin.com/guides/android/application_fundamentals/handling_rotation/

error rotate image in android , image missing

I have code to rotate image like this
private void rotate(Bitmap src, float degree) {
// create new matrix
Matrix matrix = new Matrix();
// setup rotation degree
matrix.postRotate(degree);
// return new bitmap rotated using matrix
Bitmap ro = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
mImageView.setImageBitmap(ro);
recycleBitmap();
}
It work to rotate , but if I click rotate button continuously , my Image is not vissible , How to solve this? thanks
Try setting the postScale before the postRotate
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// rotate the Bitmap
matrix.postRotate(45);
What you are doing in your code is changing the martrix value each time you click on the rotate button.Changing values continuously may destroy the structure of the image.You can adjust or scale the matrix.Try this'
private void rotate(Bitmap src, float degree) {
// create new matrix
Matrix matrix = new Matrix();
matrix.postRotate(degree,px,py);//(px,py)is pivot point with reference to which the image is rotated
// return new bitmap rotated using matrix
Bitmap ro = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
mImageView.setImageBitmap(ro);
recycleBitmap();
}

Rotate rectangle bitmap in Android

I have a rectangle-shaped Bitmap, which I need to rotate it by 90 degrees clockwise or counter-clockwise.
I can do the rotation using this code:
Matrix matrix = new Matrix();
matrix.setRotate(90, originalBitmap.getWidth()/2, originalBitmap.getHeight()/2);
return Bitmap.createBitmap(originalBitmap, 0, 0, originalBitmap.getWidth(), originalBitmap.getHeight(), matrix, true);
However, this code rotates image "in-place" using old values for height/width. And the resulting image looks stretched and ugly.
Is there any good way to rotate the image by 90 degrees into new height/width? Probably, one possible solution is to modify dimensions of the original bitmap first?
Thanks
Don't you use old values while creating new bitmap? Just replace them in the last line:
return Bitmap.createBitmap(originalBitmap, 0, 0, originalBitmap.getWidth()/2, originalBitmap.getHeight()/2, matrix, true);

Categories

Resources