2011-10-24 113 views
0

我開發基於瓷磚的遊戲繪製我使用函數canvas.drawBitmap()在每個框架函數drawBitmap使用400+次,當我使用滾動(手勢)的性能是非常糟糕的。基於Android瓷磚的遊戲

任何人都可以給我建議如何提高性能,使流暢的動畫。

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; 
    } 

} 
+0

把一些代碼... –

+0

我已發佈代碼 – gingray

回答

1

使用遊戲引擎像andEnginelibgdx。他們使用OpenGL渲染,通常速度更快。

更新:如果你不想使用OpenGL /遊戲引擎,你應該嘗試將瓷磚組合成一個更大的位圖。創建一個更大的位圖並創建一個新的畫布(新的Canvas(largeBitmap))來繪製該瓷磚。繪製一個較大的圖比繪製多個較小的圖要快。對於我的遊戲K'UMPA,我使用了類似的方法,因爲只有地圖的一小部分在屏幕上可見,所以這種方法更爲複雜。

+0

也許我們可以找到另一種方式來提高性能? – gingray

+0

更新了答案,以防使用drawBitmap繼續進行軟件渲染。 –