I am new to android,
I want to rotate a canvas bitmap without cut.
I used the below code, but when I rotate the image it cuts off a portion from the bitmap.
float px = iv_photo.getWidth()/2;
float py = iv_photo.getHeight()/2;
matrix.postTranslate(-canvas.getWidth()/2, -canvas.getHeight()/2);
matrix.postRotate(90);
matrix.postTranslate(px, py);
canvas.drawBitmap(bitmap, matrix, paint);
Related
I want to place a bitmap on top of another bitmap, I have done that with the method below. The bitmap is the current bitmap that will have another bitmap placed on top. Marker bitmap is the bitmap that is to be placed on top of the current bitmap. x and y are the locations of the tap, these are correct to my knowledge because the on tap listener does recognize the location but displays the bitmap in the wrong location.
public Bitmap drawOnToCanvas(Bitmap bitmap){
float centerX = (x - (markerBitmap.getWidth()/2));
float centerY = (y + (markerBitmap.getHeight()/2));
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(bitmap, new Matrix(), null);
canvas.drawBitmap(markerBitmap, centerX , centerY, null);
return bitmap;
}
I found the problem when you draw the bitmap for the current bitmap make sure you get the matrix and not just make a new matrix. Example fix is below:
public Bitmap drawOnToCanvas(Bitmap bitmap, Matrix currentMatrix){
float centerX = (x - (markerBitmap.getWidth()/2));
float centerY = (y + (markerBitmap.getHeight()/2));
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(bitmap, currentMatrix, null);
canvas.drawBitmap(markerBitmap, centerX , centerY, null);
return bitmap;
}
I have an image in drawable folder for clock hand. I want to rotate it in fixed point like a clock hand. I have tried below code which rotates the hand in circular path around a fixed point but not as like clock hand.
Matrix matrix = new Matrix();
matrix.reset();
matrix.preTranslate(0, 0);
matrix.postRotate(angleRotation, -0, -0);
matrix.postTranslate(240, 480);
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(bitmap, matrix, null);
I am struggling for hours to get it sorted out. Any help will be greatly appreciated.
I have tried below links for help as well with no success.
SurfaceView, draw image and reduce size, rotate image
Android: How to rotate a bitmap on a center point
How to rotate a bitmap in Android about images center smoothly without oscillatory movement
I found the answer. Let px and py be any point on canvas. bitmap.getWidth()/2 is middle point along bitmap width. It can be any point for x cordinate. For y cordinate I have taken it as 10. angleRotation is the angle that is as per required.
So matrix.postTranslate(-bitmap.getWidth()/2, -10); is the point of rotation.
Below is the code.
Matrix matrix = new Matrix();
matrix.reset();
Paint paint = new Paint();
float px = 240;
float py = 480;
matrix.postTranslate(-bitmap.getWidth()/2, -10);
matrix.postRotate(angleRotation);
matrix.postTranslate(px, py);
canvas.drawBitmap(bitmap, matrix, paint);
Above code satisfy my requirements. Please modify it as per your need.
I use this to rotate my bitmaps.
private Bitmap rotateImage(Bitmap src,int degrees) {
Matrix matrix = new Matrix();
matrix.postRotate(degrees);
return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
}
How do I specify an offset for the bitmap that will gave me the circle in another x, y position on the canvas?
Bitmap bitmap = Bitmap.createBitmap(size, size, Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
Rect rect = new Rect(120, 120, 150, 0);
canvas.drawCircle(size/2, size/2, size/2, paint);
canvas.drawBitmap(bitmap, rect, rect, paint);
For the Bitmap:
The second rect will be where the bitmap is drawn on the canvas.
Otherwise if you are drawing the entire bitmap you could use canvas.drawBitmap(bitmap, x, y, paint) Where x is a float specifying the left side position of the bitmap and y is a float specifying the top side position of the bitmap.
See Canvas.drawBitmap
As far as the drawCircle() goes. The first two parameters would be floats for the x and y position of the circle respectively.
See Canvas.drawCircle
I have a square shape image which having circle view, I am drawing that image in center of circle using canvas using below code:
Bitmap bitmapCompass = BitmapFactory.decodeResource(getResources(), R.drawable.compass_rim);
float w2= (w-bitmapCompass.getWidth())/2;
float h2= (h-bitmapCompass.getHeight())/2;
canvas.drawBitmap(bitmapCompass, w2, h2, paint);
I want to rotate this image like a dialer relative to center.Facing issue to make rotation of Image around that circle center.
Used code for rotation:
Bitmap bitmapCompass = BitmapFactory.decodeResource(getResources(), R.drawable.compass_back);
float w2= (w-bitmapCompass.getWidth())/2;
float h2= (h-bitmapCompass.getHeight())/2;
Matrix matbitmapCompass = new Matrix();
matbitmapCompass.postRotate((float)90);
Bitmap bmpRotateCompass = Bitmap.createBitmap(bitmapCompass, 0,0, bitmapCompass.getWidth(), bitmapCompass.getHeight(), matbitmapCompass, true);
canvas.drawBitmap(bmpRotateCompass, w2, h2, paint);
Help will be appreciated.
Thanks
I think you're going to want to rotate the canvas:
canvas.save();
canvas.rotate(angle, rotationCenterX, rotationCenterY);
// draw here
canvas.restore();
I have tried several hours to rotate a bitmap with no success. I have read numerous articles about this subject on this web site and it seems the prefered solution involves creating a temporary canvas. Well I did this and I still do not see a roated bitmap.
My bitmap is a 40x40 blue square and I am trying to rotate it 45 degrees. Thats not asking for much is it? When the code runs, the bitmap that appears on the screen is the non-rotated original. ( I have also tried a translate with no success as well)
Here is my code:
// Load the bitmap resource
fillBMP2 = BitmapFactory.decodeResource(context.getResources(), R.drawable.test1);
// Copy to a mutable bitmap
mb = fillBMP2.copy(Bitmap.Config.ARGB_8888, true);
// Create canvas to draw to
Canvas offscreenCanvas = new Canvas (mb);
// Create Matrix so I can rotate it
Matrix matrix = new Matrix();
matrix.setRotate (45);
offscreenCanvas.setMatrix (matrix);
// Send the contents of the canvas into a bitmap
offscreenCanvas.setBitmap(mb);
Later in an OnDraw I do the following:
canvas.drawBitmap(mb, 200, 200, null);
Any ideas what I am doing wrong? Seems like it should work.
Thanks
Try using this
Matrix matrix = new Matrix();
matrix.setRotate(15);
canvas.drawBitmap(bmp, matrix, paint);
setRotation method takes in a float representing
the degrees of rotation.
Try this...
Matrix matrix = new Matrix();
float px = 200;
float py = 200;
matrix.postTranslate(-bitmap.getWidth()/2, -bitmap.getWidth()/2);
matrix.postRotate(45);
matrix.postTranslate(px, py);
canvas.drawBitmap(bitmap, matrix, paint);
You definitely want to use transformations: check out this link.
Basically it's this:
// save the canvas
ctx.save();
// move origin to center
ctx.translate(x,y);
// rotate
ctx.rotate(angle * (Math.PI / 180));
// draw image
ctx.drawImage(image, x, y, w, h, .w/2, h/2, w, h);
// restore
ctx.restore();