Luk*_*lor 5 java android drawing rotation
我正在努力在其中心周围绘制一个旋转位图,并在不调整位图大小的情况下进行旋转.我正在通过游戏线程将所有精灵绘制到屏幕上,所以我正在寻找一种包含原始位图而不是画布的解决方案.
提前致谢.
到目前为止,这是我的代码,它围绕其中心转换位图,然后调整它的大小.
i = i + 2;
transform.postRotate(i, Assets.scoresScreen_LevelStar.getWidth()/2, Assets.scoresScreen_LevelStar.getHeight()/2);
Bitmap resizedBitmap = Bitmap.createBitmap(Assets.scoresScreen_LevelStar, 0, 0, Assets.scoresScreen_LevelStar.getWidth(), Assets.scoresScreen_LevelStar.getHeight(), transform, true);
game.getGraphics().getCanvasGameScreen().drawBitmap(resizedBitmap, null, this.levelStar.getHolderPolygons().get(0), null);
Run Code Online (Sandbox Code Playgroud)
更新:
我注意到这并不像听起来那么容易.我的旋转代码不是问题.位图旋转,但是dst rect也必须根据旋转角度增加/减少,否则bimap会显得更小,因为它被绘制到固定的dst rect中.所以我猜我必须开发一些会返回dst rect的方法. 所以旋转位图所需的方法没有出现调整大小:
public static Bitmap rotateBitmap(Bitmap bitmap, int rotation) // I've got this method working
Run Code Online (Sandbox Code Playgroud)
和
public static Rect rotateRect(Rect currentDst, int rotation) // Don't got this
Run Code Online (Sandbox Code Playgroud)
我明白这需要一些数学(触发),任何人都可以接受挑战吗?:P
您应该使用Matrix该类绘制位图.下面是一个非常基本的想法,假设您想要在"Ship"类中旋转图像.您在update方法中更新当前位置Matrix.在onDraw()中,您使用新更新的位置Matrix绘制位图.这将绘制旋转的位图而不调整其大小.
public class Ship extends View {
private float x, y;
private int rotation;
private Matrix position;
private Bitmap bitmap;
...
@Override
public void onDraw(Canvas canvas) {
// Draw the Bitmap using the current position
canvas.drawBitmap(bitmap, position, null);
}
public void update() {
// Generate a new matrix based off of the current rotation and x and y coordinates.
Matrix m = new Matrix();
m.postRotate(rotation, bitmap.getWidth()/2, bitmap.getHeight()/2);
m.postTranslate(x, y);
// Set the current position to the updated rotation
position.set(m);
rotation += 2;
}
....
}
Run Code Online (Sandbox Code Playgroud)
希望有所帮助!
还要记住,Bitmap在游戏循环中生成一个新对象将是资源密集型的.
这对我有用!
我创建了一个返回矩阵的方法。矩阵可以用如下的绘制方法:
public void drawBitmap (Bitmap bitmap, Matrix matrix, Paint paint)
Run Code Online (Sandbox Code Playgroud)
干得好!(参数形状可以轻松替换,如果您愿意,请发表评论):
public static Matrix rotateMatrix(Bitmap bitmap, Shape shape, int rotation) {
float scaleWidth = ((float) shape.getWidth()) / bitmap.getWidth();
float scaleHeight = ((float) shape.getHeight()) / bitmap.getHeight();
Matrix rotateMatrix = new Matrix();
rotateMatrix.postScale(scaleWidth, scaleHeight);
rotateMatrix.postRotate(rotation, shape.getWidth()/2, shape.getHeight()/2);
rotateMatrix.postTranslate(shape.getX(), shape.getY());
return rotateMatrix;
}
Run Code Online (Sandbox Code Playgroud)
注意:如果您想要动画旋转,则必须每帧使用新值更新旋转参数,例如。1 然后 2 然后 3 ...
| 归档时间: |
|
| 查看次数: |
9932 次 |
| 最近记录: |