The matrix is used to convert coordinates x and y as follows:
X' = A * x + B * Y + c
Y' = D * x + E * Y + F from the above formula, we can see that C and F control the offset of X and Y respectively. A and E control X, and y coordinate multiples, so
X to A, y to B,
The matrix is :,
The translation in the X direction is △x, and 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:
Moving first and then putting it big, the multiplication of the matrix is like a * B.
The image rotation is slightly complicated: The corresponding point after the P0 (x0, y0) point rotates the θ angle around the origin is p (x, y ). By using a vector, we get the following result: R is the rotation radius:
X0 = r cos α
Y0 = r sin α
X = r cos (α + θ) = x0 cos θ-y0 sin θ (expansion of trigonometric function and replacement of variable)
Y = r sin (α + θ) = x0 sin θ + y0 cos θ
So we get the matrix:
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. Vertical image changes of images can also be represented by matrix changes,
Set P0 (x0, y0) to p (x, y), fheight, fwidth, and P0 (x0, y0) in the original image) the coordinates after the vertical image are changed to (x0, 2 * fheight-y0 );
X = x 0 after the vertical image, X coordinates remain unchanged
Y = fheight-y0 after the vertical image, it is equivalent to first rotating the image around the X axis 180 degrees, and then moving the image down the height of the two images, so first y =-y0; then double the height.
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 Code is as follows:
@Override
protected void onDraw (Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw (canvas);
Paint paint = mPaint;
// useColorMatrix (canvas, paint);
matrix = new Matrix ();
matrix.setValues (carrypos);
paint.setColorFilter (null);
canvas.drawBitmap (bitmap, matrix, paint); // you can setColorFilter (null);
carrypos [5] = 2 * bitmap.getHeight (); // Set y coordinate movement
carrypos [4] = -1; // Set to rotate 180 degrees around the x axis
matrix.setValues (carrypos);
canvas.drawBitmap (bitmap, matrix, paint);
}
The result is as follows:
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, 2 * fheight );
Matrix learning-compound changes of images
If the image rotates around a certain point P (A, B), first you need to translate the coordinate system to this point, then rotate, and then translate the rotated image 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); // equivalent to matrix = matrix * t (moving the matrix of the corresponding distance) post... (swapping the product factor of pre)
Matrix learning-error tangent Transformation
What isMistangent transformation of images(Shear transformation )? We can directly see the effect after the incorrect image switch:
Is to set the second parameter of the X vector to 0.5f, that is, x = x0 + 0.5y0, So X increases with Y and the slope is 0.5 linear change.
The Code is as follows:
private float[] carrypos2 ={1,0,100,
0,1,100,
0,0,1}; //z no change
float[] test01 = carrypos2;
test01[1] = 0.5F;
matrix.setValues(test01);
canvas.drawBitmap(bitmap, matrix, paint);
You can also perform the following operations:
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
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 ()
Source: http://shaojun168.blog.51cto.com/4669717/951541