The difference between Setscale,prescale and Postscale

Source: Internet
Author: User

Here is the matrix structure of the MATRIX3*3

[Java] view plaincopy

1. {mscale_x,mskew_x,mtrans_x,

2. Mskew_y,mscale_y,mtrans_y,

3. mpersp_0,mpersp_1,mpersp_2}



First, the control of scale scaling is introduced.

Scale is scaling, we call the Matrix Setscale, Prescale, Postscale, actually inside, is by modifying the mscale_x and mscale_y to achieve.

Here's a simple example.

[Java] view plaincopy

1. Public class Matrixtestactivity extends Activity {

2. private int screenwidth;

3. private int screenheight;

4. private int bitmapwidth;

5. private int bitmapheight;

6. Private float Basescale;

7. Private float Originalscale;

8.

9. @Override

public void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);

Setcontentview (R.layout.main);

13.//Get the screen width high

ScreenWidth = GetWindow (). Getwindowmanager (). Getdefaultdisplay (). GetWidth ();

ScreenHeight = GetWindow (). Getwindowmanager (). Getdefaultdisplay (). GetHeight ();

16.//Load ImageView and get picture information

Final ImageView ImageView = (ImageView) Findviewbyid (R.id.imgview);

Final Bitmap Bitmap = Bitmapfactory.decoderesource (Getresources (), r.drawable.a);

Bitmapwidth = Bitmap.getwidth ();

Bitmapheight = Bitmap.getheight ();

21st.

22.//Calculate the zoom ratio, because if the picture is larger than the screen, it will automatically match to the screen size to display.

23.//Then we don't know how wide the picture actually appears on the screen, so we first calculate the zoom ratio that needs to be displayed,

24.//To calculate the actual width of the picture when it is displayed, then the next step to zoom.

25.//Otherwise, it will result in reduced and amplified effects, or memory leaks, etc.

ScaleX float = screenwidth/(float) bitmapwidth;

float ScaleY = screenheight/(float) bitmapheight;

Basescale = Math.min (ScaleX, ScaleY);//Get the zoom ratio of the largest scale, that is, ScaleX and ScaleY Small

Originalscale = Basescale;

30.

Final Matrix matrix = new Matrix ();

Matrix.setscale (Originalscale, Originalscale);

33.//About the difference between Setscale and Prescale and Postscale

//Matrix.prescale (Originalscale, Originalscale);

///Matrix.postscale (Originalscale, Originalscale);

Bitmap BITMAP2 = Bitmap

Panax Notoginseng: CreateBitmap (bitmap, 0, 0, bitmapwidth, Bitmapheight, Matrix, false);

Imageview.setimagebitmap (BITMAP2);

39.

Max. Final Button scale_btn = (Button) Findviewbyid (R.ID.SCALE_BTN);

Final EditText Scale_text = (EditText) Findviewbyid (R.id.scale_editview);

Scale_btn.setonclicklistener (New View.onclicklistener () {

public void OnClick (View v) {

A. String scalestr = Scale_text.gettext (). toString ();

if (Scalestr = = NULL | | "". Equals (SCALESTR))

. return;

. float scale = 0.0f;

. try {

Scale = Float.parsefloat (SCALESTR);

.} catch (NumberFormatException e) {

A. return;

52.}

Matrix.reset ();

Originalscale = scale * originalscale;//See

if (Originalscale < 0.05) {

originalscale=0.05f;

57.}

if (Originalscale > Basescale) {

Originalscale=basescale;

60.}

Matrix.setscale (Originalscale, Originalscale);

Bitmap Bitmapchange = Bitmap.createbitmap (Bitmap, 0, 0, bitmapwidth, Bitmapheight,

. Matrix, False);

Imageview.setimagebitmap (Bitmapchange);

65.}

66.});

67.}

68.}


As you can see, for the scale setting, the matrix provides 3 different ways to set it up.

Setscale, Prescale, Postscale.

What is the difference between the three ways? Look at the following.

Second, set ...., pre ..., post ... The difference

1, Setscale (Sx,sy), the matrix will first be set to a diagonal matrix, which is equivalent to call the Reset () method, and then set the matrix of mscale_x and mscale_y directly set to the value of Sx,sy

2, Prescale (Sx,sy), does not reset the matrix, but directly with the matrix before the mscale_x and mscale_y values are combined (multiplied), m ' = m * S (SX, SY).

3, Postscale (Sx,sy), does not reset the matrix, but directly with the matrix before the mscale_x and mscale_y values are combined (multiplied), M ' = S (SX, SY) * M.

Prescale and post are combined with the previous matrix, what's the difference between them?

