Introduction to the Matrix class in Android

Source: Internet
Author: User

Introduction to the Matrix class in Android
Matrix, as its name implies, is a Matrix in linear algebra. The basic knowledge about matrices and addition, subtraction, multiplication, division operations are not described here.

The Matrix class in Android is a simple 3x3 Level 3 Matrix with the following structure:

  1. Float matrix = {MSCALE_X, MSKEW_X, MTRANS_X, MSKEW_Y, MSCALE_Y, MTRANS_Y,
  2. MPERSP_0, MPERSP_1, MPERSP_2 };
    The structure is as follows: MSCALE_X and MSCALE_Y control the zooming of the X axis and Y axis respectively, and MSKEW_X and MSKEW_Y control the linear skew coefficients of the X and Y coordinates respectively, MTRANS_X and MTRANS_Y control linear translation in the X and Y directions. MPERSP_0, MPERSP_1, and MPERSP_2 are not used much at the moment.

    With the appeal description, the so-called matrix transformation is mainly used to perform arithmetic operations on the Level 3 matrix! For example, if the translation operation is switched from (0, 0) to (10,120), the above Matrix's MSCALE_X = 10 and MSCALE_Y = 120 can be matrix. setValues (Matrix array ). In this way, Canvas can use this matrix for plotting and translation.

    The rotation matrix has the following special structure:


    1. Float matrix = {cosA,-sinA, 0, sinA, cosA, 0,
    2. 0, 0, 1}; where A is the rotation angle. to rotate, you only need to input the angle to the above matrix.

      At the same time, we know that the multiplication of matrices is asymmetric, that is, A * B! = B * A. In this case, the left multiplication and right multiplication in Matrix multiplication are involved. Similarly, in Android, Matrix also implements the left multiplication and right multiplication functions, all postXXX operations are left multiplication, and all preXXX operations are right multiplication. This should be paid attention to when using Matrix for combination transformation.

      At the same time, because the final state of the matrix object is the matrix obtained after all transformations, if you need to re-use the matrix and do not want to retain the previous state, you need to call reset () resets the matrix status to avoid errors. At the same time, it should be noted that the setXXX method of the Matrix class will call reset () by default to reset the Matrix. Therefore, when implementing the combination transformation, be sure not to call the setXXX method in the middle of the transformation, in this way, all the transformations before the setXXX method are ignored.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.