0

我将矩阵连接到画布,但后来我希望能够执行 canvas.getClipBounds() 来查找画布的当前剪辑。但是,canvas.getClipBounds() 返回一个 Rect(而不是 RectF),我认为它的不精确性导致了我的问题。

所以,我打算维护我自己的 RectF 并在 Canvas 被转换时对其进行转换,以便最终它应该类似于 canvas.getClipBounds() 返回的值,但具有浮点精度。

但是,我不明白 canvas.concat(Matrix) 如何影响 Canvas 的剪辑,因为我模仿串联的尝试非常糟糕。下面是我正在尝试的一些代码,其中的值作为注释。我希望其中一个临时 RectF 具有与m_Canvas.getClipBounds.toString()连接后类似的值,但我什至没有接近。有什么建议么?

 protected void processConcatMatrix(Matrix m)
     {
        //m.toString() == Matrix{[1103.398, -134.48357, 23.99026][174.76108, 849.0959, -159.39447][0.0, 0.0, 1.0]}
        //m_Canvas.getMatrix().toString() == Matrix{[1.0, 0.0, 0.0][0.0, -1.0, 706.0][0.0, 0.0, 1.0]}
        //m_Canvas.getClipBounds.toString() == Rect(0, 0 - 1024, 706)
        m_Canvas.concat(m);
         //m_Canvas.getClipBounds().toString() = Rect(0, 0 - 1, 1);
        //m_Canvas.getMatrix().toString == Matrix{[1103.398, -134.48357, 23.99026][-174.76108, -849.0959, 865.3945][0.0, 0.0, 1.0]}

        RectF temp = new RectF(0, 0 - 1024, 706);
        Matrix m1 = new Matrix(m);
        m1.mapRect(temp);
        //temp.toString() == RectF(-94921.41, -159.39447, 1129903.5, 778257.6)

        //not sure if I should use the parameter matrix, or the canvas matrix after concatentation.  let's try with both


        RectF temp2 = new RectF(0, 0 - 1024, 706);
        Matrix m2 = new Matrix(m_Canvas.getMatrix());
        m2.mapRect(temp2);
        //temp2.toString() == RectF(-94921.41, -777551.6, 1129903.5, 865.3945)

        //maybe I'm supposed to invert the matrices???

        RectF temp3 = new RectF(0, 0 - 1024, 706);
        Matrix m3 = new Matrix(m);
        m3.invert(m3);
        m3.mapRect(temp3);
        //temp3.toString() == RectF(0.0, 0.0, 1024.0, 706.0)

        //not sure if I should use the parameter matrix, or the canvas matrix after concatentation.  let's try with both

        RectF temp4 = new RectF(0, 0 - 1024, 706);
        Matrix m4 = new Matrix(m_Canvas.getMatrix());
        m4.invert(m4);
        m4.mapRect(temp4);
        //temp4.toString == RectF(0.0, 0.0, 1024.0, 706.0)


        //ok, none of these values resembled Rect(0, 0 - 1, 1).  Clearly I am doing something wrong...
  }
4

1 回答 1

1

如果想要真正模仿,您需要另一个Matrix变量来跟踪画布的连接canvas.concatMatrix(Matrix);

    m_Canvas.concat(m);
    m_Matrix.preConcat(m); // Did you forget this?

    RectF temp = new RectF(0, 0, 1024, 706);
    Matrix m1 = new Matrix(m_Matrix);
    m1.mapRect(temp);
    //temp.toString() == RectF(-94921.41, -777551.6, 1129903.5, 865.3945)

    RectF temp2 = new RectF(0, 0, 1024, 706);
    Matrix m2 = new Matrix(m_Canvas.getMatrix());
    m2.mapRect(temp2);
    //temp2.toString() == RectF(-94921.41, -777551.6, 1129903.5, 865.3945) // same as temp
于 2013-08-23T20:22:24.560 回答