Give a few examples to test the pre .... and post .... The difference:

Set the following for a matrix

1. Pre .... Order of execution

[Java] view plaincopy

1. Matrix matrix=new matrix ();

2. float[] Points=new float[]{10.0f,10.0f};

3.

4. Matrix.prescale (2.0f, 3.0f);//

5. Matrix.pretranslate (8.0f,7.0f);//

6. Matrix.mappoints (points);

7. LOG.I ("Test", points[0]+ "");

8. LOG.I ("Test", points[1]+ "");

The result is a point coordinate of (36.0,51.0)

It can be concluded that the order of the transformations is performed first pretranslate (8.0f,7.0f), in the executed Prescale (2.0f,3.0f). This is why some people are likened to the pre ... is to grow backwards, that is, for a matrix setting,

All pre .... is executed backwards.

2. Post ... Order of execution

[Java] view plaincopy

1. Matrix matrix=new matrix ();

2. float[] Points=new float[]{10.0f,10.0f};

3.

4. Matrix.postscale (2.0f, 3.0f);//

5. Matrix.posttranslate (8.0f,7.0f);//

6. Matrix.mappoints (points);

7. LOG.I ("Test", points[0]+ "");

8. LOG.I ("Test", points[1]+ "");

The result is a point coordinate of (28.0,37.0)

It can be concluded that the order of the transformations is performed first Postscale (2.0f,3.0f), in the executed Posttranslate (8.0f,7.0f). This is why some people are likened to post ... is to grow forward, i.e. for a matrix setting, all post .... Is carried forward along the way.



3. Order of execution when pre and post alternately occur

[Java] view plaincopy

1. Matrix matrix=new matrix ();

2. float[] Points=new float[]{10.0f,10.0f};

3.

4. Matrix.postscale (2.0f, 3.0f);

5. Matrix.prerotate (90);

6. Matrix.mappoints (points);

7. LOG.I ("Test", points[0]+ "");

8. LOG.I ("Test", points[1]+ "");

The result is a point coordinate of ( -20.0,30.0)


The Pre ... and post order for a change

[Java] view plaincopy

1. Matrix matrix=new matrix ();

2. float[] Points=new float[]{10.0f,10.0f};

3.

4. Matrix.prerotate (90);

5. Matrix.postscale (2.0f, 3.0f);

6. Matrix.mappoints (points);

7. LOG.I ("Test", points[0]+ "");

8. LOG.I ("Test", points[1]+ "");

The result is the point coordinate is still ( -20.0,30.0)

Visible, always pre-executed first. Look at the following:

[Java] view plaincopy

1. Matrix matrix = new Matrix ();

2. float[] points = new float[] {10.0f, 10.0f};

3.

4. Matrix.postscale (2.0f, 3.0f);//1th step

5. Matrix.prerotate (90);//2nd Step

6. Matrix.posttranslate (8.0f, 7.0f);//3rd Step

7. Matrix.prescale (1.5f, 2.5f);//4th step

8. Matrix.mappoints (points);

9. LOG.I ("Test", Points[0] + "");

LOG.I ("Test", Points[1] + "");

The result is the point coordinate is still ( -42.0,52.0)
Following the previous conclusions and projections, it can be found that the order of execution is 4----2----1---3

Looking at the following, added a section of code for Setscale:

[Java] view plaincopy

1. Matrix matrix = new Matrix ();

2. float[] points = new float[] {10.0f, 10.0f};

3.

4. Matrix.postscale (2.0f, 3.0f);//1th step

5. Matrix.prerotate (90);//2nd Step

6. Matrix.setscale (1.4f, 2.6f);//3rd Step

7. Matrix.posttranslate (8.0f, 7.0f);//4th step

8. Matrix.prescale (1.5f, 2.5f);//5th step

9. matrix.mappoints (points);

LOG.I ("Test", Points[0] + "");

LOG.I ("Test", Points[1] + "");

The result is the point coordinate is still (29.0,72.0)
After calculation, it can be found that in the 3rd step Setscale before the 1th, 2 steps are not used at all, is directly covered by the 3rd step Setscale, executed from 3rd.

The order is 2---1----3----5----4, because 2, 1 is covered, so no effect, equivalent to direct execution 3-----5----4

Summary: Finally, it can be concluded that in the matrix before the transformation of all the settings, first detect there is no setscale, if any, jump directly to setscale that step to start the transformation, and then in the backward execution of all the pre ... Transform, followed by all post .... The transformation. So when you set the matrix transformation, be sure to pay attention to the order, different order, there will be different results.

The difference between Setscale,prescale and Postscale

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.