在绘制机器人镜像位图 [英] Drawing mirrored bitmaps in android

查看:146
本文介绍了在绘制机器人镜像位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想学习如何使机器人的动画精灵和无法弄清楚如何去整理我的位图。我有我的性格的精灵表步行到右:五份的性格,等间隔(每45像素)的位图,在步行周期

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

然后,对于下一个帧,递增sourceRect.x45,然后重画等等。

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):

方法1:加载在我原来的精灵表,做一个新的位图实例,镜像,,然后计算出所有的矩形,并使用这些+两个完整张纸画(当然有一些额外的位图空间,让精灵表未使用)。

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).

方法2:加载在我原来的精灵表,对每一帧创建一个新的双位图对象(1镜像,1正常)和存储那些可以吸取

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.

方法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:

BitmapDrawable flip(BitmapDrawable d)
{
    Matrix m = new Matrix();
    m.preScale(-1, 1);
    Bitmap src = d.getBitmap();
    Bitmap dst = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), m, false);
    dst.setDensity(DisplayMetrics.DENSITY_DEFAULT);
    return new BitmapDrawable(dst);
}

这篇关于在绘制机器人镜像位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