Rotate Image In Android
I want rotate image in both the ways Clockwise as well as Anti clock wise. I had try but not rotate image both the way, so plz give me solution for my problem if you know.. Thanks
Solution 1:
Use the code below
publicclassRotateimageextendsActivity {
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);
// or just load a resource from the res/drawable directory:BitmapmyBitmap= BitmapFactory.decodeResource(getResources(), R.drawable.flo);
// find the width and height of the screen:Displayd= getWindowManager().getDefaultDisplay();
intx= d.getWidth();
inty= d.getHeight();
// get a reference to the ImageView component that will display the image:ImageViewimg1= (ImageView)findViewById(R.id.imageView1);
// scale it to fit the screen, x and y swapped because my image is wider than it is tallBitmapscaledBitmap= Bitmap.createScaledBitmap(myBitmap, y, x, true);
// create a matrix objectMatrixmatrix=newMatrix();
matrix.postRotate(45, 90, 180);
// create a new bitmap from the original using the matrix to transform the resultBitmaprotatedBitmap= Bitmap.createBitmap(scaledBitmap , 0, 0, scaledBitmap .getWidth(), scaledBitmap .getHeight(), matrix, true);
// display the rotated bitmap
img1.setImageBitmap(rotatedBitmap);
}}
Solution 2:
Here is the base code to load a bitmap and rotate it left or right:
// Load a bitmap from a drawable, make sure this drawable exists in your projectBitmapsprite= BitmapFactory.decodeResource(this.getResources(),
R.drawable.ic_launcher);
// Create two matrices that will be used to rotate the bitmapMatrixrotateRight=newMatrix();
MatrixrotateLeft=newMatrix();
// Set the matrices with the desired rotation 90 or -90 degrees
rotateRight.preRotate(90);
rotateLeft.preRotate(-90);
// Create bitmaps based on the loaded bitmap 'sprite' and apply one of// the rotation matricesBitmaprSprite= Bitmap.createBitmap(sprite, 0, 0,
sprite.getWidth(), sprite.getHeight(), rotateRight, true);
BitmaplSprite= Bitmap.createBitmap(sprite, 0, 0,
sprite.getWidth(), sprite.getHeight(), rotateLeft, true);
Now go and use rSprite and lSprite.
Here is a full sample that actually draws the bitmaps to screen:
publicclassMainActivityextendsActivity {
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(newdrawView(this));
}
privateclassdrawViewextendsView{
publicdrawView(Context context){
super(context);
}
@OverrideprotectedvoidonDraw(Canvas canvas) {
super.onDraw(canvas);
// Load a bitmap from a drawable, make sure this drawable exists in your projectBitmapsprite= BitmapFactory.decodeResource(this.getResources(),
R.drawable.ic_launcher);
// Create two matrices that will be used to rotate the bitmapMatrixrotateRight=newMatrix();
MatrixrotateLeft=newMatrix();
// Set the matrices with the desired rotation 90 or -90 degrees
rotateRight.preRotate(90);
rotateLeft.preRotate(-90);
// Create bitmaps based on the loaded bitmap 'sprite' and apply one of// the rotation matricesBitmaprSprite= Bitmap.createBitmap(sprite, 0, 0,
sprite.getWidth(), sprite.getHeight(), rotateRight, true);
BitmaplSprite= Bitmap.createBitmap(sprite, 0, 0,
sprite.getWidth(), sprite.getHeight(), rotateLeft, true);
//Draw the first unrotated sprite at the top left of the screen
canvas.drawBitmap(sprite, 0, 0, null);
//Draw the rotated right sprite on the 2nd row
canvas.drawBitmap(rSprite, 0, sprite.getHeight() + 5, null);
//Draw the rotated left sprite on the 3rd row
canvas.drawBitmap(lSprite, 0, sprite.getHeight() * 2 + 5, null);
}
}
}
Solution 3:
You just add this simple code to your button
imVCature_pic.setRotation(imVCature_pic.getRotation() + 90);
Solution 4:
Matrixmat=newMatrix();
mat.preRotate(angle);///in degreeBitmapmBitmap= Bitmap.createBitmap(originalBmp, 0, 0, modWidth, modHeight, mat, true);
//originalBmp -> original img as bitmap//modHeight -> new height//modWidth -> new width
use the above code.
Post a Comment for "Rotate Image In Android"