Android 如何快速、一致地调整多个绘图表的大小并更新其边界

Android 如何快速、一致地调整多个绘图表的大小并更新其边界,android,android-layout,user-interface,2d-games,Android,Android Layout,User Interface,2d Games,我对基于六边形网格的游戏的实现有一个问题。因为游戏需要一个91格的六边形棋盘,就像我下面的简单棋盘一样,我希望用户能够通过缩放/收缩来缩放棋盘,并通过平移来移动棋盘。但是,我的缩放和移动实现都不允许我同时拥有: 1) 一致的边界,使我能够将碎片拖放到构成每个单元格的可绘图条上 2) 一致的相对定位,允许单元在下面相同的配置中彼此相邻 3) 平滑缩放和平移,允许游戏快速运行,并具有类似于其他应用程序的缩放/收缩体验 我尝试过的一些事情(都是使用Activity->SurfaceView->Thr

我对基于六边形网格的游戏的实现有一个问题。因为游戏需要一个91格的六边形棋盘,就像我下面的简单棋盘一样,我希望用户能够通过缩放/收缩来缩放棋盘,并通过平移来移动棋盘。但是,我的缩放和移动实现都不允许我同时拥有:

1) 一致的边界,使我能够将碎片拖放到构成每个单元格的可绘图条上

2) 一致的相对定位,允许单元在下面相同的配置中彼此相邻

3) 平滑缩放和平移,允许游戏快速运行,并具有类似于其他应用程序的缩放/收缩体验

我尝试过的一些事情(都是使用Activity->SurfaceView->Thread完成的,就像LunarLander示例中的那样):

  • 绘制位图,使用矩阵缩放位图,转换画布。处理点2和3,但我不知道如何保持单元格的边界一致。HexCell是一个类,它包含单个单元格的可绘制部分,二维数组board包含HexCell

    public void drawBoard(Canvas c, int posX, int posY, float scaleFactor, float pivotPointX, float pivotPointY, boolean firstDraw) {
        for(int i = 0; i < board.size(); i++) {
            for(int j = 0; j < board.get(i).size(); j++) {
                board.get(i).get(j).draw(bitmapCanvas);
            }
        }
        if(firstDraw) {
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            float scale;
            if(canvasWidth < canvasHeight) {
                scale = ((float) canvasWidth) / width;
            }
            else {
                scale = ((float) canvasHeight) / height;
            }
            Matrix matrix = new Matrix();
    
            // Resize the bit map
                matrix.postScale(scale, scale);
    
            // Recreate the new Bitmap
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, false);
            c.drawBitmap(bitmap, matrix, null);
        }
        c.save();
        c.translate(posX, posY);
        Matrix matrix = new Matrix();
        matrix.postScale(scaleFactor, scaleFactor, pivotPointX, pivotPointY);
        c.drawBitmap(bitmap, matrix, null);
        c.restore();
    }
    
  • 我该怎么办?如何在缩放和移动时更新边界,以便最终在板上放置块?我是否应该放弃缩放和平移的愿望,转而使用GridView或类似的工具来实现该板


    编辑:

    再进一步研究,我确定选项1是最好的选择。它的速度快得多,并使细胞保持一致的形态。我发现,如果将应用于画布的变换倒置,并将其应用于触摸事件中的坐标,则可以返回到单元格的原始边界,从而适当地选择它们

    但是,我无法准确地反转转换

    x /= scaleFactor;
    y /= scaleFactor;
    x -= posX + pivotPointX;
    y -= posY + pivotPointY;
    
    不是以下各项的工作倒置:

    canvas.save();
    canvas.translate(posX, posY);
    canvas.scale(scaleFactor, scaleFactor, pivotPointX, pivotPointY);
    
    有人知道如何适当地反转它吗?

    在drawBoard()方法中,我做到了:

    canvas.save();
    canvas.translate(posX, posY);
    canvas.scale(scaleFactor, scaleFactor, pivotPointX, pivotPointY);
    canvas.getMatrix(canvasMatrix); // Save the matrix that has the transformations so we can invert it
    for(int i = 0; i < board.size(); i++) {
        for(int j = 0; j < board.get(i).size(); j++) {
             board.get(i).get(j).draw(c);
        }
    }
    canvas.restore();
    
    这就成功了!现在,如果有人建议避免使用不推荐使用的canvas.getMatrix()方法,那就太好了:)

    canvas.save();
    canvas.translate(posX, posY);
    canvas.scale(scaleFactor, scaleFactor, pivotPointX, pivotPointY);
    
    canvas.save();
    canvas.translate(posX, posY);
    canvas.scale(scaleFactor, scaleFactor, pivotPointX, pivotPointY);
    canvas.getMatrix(canvasMatrix); // Save the matrix that has the transformations so we can invert it
    for(int i = 0; i < board.size(); i++) {
        for(int j = 0; j < board.get(i).size(); j++) {
             board.get(i).get(j).draw(c);
        }
    }
    canvas.restore();
    
    float x = ev.getX();
    float y = ev.getY();
    
    float[] pts = {x, y};
    Matrix canvasMatrix = boardThread.getCanvasMatrix(); // get the matrix with the transformations
    Matrix invertMatrix = new Matrix();
    canvasMatrix.invert(invertMatrix); // invert the matrix
    invertMatrix.mapPoints(pts); // map the inversion to the points x and y
    boardThread.selectHex((int)pts[0], (int)pts[1]);