Matrix usage of Android variants (Transform) _android

Source: Internet
Author: User

Introduction

In a recent study of Android variants, Android's 2D variants (including scaling, twisting, panning, rotating, and so on) can be implemented through the matrix, and 3D variants can be achieved by camera. Next, I'll share with you what I've been studying for the first two days, and look at the matrix's usage.

Effect chart

After Metamorphosis

Matrix matrices

The coordinate transformation matrix, a 3*3 matrix, is used to coordinate the transformation of the graph.

Figure 1.1 A is a coordinate matrix, C is the original matrix, R is a and C matrix multiplication demerit, then you can know: (Matrix knowledge, the university has not been able to learn the injury AH)

X ' = a*x + B*y + C

Y ' = d*x + b*y + F

The last column has very little data mentioned, but the initial value of g=h=0, we can go to change the value of the test, change to 3D effect, but the value did not see the rule, then I for scaling, the initial value of 1.

Initialize the coordinate matrix to {1,0,0, 0,1,0, 0,0,1}

The above is the basic algorithm, then the specific matrix x row x column value represents the above, not to look at the simple

If a={1,0,100, 0,1,-100, 0,0,2}, then you can figure out

X ' = x + 100;

Y ' = y-100;

That is, on the original basis, move right 100, up 100, in pixels. Third Act 2, expressed as the previous ratio of 1/2, remember this is easy to mistake.

The following gives the properties of the corresponding deformation of the specific coordinates

|scalex, Skewx, translatex|

|skewy, ScaleY, translatey|

|, 0, Scale |

Practice

Use the code to see the specific usage

Copy Code code as follows:

public class Matrixtransformview extends View {

Private Matrix Mmatrix;
Private Paint Mpaint = new Paint (Paint.anti_alias_flag);
Private Bitmap Mbitmap;

Public Matrixtransformview {
Super (context);
}

Public Matrixtransformview (context context, AttributeSet Attrs) {
Super (context, attrs);
}

public void setdrawable (int resid) {
Mbitmap = Bitmapfactory.decoderesource (GetContext (). Getresources (), resid);
}

/*
* Set the matrix and redraw
*/
public void Setmatrixvalues (float[] array) {
if (Mmatrix = = null) {
Mmatrix = new Matrix ();
}
Mmatrix.reset ();
Mmatrix.setvalues (array);
Invalidate ();
}

public void Resetmatrix () {
if (Mmatrix!= null) {
Mmatrix.reset ();
}
Invalidate ();
}

@Override
protected void OnDraw (Canvas Canvas) {
if (Mmatrix!= null) {
Paint Paint = Mpaint;
Canvas.drawbitmap (Mbitmap, Mmatrix, paint);
}

Super.ondraw (canvas);
}
}

The matrix coordinate value of 3*3 is set up by means of setvalues method.

The point is that when calling Setmatrixvalues, you need to call the Invalidate method, so that the view is called OnDraw to redraw.

The basic use of matrices is these, often in the development process, not directly through the matrix coordinates to achieve deformation, because if you want to achieve the choice, then more complex, involving trigonometric functions, the data has been forgotten about the same people, is painful, of course, if you have to use, it is not difficult to calculate.

So in order to avoid the direct use of matrix coordinates to manipulate deformation, the matrix class provides a way to change:

Set mode: Setscale, Setskew, Settranslate, setrotate

Post method: Postscale, Postskew, Posttranslate, postrotate

Pre way: Prescale, Preskew, Pretranslate, prerotate

The Set method is set directly, and each call to the set methods resets the matrix first. Post can be understood to be set several times effectively, the effect is cumulative. The pre here is understood to be the same as the post way, the back of the 3D when entangled.

Look at the code:

Copy Code code as follows:

public class Matrixtransformview extends View {

Private Matrix Mmatrix;
Private Paint Mpaint = new Paint (Paint.anti_alias_flag);
Private Bitmap Mbitmap;

Public Matrixtransformview {
Super (context);
}

Public Matrixtransformview (context context, AttributeSet Attrs) {
Super (context, attrs);
}

public void setdrawable (int resid) {
Mbitmap = Bitmapfactory.decoderesource (GetContext (). Getresources (), resid);
}

/*
* Set the matrix and redraw
*/
public void Setmatrixvalues (float[] array) {
if (Mmatrix = = null) {
Mmatrix = new Matrix ();
}
Mmatrix.reset ();
Mmatrix.setvalues (array);
Invalidate ();
}

public void Postmatrixscale (float ScaleX, float scaleY, float centerx, float centery) {
if (Mmatrix = = null) {
Mmatrix = new Matrix ();
}
Mmatrix.prescale (ScaleX, ScaleY, CenterX, centery);
Invalidate ();
}

public void Postmatrixskew (float skewx, float skewy, float centerx, float centery) {
if (Mmatrix = = null) {
Mmatrix = new Matrix ();
}
Mmatrix.postskew (Skewx, Skewy, CenterX, centery);
Invalidate ();
}

public void Postmatrixtranslate (float Translatex, float translatey) {
if (Mmatrix = = null) {
Mmatrix = new Matrix ();
}
Mmatrix.posttranslate (Translatex, Translatey);
Invalidate ();
}

public void Postmatrixrotate (float degree, float CenterX, float centery) {
if (Mmatrix = = null) {
Mmatrix = new Matrix ();
}
Mmatrix.postrotate (degree, CenterX, centery);
Invalidate ();
}

public void Resetmatrix () {
if (Mmatrix!= null) {
Mmatrix.reset ();
}
Invalidate ();
}

@Override
protected void OnDraw (Canvas Canvas) {
if (Mmatrix!= null) {
Paint Paint = Mpaint;
Canvas.drawbitmap (Mbitmap, Mmatrix, paint);
}

Super.ondraw (canvas);
}
}

This is the basic usage of the matrix.

Extended

Deformation is required canvas to draw, canvas drawing needs to bitmap, so this piece of the use of a inherited from view control, through the setdrawable way to set bitmap, then select the target must be a bitmap, in the article demo, Bitmap is obtained by using the Setdrawable method of an int-type resource, and if you want to distort other controls, such as ViewGroup, you can:

Copy Code code as follows:

Matrix M = new Matrix ();
M.setvalues (new float[] {
1, 0, 0,
0, 1, 0,
0, 0, 1
});
Bitmap BP = Bitmap.createbitmap (Viewgroup.getwidth (), Viewgroup.getheight (), Bitmap.Config.RGB_565);
Canvas can = new Canvas (BP);
Viewgroup.draw (CAN);
bp = Bitmap.createbitmap (BP, 0, 0, bp.getwidth (), Bp.getheight (), M, true);
Img.setimagebitmap (BP);

By converting ViewGroup to bitmap, and then customizing an image to deform, hide the viewgroup to achieve the effect.

Questions

1. If anyone knows the difference between post,pre, please let me know and see if my understanding is correct.

2. Can achieve viewgroup direct deformation, rather than the kind I said above.

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.