Java 基于Android的平铺游戏

Java 基于Android的平铺游戏,java,android,Java,Android,我开发了一个基于平铺的游戏来画图,我在每一帧中使用函数canvas.drawBitmap()当我使用滚动(手势)时,使用400多次drawBitmap函数的性能非常差 谁能给我一些建议,如何提高性能以制作流畅的动画 public class DiamondIsometric implements Render{ private int MapWidth; private int MapHeight; private Bitmap _surface; private

我开发了一个基于平铺的游戏来画图,我在每一帧中使用函数
canvas.drawBitmap()
当我使用滚动(手势)时,使用400多次drawBitmap函数的性能非常差

谁能给我一些建议,如何提高性能以制作流畅的动画

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

}
public类钻石等距工具渲染{
私有int映射宽度;
私人地图高度;
私有位图_曲面;
私有位图;
公共直径等距(整数贴图宽度、整数贴图高度、位图曲面)
{
这个.MapWidth=MapWidth;
这个.MapHeight=MapHeight;
这个。_曲面=_曲面;
}
公共映射对象[][]构建映射()
{
int-rx;
智力;
MapObject[][]MapObjects=新建MapObject[MapWidth][MapHeight];
对于(intx=0;x使用像或这样的游戏引擎。他们使用OpenGL渲染,这通常要快得多


更新:如果你不想使用OpenGL/游戏引擎,你应该尝试将平铺组合成一个更大的位图。创建一个更大的位图并创建一个新的画布(新画布(大位图))绘制平铺。绘制一个大的比画多个小的快。在我的游戏中,我使用了类似的方法,这在细节上更复杂,因为在屏幕上只能看到地图的一小部分。

也许你可以尝试在每次渲染调用时不创建位图。 我假设您正在为每个帧调用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);
    }
}
public类钻石等距工具渲染{
私有int映射宽度;
私人地图高度;
私有位图_曲面;
私有位图绘制;
私人BMPCANVAS;
公共直径等距(整数贴图宽度、整数贴图高度、位图曲面)
{
this.MapWidth=MapWidth;
this.MapHeight=MapHeight;
这个。_曲面=_曲面;
this.toDraw=null;
this.wideBMPCanvas=null;
}
公共映射对象[][]构建映射()
{
int-rx;
智力;
MapObject[][]MapObjects=新建MapObject[MapWidth][MapHeight];

对于(int x=0;xU)更新了答案,以防您希望坚持使用drawBitmap进行软件渲染。