Clearly understand matrix calculation

前端 未结 1 1014
無奈伤痛
無奈伤痛 2021-01-30 09:41

I have this code snippet. I don\'t understand the Matrix.preScale and the Bitmap.createBitmap with the matrix passed. What does it mean? Is there any simulation website

相关标签:
1条回答
  • 2021-01-30 10:13

    Don't think about it too hard, at least not at the early stages.

    Just think of a matrix as an array of numbers. In this case, an Android Matrix has 3 rows of 3 numbers. Each number tells an Android graphics function what to do to scale (bigger/smaller), translate (move), rotate (turn) or skew (distort in a 2D plane) the "thing" which the matrix is applied to.

    The matrix looks like this (see the docs here).

    {Scale X, Skew X, Transform X
    Skew Y, Scale Y, Transform Y
    Perspective 0, Perspective 1, Perspective 2}
    

    The good news is that you don't need to know any matrix maths, indeed almost no maths, to use matrices in Android. That's what methods like preScale() do for you. To understand the maths behind is not that hard, for most things you only need add, multiply and SOHCAHTOA.

    matrix-transform-for-the-mathematically-challenged/

    When you read the Matrix documentation, you will see methods for rotate, translate etc with prefixes of 'set', 'post' or 'pre'.

    Imagine that you create a new matrix. You then use setRotate() to setup the matrix to do a rotation. You then use preTranslate() to do a translation. Because you used 'pre', the translation happens before the rotation. Had you used 'post', the rotation would happen first. 'set' clears whatever is in the matrix and starts again.

    To answer your specific question, new Matrix() creates the 'identity matrix'

    {1, 0, 0
     0, 1, 0
     0, 0, 1}
    

    which scales by 1 (therefore same size) and does no translation, rotation or skew. Therefore, applying the identity matrix will do nothing. The next method is preScale() which is applied to this identity matrix and in the case you've shown, results in a matrix that scales, and does nothing else so could also be done using setScale() or postScale().

    Hope this helps.

    0 讨论(0)
提交回复
热议问题