Android平板游戏

时间:2011-10-24 11:19:37

标签: java android

我开发基于图块的游戏来绘制我在每个框架函数drawBitmap中使用函数canvas.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;
    }

}

2 个答案:

答案 0 :(得分:1)

使用andEnginelibgdx等游戏引擎。他们使用OpenGL渲染通常要快得多。

更新:如果您不想使用OpenGL /游戏引擎,您应该尝试将图块组合成更大的位图。创建一个更大的位图并创建一个新的画布(新的Canvas(largeBitmap))来绘制它。绘制一个较大的比绘制多个较小的一个快。对于我的游戏K'UMPA,我使用了类似的方法,其细节更复杂,因为在屏幕上只能看到地图的一小部分。

答案 1 :(得分:0)

也许您可以尝试不在每次渲染调用时创建位图。 我假设您正在为每个帧调用渲染:

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