在android中围绕一个没有视图动画的点旋转位图

发布于 2024-09-07 09:56:52 字数 1207 浏览 5 评论 0原文

我在这里和网上其他地方找到了一些在 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
}

我确实在这里找到了这个 J2me 类似参考。< /a> 虽然我所说的所有精灵似乎都在围绕一个点的轨道上旋转。

I found some good resources for rotating a bitmap in android here and elsewher eon the net. I am close to getting my code to work but apparently I don't fully understand how the Matrix Translations work. The following is three main functions from my sprite class. THey worked great before I added things to facilitate the matrix rotation (namely in the onDraw I called the draw with just x,y and no matrix). I wrote some test code to add a sprite, and rotate it from 0 to 360 and back to 0 again repeatedly. It results it in rotating about like its orbiting some odd point. In reality I want it to just sit there and spin:

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
}

I did find this J2me like reference here. Though it seems to have all my sprites I call rotate on in orbit around one point.

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

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

发布评论

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

评论(3

情仇皆在手 2024-09-14 09:56:52

试试这个:

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

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

Try this:

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

Basically, you need to translate your bitmap to the location you want it first, set it there, then rotate it N Degrees around its center.

梦途 2024-09-14 09:56:52

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

希望这有帮助。

I haven't worked on the android, and It's been a while since I last worked with matrices, but it sounds like you're rotation is working properly and you just forgot to translate so the point to rotate around is at 0,0. What you're going to want to do if this is in fact the problem is translate the sprite so that it's world location is 0,0; rotate the sprite; then translate it BACK to wherever it was before. This should all occur before drawing that frame, so the translation itself will never be seen.

Hope this helps.

情痴 2024-09-14 09:56:52

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

我正在像这样旋转我的图像:

//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);

我没有运行此代码,这是在查看我实际运行的代码时输入的。

华泰

For anybody who finds this and is trying to do the same sort of thing:

I am rotating my images like this:

//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);

I have not run this code, this was typed out while looking at my code that I actually do run.

HTH

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