我正在尝试创建一个弹幕游戏,但遇到了一些麻烦。当有大约500颗子弹时,帧率不能超过17fps。更新逻辑代码对于它们需要1-4ms,而渲染代码需要约40ms。
目前我的代码如下:
private void drawEntities(Canvas canvas) {
for (HashMap<UUID, Spatial> h: spatialList) {
for (Spatial spatial: h.values()) {
spatial.render(canvas);
if(spatial.life > 0)
spatial.life--;
else if (spatial.life == 0)
engine.deleteEntity(spatial.owner);
}
}
}
spatialList是一个ArrayList,其中每个索引都表示一个zLevel。
显示实际子弹的空间是:
public void render(Canvas canvas) {
float angle = (float) (vel.getAngle() * (180 / Math.PI));
matrix.reset();
matrix.setTranslate(pos.x - bullet.getWidth() / 2, pos.y - bullet.getHeight() / 2);
matrix.postRotate(angle + 90, pos.x, pos.y);
canvas.drawBitmap(bullet, matrix, paint);
canvas.drawCircle(pos.x, pos.y, col.getRadius(), paint);
}
我可以提供更多的代码,但这些似乎是主要问题。我已经尝试了我所能想到的一切,也没有在网上找到更多的信息。我能想到的唯一解决方法是从SurfaceView切换到GLSurfaceView,但我真的认为有更好的方法,我只是在使用糟糕的代码。
编辑:我注意到我的计时器有误,并删除了绘制圆形,重新运行后,我得到40ms左右500次,这仍然对于合理的性能来说有点太低了。
简而言之:500个实体=17fps。
matrix.postRotate()
,会有所改善吗?GLSurfaceView 只是 SurfaceView 的包装类,它处理 EGL 设置并为您管理渲染器线程。Grafika 拥有您需要的所有 EGL 设置,并且我猜您已经在为 SurfaceView 管理自己的线程,因此可能很简单(需要一些 GLES 工作)。在 60fps 设备上,16.7ms 的“渲染时间”可能是由于队列堆积导致的,因为 SurfaceView 不会丢帧;请参阅 https://source.android.com/devices/graphics/architecture.html#loops。 - fadden