In-depth understanding of Android Matrix theory and usage

Source: Internet
Author: User

Previously, I learned about matrices in linear algebra and had some knowledge about the basic operations of matrices. I learned how to use matrices to change images when I used GDI + some time ago, after reading this article, I will summarize it here.
First, let's take a look at the 3x3 matrix, which is divided into four parts. The reason for splitting the data into four parts is described in detail later.

First, we will give you a simple example: after we have set the point P0 (x0, y0) for translation, we will move it to P (x, y), where the translation in the x direction is △x, if the translation in the y direction is △y, then the coordinates of P (x, y) are:
X = x0 + △x
Y = y0 + △y
The matrix expression is as follows:

The above is similar to the image translation. Through the above matrix, we find that you only need to modify the two elements in the upper right corner of the matrix.
Let's look back at the division of the above matrix:

In order to verify the functional division above, we will give a specific example: now we have set the point P0 (x0, y0) to pan and move it to P (x, y), where x is multiplied by, y to B,

The matrix is: Verify according to the method similar to the previous "Translation.

The image rotation is slightly complicated: The corresponding point after the θ angle is rotated by P0 (x0, y0) is P (x, y ). By using vectors, we can get the following:
X0 = r cos α
Y0 = r sin α
X = r cos (α + θ) = x0 cos θ-y0 sin θ
Y = r sin (α + θ) = x0 sin θ + y0 cos θ

So we get the matrix:

