如何使用矩阵在中心点上旋转android中的位图

发布于 2025-01-06 07:28:12 字数 905 浏览 3 评论 0原文

我使用矩阵来旋转我的图像,它可以工作,但我对新图像位图有一些问题。 即使我设置了枢轴,新位图仍有一些空间。我应该做什么来旋转图像,但我不想要空间。我使用下面的代码来旋转位图:

Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), R.drawable.sticker01); int 宽度 = bitmapOrg.getWidth(); int height = bitmapOrg.getHeight();

矩阵矩阵 = new Matrix(); 矩阵.postRotate(旋转,宽度/2,高度/2);

位图调整大小Bitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, 宽度、高度、矩阵、true); BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);

imageView.setBackgroundColor(Color.WHITE); imageView.setImageDrawable(bmd); imageView.setScaleType(ScaleType.FIT_XY);

图像视图 -->宽度和高度 = WRAP_CONTENT

旋转 0 度: 在此处输入图像描述

旋转 10 度: 在此处输入图像描述 它在真实图像周围有空间。

旋转 50 度: 在此处输入图像描述 它在真实图像周围有空间(甚至更大的空间)。

I use matrix to rotate my image, it's working but i have some problem with new image bitmap.
The new bitmap have some space even though I have set the pivot. What should i do to rotate the image but i don't want the space. I use this code below to rotate the bitmap:

Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), R.drawable.sticker01);
int width = bitmapOrg.getWidth();
int height = bitmapOrg.getHeight();

Matrix matrix = new Matrix();
matrix.postRotate(rotate, width/2, height/2);

Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
width, height, matrix, true);
BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);

imageView.setBackgroundColor(Color.WHITE);
imageView.setImageDrawable(bmd);
imageView.setScaleType(ScaleType.FIT_XY);

imageView --> width & height = WRAP_CONTENT

Rotate 0 degree:
enter image description here

Rotate 10 degree:
enter image description here
It has space around the real image..

Rotate 50 degree:
enter image description here
It has space around the real image (even bigger space)..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

大姐,你呐 2025-01-13 07:28:12

改变imageview的scaleType:

imageView.setScaleType(ScaleType.CENTER);

Change the imageview scaleType:

imageView.setScaleType(ScaleType.CENTER);
樱娆 2025-01-13 07:28:12

使用带有中值的矩阵后旋转

matrix.postScale(scale,scale,mid.x,mid.y);

Use matrix post rotate with mid value

matrix.postScale(scale, scale, mid.x, mid.y);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文