使用矩阵旋转位图

发布于 2024-09-14 01:29:15 字数 381 浏览 3 评论 0原文

使用矩阵旋转位图时,顶点不稳定。

 矩阵矩阵 = new Matrix();
    矩阵.postRotate(mDegree,100,100);
    mCompasstemp = Bitmap.createBitmap(mCompPic, 0, 0, mCompPic.getWidth(), mCompPic.getHeight(), 矩阵, true);
    mCompassHud.setImageBitmap(mCompasstemp);

我的代码的输出就像 -位图将旋转。 - 我的位图的顶点不稳定。 -位图正在调整大小

我需要禁用图像调整大小并使旋转稳定。您能否为此建议一个解决方案?

While rotating a bitmap using matrix, vertex is not stable..

  Matrix matrix = new Matrix();
    matrix.postRotate(mDegree,100,100);
    mCompasstemp = Bitmap.createBitmap(mCompPic, 0, 0, mCompPic.getWidth(), mCompPic.getHeight(), matrix, true);
    mCompassHud.setImageBitmap(mCompasstemp);

Output of my code is like
-bitmap will rotate.
-vertex of my bitmap is not stable.
-Bitmap is resizing

I need disable image resizing and make the rotation stable.Can you please suggest a solution for this?

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

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

发布评论

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

评论(4

八巷 2024-09-21 01:29:15

不是直接从原始位图创建新位图,另一个(更直接,恕我直言)选项是创建生成的位图,使用该位图创建画布,然后在画布上进行旋转/平移/缩放并绘制原始位图位图通过画布到新位图上。

基本上,您正在寻找:

  scaledImage = Bitmap.createBitmap (croppedWidth, croppedHeight, Bitmap.Config.ARGB_8888);

  Canvas offscreenCanvas = new Canvas (scaledImage);
  Matrix matrix = new Matrix();
  matrix.setRotate (rotations, centreX, centreY);
  matrix.postScale(scaleX, scaleY);
  offscreenCanvas.setMatrix (matrix);

  offscreenCanvas.drawBitmap (pickedImage, 0, 0, new Paint(Paint.DITHER_FLAG));

Rather than creating your new Bitmap directly from the original, another (more straight-forward, imho) option is to create the resultant Bitmap, create a Canvas with that Bitmap, then do your rotation/translation/scaling on the Canvas and draw the original Bitmap onto the new Bitmap via the Canvas.

Basically, you're looking, then, at:

  scaledImage = Bitmap.createBitmap (croppedWidth, croppedHeight, Bitmap.Config.ARGB_8888);

  Canvas offscreenCanvas = new Canvas (scaledImage);
  Matrix matrix = new Matrix();
  matrix.setRotate (rotations, centreX, centreY);
  matrix.postScale(scaleX, scaleY);
  offscreenCanvas.setMatrix (matrix);

  offscreenCanvas.drawBitmap (pickedImage, 0, 0, new Paint(Paint.DITHER_FLAG));
紫竹語嫣☆ 2024-09-21 01:29:15

不确定这是否是您想要的,但它可能会有所帮助。

Android 使用其内置的兼容性功能来针对不同像素密度的屏幕适当地缩放和渲染位图。缩放的方法有两种,预缩放和自动缩放。

它将从资源中预先缩放位图,并在内部绘制位图时自动缩放(这就是您使用 createBitmap 所做的事情)。

转到 http://developer.android.com/guide/practices/screens_support.html 并检查:
4.使用密度和/或尺寸特定的资源:
位图和九个补丁的预缩放和自动缩放

Not sure if this is what your looking for but it might help.

Android uses its built in compatibility features to scale and render a bitmap appropriately for screens with different pixel densities. There are two methods of scaling, pre-scaling and auto-scaling.

It will pre-scale bitmaps' from resources and auto-scales when the bitmap is being drawn internally (which is what your doing be using createBitmap).

Go to http://developer.android.com/guide/practices/screens_support.html and check under:
4.Use density and/or size-specific resources:
Pre-scaling and auto-scaling of bitmaps and nine-patches

冰雪梦之恋 2024-09-21 01:29:15

我已经尝试过这段代码,并且旋转在位图中心稳定

matrix.reset();
matrix.setRotate(degree, Xpos+bitmap.getWidth()/2, Ypos+bitmap.getHeight()/2);

,然后在画布中 doDraw()

canvas.setMatrix(matrix);               
canvas.drawBitmap(bitmap, Xpos, Ypos, null);
canvas.setMatrix(null);

Xpos 和 Ypos 是位图的 X 和 Y 位置
setMatrix(null),将矩阵设置为null,这样旋转就不会影响后面的位图
而且它并不总是创建新的位图,因此对于性能来说非常有用
我希望有帮助

I have tried this code, and the rotate is stable at the center of the bitmap

matrix.reset();
matrix.setRotate(degree, Xpos+bitmap.getWidth()/2, Ypos+bitmap.getHeight()/2);

and then in canvas doDraw()

canvas.setMatrix(matrix);               
canvas.drawBitmap(bitmap, Xpos, Ypos, null);
canvas.setMatrix(null);

The Xpos and Ypos is the X and Y position of the bitmap
The setMatrix(null), set the matrix to null, so that the rotate didn't affect the after bitmap
And it didn't always create new bitmap, so it's great for performance
I hope that help

孤寂小茶 2024-09-21 01:29:15

我知道这是一个老问题,但是,所有带有代码的答案都意味着画布,所以这里有一个适合我的没有画布的解决方案:

Matrix matrix = new Matrix();
matrix.postRotate(mDegree,100,100);
mCompasstemp = Bitmap.createBitmap(mCompPic, 0, 0, mCompPic.getWidth(),
    mCompPic.getHeight(), matrix, true);

mCompasstemp = Bitmap.createScaledBitmap(mCompassTemp, mCompPic.getWidth(),
    mCompic.getHeight(), false);

mCompassHud.setImageBitmap(mCompasstemp);

所以基本上在旋转位图后,您将其重新缩放到所需的大小。可能不是最佳实践,但至少如果您不想/不需要的话,您不必创建额外的画布,因为涉及 Paint() 对象也不是一个便宜的操作。

I know its an old question but, all answers with code imply a canvas, so heres a solution without a canvas that worked for me :

Matrix matrix = new Matrix();
matrix.postRotate(mDegree,100,100);
mCompasstemp = Bitmap.createBitmap(mCompPic, 0, 0, mCompPic.getWidth(),
    mCompPic.getHeight(), matrix, true);

mCompasstemp = Bitmap.createScaledBitmap(mCompassTemp, mCompPic.getWidth(),
    mCompic.getHeight(), false);

mCompassHud.setImageBitmap(mCompasstemp);

So basically after rotating your bitmap you rescale it to the desired size. Might not be best practice, but at least you dont have to create an extra canvas if you dont want/need to, since the involving Paint() Object is not an inexpensive operation either.

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