What if the image rotates around a certain point (a, B? First, you need to translate the coordinates to the point, then rotate, and then translate the rotated image back to the original coordinate origin. We will introduce it in detail in the subsequent sections.

Matrix learning-how to use Matrix

In this article, we will introduce Android. graphics. Matrix in android.Image RotationMatrix:

Which of the following is the simplest rotation of 90 degrees:

There is a corresponding rotation function in android. graphics. Matrix:
Matrix matrix = new Matrix ();
Matrix. setRotate (90 );
Test. Log (MAXTRIX_TAG, "setRotate (90): % s", matrix. toString ());

View the value of the matrix after running (output through Log ):

It is basically the same as the above formula (android. graphics. Matrix uses floating-point numbers while we use integers ).
With the above example, you can try it yourself. Through the above example, we also found that we can also initialize the Matrix directly, for example, to rotate 30 degrees:

We have introduced so many topics.ImageThere are two types: horizontal image and vertical image. First, we will introduce how to implement a vertical image. What is a vertical image is not described in detail. The vertical image changes of the image can also be represented by matrix changes. Set the point P0 (x0, y0) to P (x, y), and the Image Height to fHeight, the width is fWidth, and the coordinates of P0 (x0, y0) in the original image after the vertical image are changed to (x0, fHeight-y0 );
X = x0
Y = fHeight-y0
Export the corresponding matrix:

Final FloatF [] = {1.0F, 0.0F, 0.0F, 0.0F,-1.0F, 1200000f, 0.0F, 0.0F, 1.0F };
Matrix matrix =NewMatrix ();
Matrix. setValues (f );

The result is as follows:

You can try horizontal images in a similar way.
In fact, you can use the following method to implement a vertical image:
Matrix matrix =NewMatrix ();
Matrix. setScale (1.0,-1.0 );
Matrix. postTraslate (0, fHeight );
This is what we will describe in detail later.

Matrix learning-compound changes of images

Matrix learning-Basic KnowledgeIn space, let's leave a topic: if an image rotates around a certain point P (a, B), first you need to translate the coordinate system to this point and then rotate it, then, the rotated image is moved back to the original coordinate origin.

We need three steps:
1.Translation-- Shifts the coordinate system to P (a, B );
2.Rotate-- Rotate the image with the origin as the center;
3.Translation-- Translate the rotated image back to the original coordinate origin;
Compared with the aforementioned geometric changes of images (Basic geometric changes of images ),Pan -- rotate -- panSuch geometric changes that require multiple types of images are called compound changes of images.
For a given image, F1, F2, F3 ..... , Fn, their change matrices are T1, T2, T3 ..... , Tn, image composite change matrix T can be expressed as: T = TnTn-1... T1.
According to the above principle, the change matrix sequence of θ rotating around a certain point (a, B) is:

Based on the above formula, we will give a simple example: rotate around (100,100) 30 degrees (sin 30 = 0.5, cos 30 = 0.866)
FloatF [] = {0.866F,-0.5F, 63.4F, 0.5F, 0.866F,-36.6F, 0.0F, 0.0F, 1.0F };
Matrix =NewMatrix ();
Matrix. setValues (f );
The rotated image is as follows:

Android provides us with a simpler method, as follows:
Matrix matrix = new Matrix ();
Matrix. setRotate (30,100,100 );
Actual results after matrix operation:



This is exactly the same as the matrix obtained through the formula.
Here we provide another method to achieve the same effect:
Float a = 1000000f, B = 1000000f;
Matrix = new Matrix ();
Matrix. setTranslate (a, B );
Matrix. preRotate (30 );
Matrix. preTranslate (-a,-B );
I will explain it in detail later.
Using a similar method, we can also get:Ratio of relative P (a, B) to [sx, sy] Change Matrix

Matrix Learning -- Preconcats or Postconcats?

Starting from the most basic advanced mathematics, the basic operations of Matrix include: + and ,*. The multiplication of Matrix does not satisfy the exchange law, that is, A * B = B *. There are two common matrices:

With the above foundation, we will start to enter the topic. Because the matrix does not meet the exchange law, if matrix B is used to multiply matrix A, you need to consider whether it is left multiplication (B * A) or right multiplication (A * B ). In Android. graphics. Matrix of android, a similar method is provided for us, that is, the Preconcats matrix and Postconcats matrix to be described in this article. The following is an example:

Through the output information, we analyze the running process as follows:

Read the output above. We come to the conclusion:Preconcats matrix is equivalent to the right multiplication matrix, and Postconcats matrix is equivalent to the left multiplication matrix..

Matrix learning-error tangent Transformation

What isMistangent transformation of images(Shear transformation )? We can directly see the effect after the incorrect image switch:

Make a summary of the miscut transformation of the image:

X = x0 + B * y0;
Y = d * x0 + y0;

Here we will introduce you to the following points:

Through the above, we found that the setXXXX () function of Matrix calls a reset () during the call, which requires attention during composite transformation.

Matrix learning-symmetric transformation (reflection)

What is symmetric transformation? The specific theory is not detailed. The image is a type of symmetric transformation.

Take the above summary as an example to generate a reflection image that is symmetric with the straight line y =-x. The code snippet is as follows:

The current matrix output is:

The effect of image transformation is as follows:

Appendix: trigonometric function formula
Two corners and Formula
Sin (a + B) = sinaco Sb + cosasinb
Sin (a-B) = sinaco Sb-sinbcosa
Cos (a + B) = cosacosb-sinasinb
Cos (a-B) = cosacosb + sinasinb
Tan (a + B) = (tana + tanb)/(1-tanatanb)
Tan (a-B) = (tana-tanb)/(1 + tanatanb)
Cot (a + B) = (cotacotb-1)/(cotb + cota)
Cot (a-B) = (cotacotb + 1)/(cotb-cota)
Angle X formula
Tan2a = 2 tana/[1-(tana) ^ 2]
Cos2a = (cosa) ^ 2-(sina) ^ 2 = 2 (cosa) ^ 2-1 = 1-2 (sina) ^ 2
Sin2a = 2sina * cosa
Halfwidth Formula
Sin (a/2) = √ (1-cosa)/2) sin (a/2) =-√ (1-cosa)/2)
Cos (a/2) = √ (1 + cosa)/2) cos (a/2) =-√ (1 + cosa)/2)
Tan (a/2) = √ (1-cosa)/(1 + cosa) tan (a/2) =-√ (1-cosa) /(1 + cosa ))
Cot (a/2) = √ (1 + cosa)/(1-cosa) cot (a/2) =-√ (1 + cosa) /(1-cosa ))
Tan (a/2) = (1-cosa)/sina = sina/(1 + cosa)
And difference Product
2 sinaco sb = sin (a + B) + sin (a-B)
2 cosasinb = sin (a + B)-sin (a-B ))
2 cosacosb = cos (a + B)-sin (a-B)
-2 sinasinb = cos (a + B)-cos (a-B)
Sina + sinb = 2sin (a + B)/2) cos (a-B)/2
Cosa + cosb = 2cos (a + B)/2) sin (a-B)/2)
Tana + tanb = sin (a + B)/cosacosb
Product and difference Formulas
Sin (a) sin (B) =-1/2 * [cos (a + B)-cos (a-B)]
Cos (a) cos (B) = 1/2 * [cos (a + B) + cos (a-B)]
Sin (a) cos (B) = 1/2 * [sin (a + B) + sin (a-B)]
Induction Formula
Sin (-a) =-sin ()
Cos (-a) = cos ()
Sin (pi/2-a) = cos ()
Cos (pi/2-a) = sin ()
Sin (pi/2 + a) = cos ()
Cos (pi/2 + a) =-sin ()
Sin (pi-a) = sin ()
Cos (pi-a) =-cos ()
Sin (pi + a) =-sin ()
Cos (pi + a) =-cos ()
Tga = tana = sina/cosa
Universal Formula
Sin (a) = (2tan (a/2)/(1 + tan ^ 2 (a/2 ))
Cos (a) = (1-tan ^ 2 (a/2)/(1 + tan ^ 2 (a/2 ))
Tan (a) = (2tan (a/2)/(1-tan ^ 2 (a/2 ))
Other formulas
A * sin (a) + B * cos (a) = sqrt (a ^ 2 + B ^ 2) sin (a + c) [Where tan (c) = B/a]
A * sin (a)-B * cos (a) = sqrt (a ^ 2 + B ^ 2) cos (a-c) [Where tan (c) = a/B]
1 + sin (a) = (sin (a/2) + cos (a/2) ^ 2
1-sin (a) = (sin (a/2)-cos (a/2) ^ 2
Other non-key trigonometric Functions
Csc (a) = 1/sin ()
Sec (a) = 1/cos ()
Hyperbolic Functions
Sinh (a) = (e ^ a-e ^ (-a)/2
Cosh (a) = (e ^ a + e ^ (-a)/2
Tgh (a) = sinh (a)/cosh ()

Related Article

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.