Android 中的旋转帮助

发布于 2024-10-24 11:35:52 字数 1311 浏览 1 评论 0原文

我能够让精灵旋转得很好,但是位图会向下移动很多并切断一些图像,特别是当向下看和以任何角度看时。我正在切换旋转次数较多的位图。例如,当你攻击时,它会在四个攻击图像之间切换。在图像的最后一帧中,精灵剑伸出并指向前方,并且精灵中心点与原始第一帧中他没有攻击时的中心点不同。我的印象是这应该不重要,因为它仍然应该从位图的中心旋转,而不管英雄在哪里。或者我需要重置翻译点什么的。但如果我错了,请纠正我。这是我正在使用的代码。请指教。

  public void draw(Canvas canvas, int pointerX, int pointerY) {
        // setBitmap(MainGamePanel.testIcon);
        if (setRotation) {
            canvas.save();
            m.reset();
            // get rotation for ninja based off of joystick
            m.setTranslate(spriteWidth / 2, spriteHeight / 2);
            m.postRotate((float) GameControls.getRotation(), spriteWidth / 2,
                    spriteHeight / 2);
            // rotate ninja
            flipedSprite = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
                    bitmap.getHeight(), m, true);
            // set new bitmap to rotated ninja
             setBitmap(flipedSprite);
            setRotation = false;
            canvas.restore();
        }

// create the destination rectangle for the ninjas animation pointerX // and pointerY are from the joystick moving the ninja around destRect = new Rect(pointerX, pointerY, pointerX + spriteWidth, pointerY + spriteHeight); canvas.drawBitmap(bitmap, getSourceRect(), destRect, null); }

I am able to get the sprite to rotate okay however the bitmap gets shifted down a lot and cuts off some of the image especially when looking down and to any angle. I am switching the bitmap that gets rotated a lot. Like for example when you attack it switches between four attacking images. On the last frame of the image the sprites sword is out and pointed forward and the sprites center point is not the same as the original first frame when he was not attacking. I am under the impression this should not matter because it should still rotate from the center of the bitmap and not matter where the hero is. Or do I need to reset the translate point or something. But please correct me if I am wrong. here is the code I am using. Please Advise.

  public void draw(Canvas canvas, int pointerX, int pointerY) {
        // setBitmap(MainGamePanel.testIcon);
        if (setRotation) {
            canvas.save();
            m.reset();
            // get rotation for ninja based off of joystick
            m.setTranslate(spriteWidth / 2, spriteHeight / 2);
            m.postRotate((float) GameControls.getRotation(), spriteWidth / 2,
                    spriteHeight / 2);
            // rotate ninja
            flipedSprite = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
                    bitmap.getHeight(), m, true);
            // set new bitmap to rotated ninja
             setBitmap(flipedSprite);
            setRotation = false;
            canvas.restore();
        }

// create the destination rectangle for the ninjas animation pointerX // and pointerY are from the joystick moving the ninja around destRect = new Rect(pointerX, pointerY, pointerX + spriteWidth, pointerY + spriteHeight); canvas.drawBitmap(bitmap, getSourceRect(), destRect, null); }

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

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

发布评论

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

评论(1

寻找一个思念的角度 2024-10-31 11:35:52

我通过将原始图像放大一点来修复它,以给它更多的旋转空间。虽然不是最好的解决方案,但对我有用。

I fixed it by making the original image a little bit bigger to give it more space to rotate. Although not the best solution but works for me.

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