Knowledge about Matrix multiplication and multiplication in Android

Source: Internet
Author: User

In the previous article, we mentioned that in Android, scale, rotation, and translation are both defined in the form of Matrix, in fact, in graphics, the transformations of these plane images exist in the form of matrices. Let's review the Matrix Representation of scale, rotation and translation in Android in the middle left and right of the following:


If we simply apply a transformation, we all know that we can simply use a matrix to multiply the corresponding vertex. But for scaling and rotation, their axes are based on the origin point (). For example, we will reduce it by one time. The Code is as follows: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PHByZSBjbGFzcz0 = "brush: java;"> public void onDraw (Canvas canvas) {canvas. drawBitmap (bitmap, 0, 0, null); matrix. reset (); matrix. postScale (0.5f, 0.5f); canvas. drawBitmap (bitmap, matrix, null );}

The effect is shown in the right figure:


What should we do if we want to scale it based on the image center? We also mentioned in the previous article that combined transformation is required,

1) First, translate the image from the center to the origin. This is the application transformation T.

2) apply scaling and transformation S to the graph

3) then translate the image back to the center and apply the transform-T

The formula for their combination transformation is given below:


The code in Android is as follows:

public void onDraw(Canvas canvas){canvas.drawBitmap(bitmap, 0, 0, null);matrix.reset();matrix.postScale(0.5f, 0.5f);matrix.preTranslate(-pivotX, -pivotY);matrix.postTranslate(pivotX, pivotY);canvas.drawBitmap(bitmap, matrix, null);}
Let's take a look at the translation (The Centers of the images are Tx and Ty ):


We can see that in the Code, after we scale, we also set the following two Translation Transformations for the image:

matrix.preTranslate(-pivotX, -pivotY);matrix.postTranslate(pivotX, pivotY);
What is PreTranlsate and what is postTranslate? Let's take a look at the definition in Android.

PreTranslate:

    /**     * Preconcats the matrix with the specified translation.     * M' = M * T(dx, dy)     */    public boolean preTranslate(float dx, float dy) {        return native_preTranslate(native_instance, dx, dy);    }

PostTranslate:

    /**     * Postconcats the matrix with the specified translation.     * M' = T(dx, dy) * M     */    public boolean postTranslate(float dx, float dy) {        return native_postTranslate(native_instance, dx, dy);    }

We can see that pre is to multiply the current matrix by T, while post is to multiply the current matrix by T. (The matrix multiplication does not meet the exchange rate, so the results of the two multiplication methods are different)

In graphics, the right multiplication of matrix M is A * M, while that of matrix M is M *, it can be interpreted as "right multiplication", "Left multiplication", and "Left multiplication.

After comparison, we can see that pre actually performs the right Multiplication operation, while post executes the left Multiplication operation.

This is because, in image processing, the closer the Matrix to the right is to be executed first, so the pre (that is, the meaning of the first) matrix T (Scale, Rotation is also the same) it will be executed before the Scale set at the beginning of the first step, and because post (meaning) is left multiplication, it will be placed on the leftmost side, then it will be executed at the end.

Therefore, the above three steps actually mean:

1) Add a Scale operation to the left of the current matrix. Because there is no reset operation before, the current matrix is actually only S.

2) execute in the current matrix (S)BeforeFirst, execute a translation operation (preTranslate) to the origin, which is actually a right multiplication (S * T ).

3) execute in the current matrix (S * T)AfterThen, execute the operation (postTranslate) from the origin point to the center point, that is, perform a left multiplication (-T * S * T ).

This is the operation of right multiplication and left multiplication in the Matrix in the graphics corresponding to the multiplication before and after the Matrix (Matrix) in Android.

I don't know if there are any mistakes. If there are any mistakes, I hope some friends can correct them. thank you and wish you a happy New Year!

The following is the previous article about the relationship between matrix and graph transformation in graphics. If you are interested, you can also read it:

Introduction to Matrix and graphic transformation in 2D planes

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.