Hello everyone!
I'm making an app that will rotate a bitmap that contains the map of my university based on compass changes.
I have a squareish pictucre of my university and nearby area, on my phone it's a bit flattened:
Now i only need to display the campus area, i achieved that by drawing a Rectangle part of that bitmap on my custom View's canvas:
My issue is that i'm unable to achieve rotating the bitmap around the center point.
Right now rotation by 30 degrees looks like this:
and it doesn't do it's job at all, the campus disappears from the smaller rectangle.
I wan't the image to be rotated around it's center and i want the corners of the image to go off the canvas, so the campus stays in the same place and rotates nicely.
To display the map i have custom View with overriden onDraw method.
@Override
protected void onDraw(Canvas canvas) {
Rect rekt = new Rect();
Rect tyrRekt = new Rect();
super.onDraw(canvas);
tyrRekt.set(0, 0, canvas.getWidth(), canvas.getHeight());
rekt.set(350, 370, 1520, 900);
canvas.drawBitmap(bitmap, rekt, tyrRekt, null);
}
and the bitmap is rotated in AsyncTask to save some memory, RotateTaskParams is an object with oryginal bitmap and degrees:
@Override
protected Bitmap doInBackground(RotateTaskParams... params) {
Matrix matrix = new Matrix();
matrix.postRotate(params[0].deg);
rotateBitmap = new WeakReference<Bitmap>(Bitmap.createBitmap(params[0].bmp, 0, 0 ,params[0].bmp.getWidth(), params[0].bmp.getHeight(), matrix, true ));
return rotateBitmap.get();
}
I was searching for a lot, read tons of simliar questions here, and tried a lot of answers. What didn't work out for me (maybe i did something wrong)
postRotate around center of the bitmap
matrix.postTranslate(params[0].deg, params[0].bmp.getWidth()/2 , params[0].bmp.getHeight()/2);
the params[0].bmp is always a oryginal image.
Thanks in advance!
What helped my achieve the wanted result with good performance was switching from bitmap drawing to Google Maps, it's API has everything needed here