Android-using the matrix to process the bitmap

Source: Internet
Author: User

The matrix is used in 1.Android to scale, rotate, pan, skew, and so on.

The matrix is a 3*3, and its values correspond to the following:

The following gives the properties of the specific coordinates corresponding to the deformation
|scalex, Skewx, translatex|
|skewy, ScaleY, translatey|
| |, 0, Scale |

matrix provides several ways to control the image transformation:
settranslate (float dx,float dy): Controls the Matrix for displacement.
Setskew (Float kx,float ky): Controls the Matrix to tilt, KX, KY to the X-, y-direction scale.
Setskew (float kx,float ky,float px,float py): The control matrix is tilted with PX, py as the axis, and KX, KY is the tilt ratio in x and y directions.
SetRotate (float degrees): controls the matrix for depress angular rotation with the axis (0,0).
SetRotate (float degrees,float px,float py): Controls the matrix for the rotation of the depress angle, with the axis (px,py).
Setscale (float sx,float sy): Sets the matrix for scaling, SX, SY for the X-, y-direction scale.
Setscale (float sx,float sy,float px,float py): Sets the Matrix to be scaled (px,py) for the axis, SX, SY is the scale in X, y direction.
Note: The above set methods have corresponding post and pre methods, which are considered to be inserted into a queue when the matrix calls a series of set,pre,post methods. Of course, Execute is called from beginning to end in the queue. Where pre means inserting a method in the head of the team, post means inserting a method at the tail of the team. The set indicates that the current queue is emptied and is always in the middle of the queue. After a set has been executed: The pre method is always inserted at the top of the queue at the front of the set, and the Post method is always inserted at the end of the queue at the back of the set

Demo

Package Com.example.testaa;import Org.androidannotations.annotations.afterviews;import Org.androidannotations.annotations.click;import Org.androidannotations.annotations.eactivity;import Org.androidannotations.annotations.uithread;import Org.androidannotations.annotations.viewbyid;import Android.app.activity;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.graphics.matrix;import Android.util.log;import Android.widget.button;import Android.widget.ImageView; Import Android.widget.Toast; @EActivity (r.layout.activity_main) public class Mainactivity extends activity {@ Viewbyidimageview Iv1, @ViewByIdImageView iv2, @ViewByIdButton btn1, @ViewByIdButton btn2; @ViewByIdButton btn3;@ Viewbyidbutton btn4; @ViewByIdButton btn5; Bitmap Bitmap = null;/** * Processing After the view is loaded */@AfterViewsvoid afterviewprocess () {Bitmap = Bitmapfactory.decoderesource ( Getresources (), R.drawable.lena);} /** * Zoom Out */@Clickvoid btn1 () {Matrix matrix = new Matrix (); Matrix.setscale (0.5f, 0.5f); Bitmap BM = Bitmap.createbitmap (Bitmap, 0, 0, bitmap.getwidth (), Bitmap.getheight (), Matrix, True); Iv2.setimagebitmap (BM);    Showtoast (matrix);} /** * Zoom In/out first/@Clickvoid btn2 () {Matrix matrix = new Matrix () Matrix.setscale (0.5f, 0.5f);//Zoom out to the original half matrix.postr Otate (45.0f);//rotate 45 degrees = = Matrix.setsincos (0.5f, 0.5f); Bitmap BM = bitmap.createbitmap (Bitmap, 0, 0, bitmap.getwidth (), Bitmap.getheight (), Matrix, True); Iv2.setimagebitmap ( BM); Showtoast (matrix);}  /** * pan */@Clickvoid btn3 () {Matrix matrix = new Matrix (); Matrix.settranslate (Bitmap.getwidth ()/2, bitmap.getheight ()/ 2);//Left down translation Bitmap BM = bitmap.createbitmap (Bitmap, 0, 0, bitmap.getwidth (), Bitmap.getheight (), Matrix, True); Iv2.setimagebitmap (BM); showtoast (matrix);}  /** * chamfered */@Clickvoid Btn4 () {Matrix matrix = new Matrix () Matrix.setskew (0.5f, 0.5f);//chamfered Matrix.postscale (0.5f, 0.5f);// Zoom out for the original half Bitmap BM = bitmap.createbitmap (Bitmap, 0, 0, bitmap.getwidth (), Bitmap.getheight (), Matrix, True); Iv2.setimagebitmap (BM); showtoast (matrix);} /*** Equivalent to a free transform * by a rectangle into a quadrilateral/@Clickvoid btn5 () {Matrix matrix = new Matrix (); float[] src = new float[] {0, 0,//upper left Bitmap.get Width (), 0,//right Upper Bitmap.getwidth (), Bitmap.getheight (),//Bottom right 0, bitmap.getheight ()};//left float[] dst = new float[] {0, 0, Bitmap.getwidth (), 30,bitmap.getwidth (), Bitmap.getheight ()-30,0,bitmap.getheight ()};matrix.setpolytopoly (src, 0, DST, 0, SRC.LENGTH/2); Bitmap BM = bitmap.createbitmap (Bitmap, 0, 0, bitmap.getwidth (), Bitmap.getheight (), Matrix, True); Iv2.setimagebitmap ( BM); Showtoast (matrix);} /** * Display values in Matrix * @param matrix */@UiThreadvoid Showtoast (Matrix matrix) {String string = ""; float[] values = new Float[9];m Atrix.getvalues (values); for (int i = 0; i < values.length; i++) {string + = "matrix.at" + i + "=" + Values[i];} Toast.maketext (This, string, Toast.length_short). Show (); LOG.D ("TEST", String);}}


Here are the results of the following actions for each image:



For the entire project: http://download.csdn.net/detail/nuptboyzhb/7261933


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.