在android中围绕一个点旋转位图,而不显示视图动画

在android中围绕一个点旋转位图,而不显示视图动画,android,bitmap,rotation,Android,Bitmap,Rotation,我在android中找到了一些很好的旋转位图的资源,在网上也有。我很快就能让我的代码正常工作了,但显然我并不完全理解矩阵翻译是如何工作的。下面是我的sprite类的三个主要函数。在我添加一些东西来促进矩阵旋转(即在onDraw中,我称之为仅使用x、y和无矩阵绘制)之前,它们工作得非常好。我编写了一些测试代码来添加一个精灵,并将其从0旋转到360,然后再次旋转到0。它导致它像绕着某个奇点旋转一样旋转。实际上,我希望它只是坐在那里旋转: public void Rotate_Sprite(int t

我在android中找到了一些很好的旋转位图的资源,在网上也有。我很快就能让我的代码正常工作了,但显然我并不完全理解矩阵翻译是如何工作的。下面是我的sprite类的三个主要函数。在我添加一些东西来促进矩阵旋转(即在onDraw中,我称之为仅使用x、y和无矩阵绘制)之前,它们工作得非常好。我编写了一些测试代码来添加一个精灵,并将其从0旋转到360,然后再次旋转到0。它导致它像绕着某个奇点旋转一样旋转。实际上,我希望它只是坐在那里旋转:

public void Rotate_Sprite(int transform, int deg)
    {
        int spriteCenterX = x+(width/2);
                int spriteCenterY = y+(height/2);
        mMatrix.setRotate(deg, spriteCenterX, spriteCenterY);
        }

public void Draw_Sprite(Canvas c) { 
//c.drawBitmap(images[curr_frame], x, y, null); //this worked great esp in move sprite
    c.drawBitmap(images[curr_frame], mMatrix, null);
}

public void Create_Sprite(blah blah) {

    ...
    ...
    mMatrix = new Matrix();
    mMatrix.reset();
}

public int Move_Sprite() {
    //with the matrix stuff, I assume I need a translate.  But it does't work right 
    //for me at all.
    int lastx=this.x;
    int lasty=this.y;
    this.x+=this.vx;
    this.y+=this.vy;
    mMatrix.postTranslate(lastX-x,lastY-y); //doesn't work at all
}

我确实发现了这一点,尽管我称之为旋转的所有精灵似乎都围绕一个点在轨道上旋转。

我没有在android上工作过,自从我上次使用矩阵已经有一段时间了,但听起来你的旋转工作正常,你只是忘记了平移,所以旋转的点是0,0。如果问题是这样的,你要做的是转换精灵,使它的世界位置为0,0;旋转精灵;然后把它翻译回以前的地方。这一切都应该发生在绘制框架之前,因此永远不会看到翻译本身


希望这能有所帮助。

我还没有在android上工作过,自从我上次使用Matrix已经有一段时间了,但听起来你的旋转工作正常,你只是忘记了平移,所以旋转点是0,0。如果问题是这样的,你要做的是转换精灵,使它的世界位置为0,0;旋转精灵;然后把它翻译回以前的地方。这一切都应该发生在绘制框架之前,因此永远不会看到翻译本身

希望这有帮助。

试试这个:

mMatrix.setTranslate(objectX,objectY);
mMatrix.postRotate(Degrees, objectXcenter, objectYcenter);
基本上,您需要首先将位图转换到您想要的位置,将其设置在该位置,然后围绕其中心旋转N度。

尝试以下操作:

mMatrix.setTranslate(objectX,objectY);
mMatrix.postRotate(Degrees, objectXcenter, objectYcenter);

基本上,您需要先将位图转换到您想要的位置,然后将其设置在该位置,然后围绕其中心旋转N度。

对于找到该位图并尝试执行相同操作的任何人:

我像这样旋转我的图像:

//create all your canvases and bitmaps and get sizes first
Bitmap minBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.minute);
minCanvas..setBitmap(minBitmap);
int height = min.getHeight();
int width = min.getWidth();
//Bitmap minBitmap = Bitmap(width, height, Bitmap.Config.ARGB_8888); //not using in example


//The basically applies the commands to the source bitmap and creates a new bitmpa.  Check the order of width and height, mine was a square.
minMatrix.setRotate(minDegrees, width/2, height/2);
Bitmap newMin = Bitmap.createBitmap(minBitmap, 0, 0, (int) width, (int) height, minMatrix, true);

//apply this to a canvas.  the reason for this is that rotating an image using Matrix changes the size of the image and this will trim it and center it based on new demensions.
minCanvas2.drawBitmap(newMin, width/2 - newMin.getWidth()/2, height/2 - newMin.getHeight()/2, null);

//Then you can use it the way you want, but I create a bitmap from the canvas
minCanvas2.setBitmap(minBitmap);
我没有运行这段代码,这是在查看我实际运行的代码时输入的


HTH

对于任何发现这一点并试图做同样事情的人:

我像这样旋转我的图像:

//create all your canvases and bitmaps and get sizes first
Bitmap minBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.minute);
minCanvas..setBitmap(minBitmap);
int height = min.getHeight();
int width = min.getWidth();
//Bitmap minBitmap = Bitmap(width, height, Bitmap.Config.ARGB_8888); //not using in example


//The basically applies the commands to the source bitmap and creates a new bitmpa.  Check the order of width and height, mine was a square.
minMatrix.setRotate(minDegrees, width/2, height/2);
Bitmap newMin = Bitmap.createBitmap(minBitmap, 0, 0, (int) width, (int) height, minMatrix, true);

//apply this to a canvas.  the reason for this is that rotating an image using Matrix changes the size of the image and this will trim it and center it based on new demensions.
minCanvas2.drawBitmap(newMin, width/2 - newMin.getWidth()/2, height/2 - newMin.getHeight()/2, null);

//Then you can use it the way you want, but I create a bitmap from the canvas
minCanvas2.setBitmap(minBitmap);
我没有运行这段代码,这是在查看我实际运行的代码时输入的


HTH

我在哪里进行翻译,这些翻译坐标是以像素为单位的吗?我在哪里进行翻译,这些翻译坐标是以像素为单位的吗?这似乎不起作用。如果对后跟.postRotate的矩阵调用.setTranslate,则只会得到旋转,即使如此,它也不会围绕指定的中心旋转。是否对图像的大小或形状有任何要求?您可能希望转换为objectX objectRadius、objectY objectRadius,然后执行旋转。这似乎不起作用。如果对后跟.postRotate的矩阵调用.setTranslate,则只会得到旋转,即使如此,它也不会围绕指定的中心旋转。是否对图像的大小或形状有任何要求?您可能希望转换为objectX objectRadius、objectY objectRadius,然后执行旋转。