问题描述
我有一个由 42 帧组成的大精灵表 (3808x1632).我会用这些帧呈现一个动画,我使用一个线程来加载一个包含所有帧的位图数组,一个启动画面等待它的结束.我没有使用 SurfaceView(和画布的绘图功能),我只是在主布局的 ImageView 中逐帧加载.我的方法类似于 从 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 spritesheetThe 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 是大精灵表.看的更深一点,我在logcat中看到createBitmap函数需要很多时间,可能是因为spritesheet太大了.我发现在某个地方可以在大精灵表上创建一个窗口,使用 rect 函数和画布,创建要加载到数组中的小位图,但并不是很清楚.我说的是那个帖子:剪切位图部分
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
我的问题是:如何加快精灵表的剪切速度?
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 编程切割:最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!