Android Development notes -- matrix transformation

Source: Internet
Author: User

 

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.


  1. Public ClassMatrixtestactivityExtendsActivity {
  2. Private IntScreenwidth;
  3. Private IntScreenheight;
  4. Private IntBitmapwidth;
  5. Private IntBitmapheight;
  6. Private FloatBasescale;
  7. Private FloatOriginalscale;
  8. @ Override
  9. Public VoidOncreate (bundle savedinstancestate ){
  10. Super. Oncreate (savedinstancestate );
  11. Setcontentview (R. layout. Main );
  12. // Obtain the screen width and height
  13. Screenwidth = getwindow (). getwindowmanager (). getdefadisplay display (). getwidth ();
  14. Screenheight = getwindow (). getwindowmanager (). getdefadisplay display (). getheight ();
  15. // Load the imageview and obtain the image information
  16. FinalImageview = (imageview) findviewbyid (R. Id. imgview );
  17. FinalBitmap bitmap = bitmapfactory. decoderesource (getresources (), R. drawable. );
  18. Bitmapwidth = bitmap. getwidth ();
  19. Bitmapheight = bitmap. getheight ();
  20. // Calculate the zoom ratio, because if the image size exceeds the screen size, it will be automatically matched to the screen size for display.
  21. // 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,
  22. // Calculate the actual width and height of the image display, and then perform the next step of scaling.
  23. // Otherwise, it will lead to reduced or Amplified effects, or memory leakage, etc.
  24. FloatScalex = screenwidth /(Float) Bitmapwidth;
  25. FloatScaley = screenheight /(Float) Bitmapheight;
  26. Basescale = math. Min (scalex, scaley );// Obtain the largest scaling ratio, that is, the one in scalex and scaley.
  27. Originalscale = basescale;
  28. FinalMatrix matrix =NewMatrix ();
  29. Matrix. setscale (originalscale, originalscale );
  30. // The difference between setscale and prescale and postscale will be discussed later
  31. // Matrix. prescale (originalscale, originalscale );
  32. // Matrix. postscale (originalscale, originalscale );
  33. Bitmap bitmap2 = bitmap
  34. . Createbitmap (bitmap,0,0, Bitmapwidth, bitmapheight, matrix,False);
  35. Imageview. setimagebitmap (bitmap2 );
  36. FinalButton scale_btn = (button) findviewbyid (R. Id. scale_btn );
  37. FinalEdittext scale_text = (edittext) findviewbyid (R. Id. scale_editview );
  38. Scale_btn.setonclicklistener (NewView. onclicklistener (){
  39. Public VoidOnclick (view v ){
  40. String scalestr = scale_text.gettext (). tostring ();
  41. If(Scalestr =Null|"". Equals (scalestr ))
  42. Return;
  43. FloatScale =0. 0f;
  44. Try{
  45. Scale = float. parsefloat (scalestr );
  46. }Catch(Numberformatexception e ){
  47. Return;
  48. }
  49. Matrix. Reset ();
  50. Originalscale = scale * originalscale;// View
  51. If(Originalscale <0.05){
  52. Originalscale =0. 05f;
  53. }
  54. If(Originalscale> basescale ){
  55. Originalscale = basescale;
  56. }
  57. Matrix. setscale (originalscale, originalscale );
  58. Bitmap bitmapchange = bitmap. createbitmap (bitmap,0,0, Bitmapwidth, bitmapheight,
  59. Matrix,False);
  60. Imageview. setimagebitmap (bitmapchange );
  61. }
  62. });
  63. }
  64. }


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...


  1. Matrix matrix =NewMatrix ();
  2. Float[] Points =New Float[] {10. 0f,10. 0f };
  3. Matrix. prescale (2. 0f,3. 0f );//
  4. Matrix. pretranslate (8. 0f,7. 0f );//
  5. Matrix. mappoints (points );
  6. Log. I ("Test", Points [0] +"");
  7. 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...

  1. Matrix matrix =NewMatrix ();
  2. Float[] Points =New Float[] {10. 0f,10. 0f };
  3. Matrix. postscale (2. 0f,3. 0f );//
  4. Matrix. posttranslate (8. 0f,7. 0f );//
  5. Matrix. mappoints (points );
  6. Log. I ("Test", Points [0] +"");
  7. 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


  1. Matrix matrix =NewMatrix ();
  2. Float[] Points =New Float[] {10. 0f,10. 0f };
  3. Matrix. postscale (2. 0f,3. 0f );
  4. Matrix. prerotate (90);
  5. Matrix. mappoints (points );
  6. Log. I ("Test", Points [0] +"");
  7. Log. I ("Test", Points [1] +"");

The result is the coordinate of (-20.0, 30.0)

Change the order of pre... and post.


  1. Matrix matrix =NewMatrix ();
  2. Float[] Points =New Float[] {10. 0f,10. 0f };
  3. Matrix. prerotate (90);
  4. Matrix. postscale (2. 0f,3. 0f );
  5. Matrix. mappoints (points );
  6. Log. I ("Test", Points [0] +"");
  7. 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:


  1. Matrix matrix =NewMatrix ();
  2. Float[] Points =New Float[] {10. 0f,10. 0f };
  3. Matrix. postscale (2. 0f,3. 0f );// Step 2
  4. Matrix. prerotate (90);// Step 2
  5. Matrix. posttranslate (8. 0f,7. 0f );// Step 2
  6. Matrix. prescale (1. 5f,2. 5f );// Step 2
  7. Matrix. mappoints (points );
  8. Log. I ("Test", Points [0] +"");
  9. 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:


  1. Matrix matrix =NewMatrix ();
  2. Float[] Points =New Float[] {10. 0f,10. 0f };
  3. Matrix. postscale (2. 0f,3. 0f );// Step 2
  4. Matrix. prerotate (90);// Step 2
  5. Matrix. setscale (1. 4f,2. 6f );// Step 2
  6. Matrix. posttranslate (8. 0f,7. 0f );// Step 2
  7. Matrix. prescale (1. 5f,2. 5f );// Step 2
  8. Matrix. mappoints (points );
  9. Log. I ("Test", Points [0] +"");
  10. 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.

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.