首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于Android tile的游戏

基于Android tile的游戏
EN

Stack Overflow用户
提问于 2011-10-24 19:19:37
回答 2查看 3.8K关注 0票数 0

我开发了基于瓷砖的游戏来绘制,我在每一帧中使用函数canvas.drawBitmap(),函数drawBitmap使用400+,当我使用滚动(手势)时,性能相当差。

谁能给我一些建议,如何提高性能,使流畅的动画。

代码语言:javascript
复制
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;
    }

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-10-24 19:25:09

使用像andEnginelibgdx这样的游戏引擎。他们使用OpenGL渲染,这通常要快得多。

更新:如果你不想使用OpenGL/游戏引擎,你应该试着把tiles组合成一个更大的位图。创建一个较大的位图,然后创建一个新画布(新画布(LargeBitmap))以在其中绘制平铺。绘制一个较大的比绘制多个较小的要快。对于我的游戏K'UMPA,我使用了类似的方法,这在细节上更加复杂,因为只有一小部分地图在屏幕上可见。

票数 1
EN

Stack Overflow用户

发布于 2018-03-28 18:00:02

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

代码语言:javascript
复制
    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);
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7874807

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档