Android matrix Introduction 1

Source: Internet
Author: User

Matrix is called a matrix in Chinese. It is introduced in advanced mathematics. In terms of image processing, it is mainly used for operations such as plane scaling, translation, and rotation.

First, we will introduce matrix operations. Addition and subtraction are not required. It is too simple to add the corresponding bits. Image processing mainly uses multiplication. The following is a multiplication formula:

In Android, matrix is a 3*3 matrix consisting of 9 float values. For example.

Without professional tools, the painting is ugly. The SiNx and cosx above indicate the COs and sin values of the rotation angle. Note that the rotation angle is calculated clockwise. Translatex and translatey indicate the translation quantity of X and Y. Scale is the scale ratio, 1 is the same, 2 is the scale 1/2, this way.

Next, we will try the effect of matrix on Android.

Java Code
  1. Public class myview extends view {
  2. Private bitmap mbitmap;
  3. Private matrix mmatrix = new matrix ();
  4. Public myview (context ){
  5. Super (context );
  6. Initialize ();
  7. }
  8. Private void initialize (){
  9. Mbitmap = (bitmapdrawable) getresources (). getdrawable (R. drawable. Show). getbitmap ();
  10. Float cosvalue = (float) math. Cos (-math. PI/6 );
  11. Float sinvalue = (float) math. Sin (-math. PI/6 );
  12. Mmatrix. setvalues (
  13. New float [] {
  14. Cosvalue,-sinvalue, 100,
  15. Sinvalue, cosvalue, 100,
  16. 0, 0, 2 });
  17. }
  18. @ Override protected void ondraw (canvas ){
  19. // Super. ondraw (canvas); // Of course, if there are other elements to draw on the interface, just write this sentence.
  20. Canvas. drawbitmap (mbitmap, mmatrix, null );
  21. }
  22. }
Public class myview extends view {private bitmap mbitmap; private matrix mmatrix = new matrix (); Public myview (context) {super (context); initialize ();} private void initialize () {mbitmap = (bitmapdrawable) getresources (). getdrawable (R. drawable. show )). getbitmap (); float cosvalue = (float) math. cos (-math. PI/6); float sinvalue = (float) math. sin (-math. PI/6); mmatrix. setvalues (new float [] {cosvalue,-S Invalue, 100, sinvalue, cosvalue, 0, 0, 2}) ;}@ override protected void ondraw (canvas) {// super. ondraw (canvas); // Of course, if there are other elements to be drawn on the interface, just write this sentence. Canvas. drawbitmap (mbitmap, mmatrix, null );}}

The running result is as follows:

Take the top left corner as the vertex, zoom in half, rotate 30 degrees counter-clockwise, and then translate 50 pixels along the X axis and Y axis, respectively. The Code writes 100. Why translate 50, because it is scaled in half.

You can set the value of matrix by yourself, or try to multiply the two matrices to set the value so that you can be more familiar with matrix.

The direct assignment method mentioned here may be a bit difficult to understand, but fortunately, andrid provides a more convenient method for the matrix, which will be introduced in the next 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.