在android中绘制镜像位图
我正在尝试学习如何在 android 中制作动画精灵,但不知道如何组织我的位图。我有一个角色向右行走的精灵表:一个角色的五个副本的位图,在一个行走周期中等距(每 45 像素)。
我计划通过一次绘制精灵表位图的一小部分来绘制每个帧,方法是:
Rect sourceRect = new Rect(0, 0, 45, 75);
canvas.drawBitmap(spriteSheetBitmap, sourceRect, new Rect(0, 0, 45, 75), null);
然后对于下一个帧,将“sourceRect.x”增加 45,然后重新绘制,依此类推。
然而,我现在不知道如何让我的精灵向左走。我最初以为我可以镜像我正在绘制的矩形来获得翻转的图片。像这样的东西:
sourceRect = new Rect(45, 0, 0, 75);
这似乎不起作用(不确定这里实际发生了什么,但没有任何东西被吸引到我的表面)。
在网上搜索,似乎我应该复制原始位图,用变换矩阵镜像它,然后在向左行走时使用该位图进行绘制。然而,我还发现了一些实现,其中许多较小的位图对象是从原始精灵表中创建的,存储(并转换为镜像运动),然后根据需要使用。
所以我想知道在这种情况下最好的是什么,或者是否真的有任何区别(性能/内存):
方法1:加载到我的原始精灵表中,创建一个新的位图实例,镜像然后计算所有矩形并使用这些矩形+两个完整的工作表进行绘制(诚然,有一些额外的位图空间未使用精灵表)。
方法 2: 加载我的原始精灵表,为每一帧创建一个新的两个位图对象(1 个镜像,1 个正常)并存储它们以供绘制。
方法3:还有其他更好的方法吗?
I'm trying to learn how to make an animated sprite in android and couldn't figure out how to go about organising my bitmaps. I have a sprite sheet of my character walking to the Right: a bitmap of five copies of a character, equally spaced (every 45px), in a walk cycle.
I planned to draw each frame by drawing a tiny section of my sprite sheet bitmap at a time by going:
Rect sourceRect = new Rect(0, 0, 45, 75);
canvas.drawBitmap(spriteSheetBitmap, sourceRect, new Rect(0, 0, 45, 75), null);
Then for the next frames, increment "sourceRect.x" by 45, then redraw and so forth.
However, I'm now not sure how to go about making my sprite walk to the Left. I had initially thought I could just mirror my rectangle that I am drawing from to get a flipped picture. Something like:
sourceRect = new Rect(45, 0, 0, 75);
which doesn't seem to work (not sure what actually happens here, but nothing gets drawn to my surface).
Searching online, it seems I should make a copy of my original bitmap, mirror it with a transform matrix, then use that bitmap for drawing when walking to the left. However I've also found implementations where many smaller bitmap objects get created out of the original sprite sheet, stored (and transformed for the mirrored motion), then used as needed.
So I'm wondering what would be the best in this case or if there is really any difference (performance/memory):
Method 1: Load in my original sprite sheet, make a new bitmap instance, mirror it,, then calculate all the rectangles and use those + two entire sheets to draw from (admittedly there is some extra bitmap space where the sprite sheet is unused).
Method 2: Load in my original sprite sheet, for every frame create a new two bitmap objects (1 mirrored, 1 normal) and store those to draw from.
Method 3: Other better ways?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
方法 2 成本太高,而且您不需要画布来翻转位图。只需创建另一个应用了矩阵的位图,如下所示:
Method 2 would be way too expensive, and you don't need a canvas to flip a bitmap. Simply create another bitmap with a Matrix applied, like so:
要镜像您的精灵,只需在画布上应用以下变换:scale(-1, 1)。您还必须将精灵的宽度偏移。
To mirror your sprite simply apply the following transform on the Canvas: scale(-1, 1). You will have to offset the sprite by its width too.
要在
canvas
上绘制垂直镜像位图bmp
:To draw a vertical mirrored bitmap
bmp
on acanvas
: