我有一个由 42 帧组成的大精灵表 (3808x1632)。我会用这些帧呈现一个动画,然后我使用一个线程来加载一个包含所有帧的位图数组,并用一个初始屏幕等待它的结束。我没有使用 SurfaceView(和画布的绘制功能),我只是在我的主布局中的 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 是大精灵表。更深入地看,我在logcat中读到createBitmap函数需要很多时间,可能是因为spritesheet太大了。我找到了可以在大精灵表上创建一个窗口的地方,使用 rect 函数和画布,创建要加载到数组中的小位图,但不是很清楚。我说的是那个帖子:cut the part 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);
}
可能,位图 bmFrame 未正确管理。