Spritesheet编程切割:最佳实践 [英] Spritesheet programmatically cutting: best practices

查看:344
本文介绍了Spritesheet编程切割:最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有42帧组成一个大的spritesheet(3808x1632)。 我想present这些帧的动画,我使用一个线程加载一个位图阵列的所有帧,用闪屏等待其结束。 我不使用SurfaceView(和画布的绘制函数),我只是加载一帧一帧中的ImageView在我的主要版面。 我的做法是类似<一个href="http://stackoverflow.com/questions/5294016/loading-a-large-number-of-images-from-a-spritesheet">Loading从spritesheet 大量图片 完成实际需要近15秒,不能接受的。

I have a big spritesheet (3808x1632) composed by 42 frames. I would present an animation with these frames and I use a thread to load a bitmap array with all the frames, with a splash screen waiting for its end. I'm not using a SurfaceView (and a draw function of a canvas), I just load frame by frame in an ImageView in my main layout. My approach is similar to Loading a large number of images from a spritesheet The completion actually takes almost 15 seconds, not acceptable.

我用这种功能:

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。 展望更深刻,我在报纸上看到的createBitmap功能需要花费大量的时间,也许是因为spritesheet太大的logcat的。 我发现的地方,我可以做一个窗口上的大spritesheet,使用矩形功能和帆布,创建小位图数组中加载,但它并不真正清楚。我说的那个帖子:切位的部分。

framesBitmapTeapotBG is the big spritesheet. Looking more deeply, I've read in the logcat that the createBitmap function takes a lot of time, maybe because the spritesheet is too big. I found somewhere that I could make a window on the big spritesheet, using the rect function and canvas, creating small bitmaps to be loaded in the array, but it was not really clear. I'm talking about that post: cut the portion of bitmap

我的问题是:我怎么能加快spritesheet切

My question is: how can I speed the spritesheet cut?

编辑: 我试图用这个方法,但我看不到最终的动画:

I'm trying to use this approach but I cannot see the final animation:

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

也许,位图bmFrame不正确的管理。

Probably, the Bitmap bmFrame is not correctly managed.

推荐答案

感谢stevehb的建议下,我终于明白了:

Thanks to stevehb for the suggestion, I finally got it:

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, xStartTeapotBG+frameWidthTeapotBG, yStartTeapotBG+frameHeightTeapotBG); 
            Rect dst = new Rect(0, 0, frameWidthTeapotBG, frameHeightTeapotBG);  
            c.drawBitmap(framesBitmapTeapotBG, src, dst, null);         
            mVectorTeapotBG.add(bmFrame);
        }

中的计算时间降到令人难以置信! :)

The computation time falls incredibly! :)

这篇关于Spritesheet编程切割:最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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