I. first introduce the Scale Control
Scale is scaling. We call setscale, prescale, and postscale of Matrix. Actually, it is implemented by modifying mscale_x and mscale_y.
The following is a simple example.
-
- Public ClassMatrixtestactivityExtendsActivity {
-
- Private IntScreenwidth;
-
- Private IntScreenheight;
- Private IntBitmapwidth;
-
- Private IntBitmapheight;
-
- Private FloatBasescale;
-
- Private FloatOriginalscale;
-
-
- @ Override
- Public VoidOncreate (bundle savedinstancestate ){
-
- Super. Oncreate (savedinstancestate );
-
- Setcontentview (R. layout. Main );
-
- // Obtain the screen width and height
-
- Screenwidth = getwindow (). getwindowmanager (). getdefadisplay display (). getwidth ();
- Screenheight = getwindow (). getwindowmanager (). getdefadisplay display (). getheight ();
-
- // Load the imageview and obtain the image information
-
- FinalImageview = (imageview) findviewbyid (R. Id. imgview );
-
- FinalBitmap bitmap = bitmapfactory. decoderesource (getresources (), R. drawable. );
-
- Bitmapwidth = bitmap. getwidth ();
- Bitmapheight = bitmap. getheight ();
-
-
- // Calculate the zoom ratio, because if the image size exceeds the screen size, it will be automatically matched to the screen size for display.
-
- // Then, we do not know the width and height of the image displayed on the screen. Therefore, we need to calculate the zoom ratio of all the images displayed first,
-
- // Calculate the actual width and height of the image display, and then perform the next step of scaling.
-
- // Otherwise, it will lead to reduced or Amplified effects, or memory leakage, etc.
- FloatScalex = screenwidth /(Float) Bitmapwidth;
-
- FloatScaley = screenheight /(Float) Bitmapheight;
-
- Basescale = math. Min (scalex, scaley );// Obtain the largest scaling ratio, that is, the one in scalex and scaley.
-
- Originalscale = basescale;
-
- FinalMatrix matrix =NewMatrix ();
-
- Matrix. setscale (originalscale, originalscale );
-
- // The difference between setscale and prescale and postscale will be discussed later
-
- // Matrix. prescale (originalscale, originalscale );
-
- // Matrix. postscale (originalscale, originalscale );
- Bitmap bitmap2 = bitmap
-
- . Createbitmap (bitmap,0,0, Bitmapwidth, bitmapheight, matrix,False);
-
- Imageview. setimagebitmap (bitmap2 );
-
-
- FinalButton scale_btn = (button) findviewbyid (R. Id. scale_btn );
- FinalEdittext scale_text = (edittext) findviewbyid (R. Id. scale_editview );
-
- Scale_btn.setonclicklistener (NewView. onclicklistener (){
-
- Public VoidOnclick (view v ){
-
- String scalestr = scale_text.gettext (). tostring ();
- If(Scalestr =Null|"". Equals (scalestr ))
-
- Return;
-
- FloatScale =0. 0f;
- Try{
-
- Scale = float. parsefloat (scalestr );
-
- }Catch(Numberformatexception e ){
-
- Return;
- }
-
- Matrix. Reset ();
-
- Originalscale = scale * originalscale;// View
-
- If(Originalscale <0.05){
- Originalscale =0. 05f;
-
- }
-
- If(Originalscale> basescale ){
-
- Originalscale = basescale;
-
- }
- Matrix. setscale (originalscale, originalscale );
-
- Bitmap bitmapchange = bitmap. createbitmap (bitmap,0,0, Bitmapwidth, bitmapheight,
-
- Matrix,False);
-
- Imageview. setimagebitmap (bitmapchange );
- }
-
- });
-
- }
-
- }
It can be found that for the scale setting, matrix provides different methods in 3 to set.
Setscale, prescale, and postscale.
What are the differences between the three methods? See the following.
Ii. Differences between set..., pre..., post...
1. setscale (sx, Sy): First, it sets the Matrix to a diagonal matrix, that is, it is equivalent to calling the reset () method, then, set mscale_x and mscale_y of the Matrix to SX and SY values.
2. prescale (sx, Sy). Instead of resetting the matrix, it is directly combined (multiplied) with the mscale_x and mscale_y values before the matrix, M' = m * s (sx, sy ).
3. postscale (sx, Sy). Instead of resetting the matrix, it is directly combined (multiplied) with the mscale_x and mscale_y values before the matrix, M' = S (sx, SY) * m.
Prescale and post are both combined with the previous matrix. What is the difference between them?
Let's take a few examples to test the differences between pre... and post:
Set a matrix as follows:
1. execution sequence of pre...
-
- Matrix matrix =NewMatrix ();
-
- Float[] Points =New Float[] {10. 0f,10. 0f };
-
-
- Matrix. prescale (2. 0f,3. 0f );//
- Matrix. pretranslate (8. 0f,7. 0f );//
-
- Matrix. mappoints (points );
-
- Log. I ("Test", Points [0] +"");
-
- Log. I ("Test", Points [1] +"");
The result is the coordinate (36.0, 51.0)
It can be concluded that the order of transformation is to execute pretranslate (8.0f, 7.0f) first, and execute prescale (2.0f, 3.0f) in the execution ). This is why some people say that pre... is backward-growing, that is, in the setting of a matrix,
All pre... is executed backwards.
2. execution sequence of post...
- Matrix matrix =NewMatrix ();
-
- Float[] Points =New Float[] {10. 0f,10. 0f };
-
-
- Matrix. postscale (2. 0f,3. 0f );//
-
- Matrix. posttranslate (8. 0f,7. 0f );//
- Matrix. mappoints (points );
-
- Log. I ("Test", Points [0] +"");
-
- Log. I ("Test", Points [1] +"");
The result is the coordinate (28.0, 37.0)
It can be concluded that the order of transformation is to first execute postscale (2.0f, 3.0f) and then execute posttranslate (8.0f, 7.0f ). This is why some people refer to post... as a forward growth, that is, all post... is executed along the forward in a matrix setting.
3. execution sequence when pre and post Alternate
-
- Matrix matrix =NewMatrix ();
- Float[] Points =New Float[] {10. 0f,10. 0f };
-
-
- Matrix. postscale (2. 0f,3. 0f );
-
- Matrix. prerotate (90);
-
- Matrix. mappoints (points );
-
- Log. I ("Test", Points [0] +"");
- Log. I ("Test", Points [1] +"");
The result is the coordinate of (-20.0, 30.0)
Change the order of pre... and post.
-
- Matrix matrix =NewMatrix ();
-
- Float[] Points =New Float[] {10. 0f,10. 0f };
-
-
- Matrix. prerotate (90);
- Matrix. postscale (2. 0f,3. 0f );
-
- Matrix. mappoints (points );
-
- Log. I ("Test", Points [0] +"");
-
- Log. I ("Test", Points [1] +"");
The result is that the coordinate is still (-20.0, 30.0)
It can be seen that pre is always executed first. See the following:
-
- Matrix matrix =NewMatrix ();
- Float[] Points =New Float[] {10. 0f,10. 0f };
-
-
- Matrix. postscale (2. 0f,3. 0f );// Step 2
-
- Matrix. prerotate (90);// Step 2
- Matrix. posttranslate (8. 0f,7. 0f );// Step 2
-
- Matrix. prescale (1. 5f,2. 5f );// Step 2
-
- Matrix. mappoints (points );
-
- Log. I ("Test", Points [0] +"");
-
- Log. I ("Test", Points [1] +"");
The result is that the coordinate is still (-42.0, 52.0)
After the previous conclusions and calculations, we can find that the execution sequence is 4----2----1---3.
In the following example, the setscale section is added.Code:
-
- Matrix matrix =NewMatrix ();
-
- Float[] Points =New Float[] {10. 0f,10. 0f };
-
- Matrix. postscale (2. 0f,3. 0f );// Step 2
-
- Matrix. prerotate (90);// Step 2
-
- Matrix. setscale (1. 4f,2. 6f );// Step 2
-
- Matrix. posttranslate (8. 0f,7. 0f );// Step 2
- Matrix. prescale (1. 5f,2. 5f );// Step 2
-
- Matrix. mappoints (points );
-
- Log. I ("Test", Points [0] +"");
-
- Log. I ("Test", Points [1] +"");
The result is that the coordinate is still (29.0, 72.0)
After calculation, we can find that steps 3rd and 2 before step 1 setscale are useless, and are directly covered by Step 2 setscale and executed from step 3.
The sequence is 2---1----3----5----4. Because 2 and 1 are overwritten, there is no effect. It is equivalent to directly executing 3-----5----4.
conclusion: Finally, we can draw a conclusion that in all the settings before the matrix transformation, we should first check whether there is setscale. If so, we will jump directly to the setscale step to start the transformation, then execute all the following pre... change, and execute all post ..... Therefore, when setting matrix transformations, you must pay attention to the order. Different sequences have different results.