Android-uses Matrix to process Bitmap

Source: Internet
Author: User

1. In Android, the Matrix is used to scale, rotate, translate, and slice the image.

Matrix is a 3*3 Matrix with the following values:

The following shows the deformation attributes of the specific coordinates.
| ScaleX, skewX, and translateX |
| SkewY, scaleY, and translateY |
| 0, 0, scale |

Matrix provides some methods to control image transformation:
SetTranslate (float dx, float dy): controls the Matrix for displacement.
SetSkew (float kx, float ky): Controls Matrix skew. kx and ky are the ratios in the X and Y directions.
SetSkew (float kx, float ky, float px, float py): controls the tilt ratio of Matrix to px, py as the axis, kx, ky as X, and Y.
SetRotate (float degrees): controls the rotation of the depress angle on the Matrix. The axis is (0, 0 ).
SetRotate (float degrees, float px, float py): controls the rotation of the depress angle of the Matrix. The axis is (px, py ).
SetScale (float sx, float sy): sets the Matrix for scaling. sx, sy, and Y scale.
SetScale (float sx, float sy, float px, float py): sets Matrix to scale with (px, py) as the axis, sx and sy are scaled in the X and Y directions.
Note: The above set methods have the corresponding post and pre methods. When Matrix calls a series of set, pre, and post methods, these methods can be regarded as being inserted into a queue. of course, the call is executed in the order from beginning to end in the queue. pre indicates inserting a method in the queue header, and post indicates inserting a method at the end of the queue. set indicates that the current queue is cleared and always located in the middle of the queue. after a set operation is executed: The pre method is always inserted at the beginning 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 and rotate */@ Clickvoid btn2 () {Matrix matrix = new Matrix (); matrix. setScale (0.5f, 0.5f); // reduce it to half of the original matrix. postRotate (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); // Pan Bitmap bm = Bitmap to the bottom left. createBitmap (bitmap, 0, 0, bitmap. getWidth (), bitmap. getHeight (), matrix, true); iv2.setImageBitmap (bm); showToast (matrix);}/*** diagonal cut */@ Clickvoid btn4 () {Matrix matrix = new Matrix (); matrix. setSkew (0.5f, 0.5f); // diagonal tangent matrix. postScale (0.5f, 0.5f); // scale down to half of the original Bitmap bm = Bitmap. createBitmap (bitmap, 0, 0, bitmap. getWidth (), bitmap. getHeight (), matrix, true); iv2.setImageBitmap (bm); showToast (matrix);}/*** is equivalent to a free transformation * from a rectangle to a quadrilateral */@ Clickvoid btn5 () {Matrix matrix = new Matrix (); float [] src = new float [] {0, 0, // bitmap on the left. getWidth (), 0, // bitmap on the right. getWidth (), bitmap. getHeight (), // lower right 0, bitmap. getHeight ()}; // 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 the value in the matrix * @ param Matrix */@ UiThreadvoid showToast (matrix) {String string = ""; float [] values = new float [9]; matrix. 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 );}}


The following are the results of the following operations on the image:

: Http://download.csdn.net/detail/nuptboyzhb/7261933


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.