简体   繁体   English

使用矩阵旋转位图

[英]Rotating a bitmap using matrix

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. 我的代码的输出就像-bitmap会旋转一样。 -vertex of my bitmap is not stable. 我的位图的-vertex不稳定。 -Bitmap is resizing -位图正在调整大小

I need disable image resizing and make the rotation stable.Can you please suggest a solution for this? 我需要禁用图像大小调整并保持旋转稳定。您能为此提出解决方案吗?

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. 而不是直接从原始位图创建新的位图,另一个(更直接的方法,恕我直言)选项是创建结果位图,使用该位图创建Canvas,然后在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));

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. Android使用其内置的兼容性功能来缩放和渲染位图,以适合具有不同像素密度的屏幕。 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). 当内部绘制位图时,它将从资源中预缩放位图并自动缩放(这就是使用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 转到http://developer.android.com/guide/practices/screens_support.html并在以下位置进行检查:4.使用特定于密度和/或大小的资源:位图和九个补丁的预缩放和自动缩放

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() 然后在画布上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 Xpos和Ypos是位图的X和Y位置setMatrix(null),将矩阵设置为null,以便旋转不会影响after位图,而且它并不总是创建新的位图,因此非常适合表现希望对我有帮助

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. 可能不是最佳实践,但是至少,如果您不想/不需要,也不必创建额外的画布,因为涉及的Paint()对象也不是一项便宜的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM