你看过使用MappedByteBuffer吗?这应该允许您解析文件一次并将内存优化的二进制版本写入 sdcard 并以块的形式快速读取它。
// Open the file here, store it in a field
RandomAccessFile file = null;
try {
mFile = new RandomAccessFile("/sdcard/reallyLargeFile.dat", "r");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
在你想渲染的地方,这样做:
for (int offset = 0; offset < POINTS_SIZE; offset += BLOCK_SIZE)
if (file != null)
try {
MappedByteBuffer buffer = file.getChannel().map(FileChannel.MapMode.READ_ONLY, offset, BLOCK_SIZE);
vertexBuffer.put(buffer);
vertexBuffer.position(0);
// Render here
} catch (IOException e) {
e.printStackTrace();
}
其中 POINTS_SIZE = 总点数 * 4(= 浮点大小),而 BUFFER_SIZE 是一个(可整除)大小,不会使您的应用程序崩溃。请注意,这是一个单线程解决方案,可以通过使用多个线程同时读取较小的缓冲区来分摊成本。此外,虽然这在实践中可能与一堆查找和读取一样有效,但它会产生更优雅和可维护的代码。内存映射文件也完全有可能在许多机器上提供性能提升。
希望这可以帮助。