Android 中的旋转帮助
我能够让精灵旋转得很好,但是位图会向下移动很多并切断一些图像,特别是当向下看和以任何角度看时。我正在切换旋转次数较多的位图。例如,当你攻击时,它会在四个攻击图像之间切换。在图像的最后一帧中,精灵剑伸出并指向前方,并且精灵中心点与原始第一帧中他没有攻击时的中心点不同。我的印象是这应该不重要,因为它仍然应该从位图的中心旋转,而不管英雄在哪里。或者我需要重置翻译点什么的。但如果我错了,请纠正我。这是我正在使用的代码。请指教。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过将原始图像放大一点来修复它,以给它更多的旋转空间。虽然不是最好的解决方案,但对我有用。
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.