在android中绘制镜像位图 [英] Drawing mirrored bitmaps in android

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

问题描述

我正在尝试学习如何在 android 中制作动画精灵,但不知道如何组织我的位图.我有一个角色向右走的精灵表:一个角色的五个副本的位图,等距(每 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.x"增加 45,然后重绘等等.

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

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

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