我开发了基于瓷砖的游戏来绘制,我在每一帧中使用函数canvas.drawBitmap(),函数drawBitmap使用400+,当我使用滚动(手势)时,性能相当差。
谁能给我一些建议,如何提高性能,使流畅的动画。
public class DiamondIsometric implements Render{
private int MapWidth;
private int MapHeight;
private Bitmap _surface;
private Bitmap tempBitmap;
public DiamondIsometric(int MapWidth,int MapHeight,Bitmap _surface)
{
this.MapWidth=MapWidth;
this.MapHeight=MapHeight;
this._surface = _surface;
}
public MapObject[][] BuildMap()
{
int rx;
int ry;
MapObject[][] MapObjects = new MapObject[MapWidth][MapHeight];
for(int x=0;x<MapHeight;x++)
for(int y=0;y<MapWidth;y++)
{
rx=(x-y)*_surface.getWidth()/2;
ry=(x+y)*_surface.getHeight()/2;
MapObject temp = new MapObject(new Point(rx,ry),_surface);
MapObjects[x][y]=temp;
}
return MapObjects;
}
@Override
public void Render(Canvas canvas) {
}
@Override
public void Render(Canvas canvas,MapObject[][] MapObjects)
{
Paint temp = new Paint();
temp.setColor(Color.BLACK);
canvas.drawPaint(temp);
Bitmap toDraw = Bitmap.createBitmap(MapWidth*_surface.getWidth(), MapHeight*_surface.getHeight(), Config.RGB_565);
int bitmapOffsetX,bitmapOffsetY;
canvas.drawBitmap(this.Render2(canvas,MapObjects),0,0,null);
}
public Bitmap Render2(Canvas canvas, MapObject[][] MapObjects)
{
Paint temp = new Paint();
temp.setColor(Color.BLACK);
tempBitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Config.RGB_565);
Canvas wideBMPCanvas = new Canvas(tempBitmap);
int bitmapOffsetX,bitmapOffsetY;
for(int i=0;i<MapObjects.length;i++)
for(int j=0;j<MapObjects[i].length;j++)
{
bitmapOffsetX=(IsometricView.globalAnchor.x % MapObjects[i][j]._bitmap.getWidth());
bitmapOffsetY=(IsometricView.globalAnchor.y % MapObjects[i][j]._bitmap.getHeight());
wideBMPCanvas.drawBitmap(MapObjects[i][j]._bitmap,MapObjects[i][j].coords.x-IsometricView.globalAnchor.x ,MapObjects[i][j].coords.y-IsometricView.globalAnchor.y , null);
}
return tempBitmap;
}
}发布于 2011-10-24 19:25:09
使用像andEngine或libgdx这样的游戏引擎。他们使用OpenGL渲染,这通常要快得多。
更新:如果你不想使用OpenGL/游戏引擎,你应该试着把tiles组合成一个更大的位图。创建一个较大的位图,然后创建一个新画布(新画布(LargeBitmap))以在其中绘制平铺。绘制一个较大的比绘制多个较小的要快。对于我的游戏K'UMPA,我使用了类似的方法,这在细节上更加复杂,因为只有一小部分地图在屏幕上可见。
发布于 2018-03-28 18:00:02
也许您可以尝试不在每次渲染调用时创建位图。我假设您正在为每个帧调用render:
public class DiamondIsometric implements Render {
private int MapWidth;
private int MapHeight;
private Bitmap _surface;
private Bitmap toDraw;
private Canvas wideBMPCanvas;
public DiamondIsometric(int MapWidth,int MapHeight,Bitmap _surface)
{
this.MapWidth = MapWidth;
this.MapHeight = MapHeight;
this._surface = _surface;
this.toDraw = null;
this.wideBMPCanvas = null;
}
public MapObject[][] BuildMap()
{
int rx;
int ry;
MapObject[][] MapObjects = new MapObject[MapWidth][MapHeight];
for(int x=0;x<MapHeight;x++)
for(int y=0;y<MapWidth;y++)
{
rx=(x-y)*_surface.getWidth()/2;
ry=(x+y)*_surface.getHeight()/2;
MapObject temp = new MapObject(new Point(rx,ry),_surface);
MapObjects[x][y]=temp;
}
return MapObjects;
}
@Override
public void Render(Canvas canvas) { }
@Override
public void Render(Canvas canvas,MapObject[][] MapObjects)
{
Paint temp = new Paint();
temp.setColor(Color.BLACK);
canvas.drawPaint(temp);
if (this.toDraw == null) {
this.toDraw = Bitmap.createBitmap(MapWidth*_surface.getWidth(), MapHeight*_surface.getHeight(), Config.RGB_565);
}
if (this.wideBMPCanvas == null) {
this.wideBMPCanvas = new Canvas(this.toDraw);
}
int bitmapOffsetX,bitmapOffsetY;
for (int i = 0; i < MapObjects.length; i++) {
for(int j = 0; j < MapObjects[i].length; j++)
{
bitmapOffsetX = (IsometricView.globalAnchor.x % MapObjects[i][j]._bitmap.getWidth());
bitmapOffsetY = (IsometricView.globalAnchor.y % MapObjects[i][j]._bitmap.getHeight());
this.wideBMPCanvas.drawBitmap(MapObjects[i][j]._bitmap, MapObjects[i][j].coords.x - IsometricView.globalAnchor.x,
MapObjects[i][j].coords.y - IsometricView.globalAnchor.y, null);
}
}
canvas.drawBitmap(this.toDraw, 0, 0, null);
}
}https://stackoverflow.com/questions/7874807
复制相似问题