[英]Android rotating bitmap around center point with cropping the corners
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: 现在,我只需要显示校园区域,就可以通过在自定义视图的画布上绘制该位图的Rectangle部分来实现:
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: 现在旋转30度看起来像这样:
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. 为了显示地图,我使用覆盖了onDraw方法的自定义视图。
@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: 并且位图在AsyncTask中旋转以节省一些内存,RotateTaskParams是具有原始位图和度的对象:
@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 postRotate围绕位图的中心
matrix.postTranslate(params[0].deg, params[0].bmp.getWidth()/2 , params[0].bmp.getHeight()/2); matrix.postTranslate(params [0] .deg,params [0] .bmp.getWidth()/ 2,params [0] .bmp.getHeight()/ 2);
the params[0].bmp is always a oryginal image. params [0] .bmp始终是原始图像。
Thanks in advance! 提前致谢!
Just try 你试一试
matrix.postRotate(90);
Bitmap rotated = Bitmap.createBitmap(originalBitmap, 0, 0, originalBitmap.getWidth(),
originalBitmap.getHeight(), matrix, true);
and in your xml, set android:scaleType="centerCrop" to your ImageView tag. 然后在您的xml中,将android:scaleType =“ centerCrop”设置为ImageView标签。 Hope it helps.
希望能帮助到你。
帮助我以良好的性能实现所需结果的方法是从位图绘制切换到Google Maps,它的API具备了这里所需的一切
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.