android - Spritesheet 以编程方式切割 : best practices

标签 android bitmap sprite-sheet

我有一个由 42 帧组成的大 spritesheet (3808x1632)。 我会用这些帧呈现一个动画,我使用一个线程来加载一个包含所有帧的位图数组,并有一个等待其结束的启动画面。 我没有使用 SurfaceView(和 Canvas 的绘制功能),我只是在主布局的 ImageView 中逐帧加载。 我的方法类似于 Loading a large number of images from a spritesheet 完成实际上需要将近 15 秒,不能接受。

我用的是这种函数:

for (int i=0; i<TotalFramesTeapotBG; i++) {
            xStartTeapotBG = (i % framesInRowsTeapotBG) * frameWidthTeapotBG; 
            yStartTeapotBG = (i / framesInRowsTeapotBG) * frameHeightTeapotBG;
            mVectorTeapotBG.add(Bitmap.createBitmap(framesBitmapTeapotBG, xStartTeapotBG, yStartTeapotBG, frameWidthTeapotBG, frameHeightTeapotBG));
        }

framesBitmapTeapotBG 是一个大的 spritesheet。 更深入地看,我在 logcat 中读到 createBitmap 函数需要很多时间,可能是因为 spritesheet 太大了。 我在某个地方发现我可以在大 spritesheet 上创建一个窗口,使用 rect 函数和 Canvas ,创建要加载到数组中的小位图,但它不是很清楚。我说的是那个帖子:cut the portion of bitmap

我的问题是:如何加快 spritesheet 的切割速度?

编辑: 我正在尝试使用这种方法,但看不到最终动画:

    for (int i=0; i<TotalFramesTeapotBG; i++) {
        xStartTeapotBG = (i % framesInRowsTeapotBG) * frameWidthTeapotBG; 
        yStartTeapotBG = (i / framesInRowsTeapotBG) * frameHeightTeapotBG;
        Bitmap bmFrame = Bitmap.createBitmap(frameWidthTeapotBG, frameHeightTeapotBG, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bmFrame); 
        Rect src = new Rect(xStartTeapotBG, yStartTeapotBG, frameWidthTeapotBG, frameHeightTeapotBG); 
        Rect dst = new Rect(0, 0, frameWidthTeapotBG, frameHeightTeapotBG);  
        c.drawBitmap(framesBitmapTeapotBG, src, dst, null);         
        mVectorTeapotBG.add(bmFrame);
    }

可能是 Bitmap bmFrame 管理不当。

最佳答案

简短的回答是更好的内存管理。

您正在加载的 Sprite 表很大,然后您将其复制到一堆小位图中。假设 Sprite 表不能再小了,我建议采用以下两种方法之一:

  1. 使用单独的位图。这将减少内存副本以及 Dalvik 必须增长堆的次数。然而,这些好处可能会受到从文件系统加载许多图像而不是一个图像的需要的限制。在普通计算机中会出现这种情况,但 Android 系统可能会得到不同的结果,因为它们使用闪存运行。
  2. 直接从您的 sprite 表中 Blit。绘制时,只需使用类似 Canvas.drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint) 的东西直接从 sprite 表中绘制。 .这会将您的文件加载减少到一个大的分配,这可能只需要在您的 Activity 生命周期内发生一次。

我认为第二个选项可能是两者中更好的一个,因为它在内存系统上更容易,并且对 GC 的工作更少。

关于android - Spritesheet 以编程方式切割 : best practices,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7341017/

相关文章:

android - 更改分隔线颜色或 Android DatePicker 对话框的主题

android - 如何从android中的日历中获取出生日期?

javascript - 移相器 3 : load spritesheet for animation with unequal dimensions

javascript - PIXI.js AnimatedSprite 在第一次播放时滞后

android - 如何使用资源文件使用 layout_width?

java - Android 中的 SQLite 替换和外键

android - 使用 Renderscript 和超过 25 个半径的 Android 模糊位图

Android:使用 ImageView 显示 sdcard 中的 .jpg 图像

c# - 将 .bmp 文件解码为二进制文件

android - AndEngine如何获取透明动画?