Android:如何模仿canvas.concat(Matrix)?

时间:2013-08-15 21:54:47

标签: android

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

所以,我打算维护自己的RectF并在Canvas被转换时对其进行转换,因此最后它应该类似于canvas.getClipBounds()返回的值,但是在float精度中。

但是,我不明白canvas.concat(Matrix)如何影响Canvas的剪辑,因为我试图模仿连接的效果非常差。下面是我正在尝试的一些代码,其值为注释。我希望其中一个临时RectFs在连接后具有与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...
  }

1 个答案:

答案 0 :(得分: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