Android image scaling-Matrix

Source: Internet
Author: User

Android uses matrix to scale and rotate images.
You will learn how to operate images through matrix.



Matrix Operations are divided into four types: translate, rotate, scale, and skew.

The android API provides set, post, and Pre operations. In addition to translate, you can specify the center point for all the other three operations.

Set is to directly set the value of the matrix. Each time a set is set, the array of the entire matrix is changed.

Post is the posterior multiplication. The current matrix is multiplied by the matrix given by the parameter. You can use post multiple times in a row to complete the entire transformation. For example

To 30 degrees, and then to the (100,100) place, you can do this:

<Type = "application/X-Shockwave-flash" width = "14"
Height = "15"
Src = "http://www.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf"

Src = "http://www.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf"

Flashvars = "Clipboard = matrix % 20 m % 20% 3D % 20new % 20 matrix () % 3B % 0a % 0 AM. postrotate (30) % 3B % 0a % 0 AM. posttranslate (100% 2C % 20100) % 3B % 20% 20"
Quality = "high" allowScriptAccess = "always"
Type = "application/X-Shockwave-flash"
Pluginspage = "http://www.macromedia.com/go/getflashplayer" Height = "15"
Width = "14">

  1. Matrix M =
    New
    Matrix ();
  2. M. postrotate (30
    );
  3. M. posttranslate (100
    ,
    100
    );
Matrix m = new Matrix();m.postRotate(30);m.postTranslate(100, 100);  

 

This achieves the desired effect.

PRE is a forward multiplication. The matrix given by the parameter is multiplied by the current matrix. Therefore, the operation occurs at the beginning of the current matrix. For example, in the above example, if pre is used

In this way:

<Type = "application/X-Shockwave-flash" width = "14"
Height = "15"
Src = "http://www.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf"

Src = "http://www.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf"

Flashvars = "Clipboard = matrix % 20 m % 20% 3D % 20new % 20 matrix () % 3B % 0a % 0 AM. settranslator (100% 2C % 20100) % 3B % 0a % 0 AM. prerotate (30) % 3B"
Quality = "high" allowScriptAccess = "always"
Type = "application/X-Shockwave-flash"
Pluginspage = "http://www.macromedia.com/go/getflashplayer" Height = "15"
Width = "14">

  1. Matrix M =
    New
    Matrix ();
  2. M. settranslate (100
    ,
    100
    );
  3. M. prerotate (30
    );
Matrix m = new Matrix();m.setTranslate(100, 100);m.preRotate(30);

Rotation, scaling, and skew can all be performed around a central point. If not specified, the rotation is performed around (0, 0) points by default.





  1. Package com. EOE
    Android
    . Demo. testcode;
  2. Import Android. app
    . Activity;
  3. Import Android. Graphics. Bitmap;
  4. Import Android. Graphics. bitmapfactory;
  5. Import Android. Graphics. matrix;
  6. Import Android. Graphics. drawable. bitmapdrawable;
  7. Import Android. OS. Bundle;
  8. Import Android. View. View
    Group. Layout
    Params;
  9. Import Android. widget. imageview;
  10. Import Android. widget. linearlayout;
  11. Import Android. widget. imageview. scaletype;
  12. Public class bitmaptest extends activity {
  13. Public void oncreate (bundle icicle ){
  14. Super. oncreate (icicle );
  15. Settitle ("eoeandroid Tutorial: Scaling and rotating images-by: iceskysl ");
  16. Linearlayout linlayout = new linearlayout (this );
  17. // Load the image to be operated. Here is the logo image of eoeandroid.
  18. Bitmap bitmaporg = bitmapfactory. decoderesource (getresources (),
  19. R. drawable. eoe_android );
  20. // Obtain
    Width and height of the image
  21. Int width = bitmaporg. getwidth ();
  22. Int Height = bitmaporg. getheight ();
  23. // Define
    Width and height of the pre-converted Image
  24. Int newwidth = 200;
  25. Int newheight = 200;
  26. // Calculate the zooming rate. The new size excludes the original size.
  27. Float scalewidth = (float) newwidth)/width;
  28. Float scaleheight = (float) newheight)/height;
  29. // Create a matrix object for image operations
  30. Matrix matrix = new matrix ();
  31. // Scaling the image
  32. Matrix. postscale (scalewidth, scaleheight );
  33. // Rotate the image
  34. Matrix. postrotate (45 );
  35. // Create a new image
  36. Bitmap resizedbitmap = bitmap. createbitmap (bitmaporg, 0, 0,
  37. Width, height, matrix, true );
  38. // Convert the bitmap created above to a drawable object so that it can be used in imageview and imagebutton.
  39. Bitmapdrawable BMI = new bitmapdrawable (resizedbitmap );
  40. // Create an imageview
  41. Imageview = new imageview (this );
  42. // Set
    The image in imageview is the image converted above.
  43. Imageview. setimagedrawable (BMI );
  44. // Center the image
  45. Imageview. setscaletype (scaletype. center );
  46. // Add the imageview to the layout Template
  47. Linlayout. addview (imageview,
  48. New linearlayout. layoutparams (
  49. Layoutparams. fill_parent, layoutparams. fill_parent
  50. )
  51. );
  52. // Set this activity
    Template
  53. Setcontentview (linlayout );
  54. }
  55. }

Copy code

No XML definition is used here.
Layout templates directly generate the required templates and view components in the code. You can define XML templates. Other principles are the same.





In the game
Development
, Custom
View
Is a very important feature
The following describes how to draw the four basic primary keys on the View:
Bitmap: used to hold pixels (Android
. Graphics. Bitmap)
Canvas: used to call the drawing method. It is the entrance to the whole process.
The object to be drawn: for example, drawing a bitmap, rectangle or circle.
Paint: Set
Draw the color and style of the image

Matrix: contains a 3x3 matrix used for transformation matching (in image processing). matrix does not have a struct and must be initialized. By implementing the reset () method or set .. () method.
Next let's look at the code
:
Import Android. app
. Activity;
Import Android. content. context;
Import Android. content. res. Resources;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. Graphics. Bitmap;
Import Android. Graphics. bitmapfactory;
// Import Android. Graphics. color;
Import Android. Graphics. matrix;
Import Android. Graphics. Canvas;
Import Android. Graphics. paint;
// Import Android. Graphics. rect;

Public class testmartix extends activity {
// Create bitmap, canvas, and paint
Private bitmap IMG, r_img;
Private canvas;
Private paint;
// Layout is not required because it is a custom view.
File

Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
// Call the custom View
Setcontentview (New myview (this ));

}

Public class myview extends view {
// View Initialization
Public myview (context ){
Super (context );

// Bitmapfactory: Creates a bitmap object from the source, which includes files, streams, or arrays.
IMG = bitmapfactory. decoderesource (getresources (), R. drawable. IMG );
// Create a matrix object
Matrix matrix = new matrix ();
// Flip the matrix. The parameter is float.
Matrix. postrotate (90 );
// Matrix. postrotate (0 );
// Obtain
Bitmap height and width
Int width = IMG. getwidth ();
Int Height = IMG. getheight ();
// After the source bitmap changes through a matrix, an immutable bitmap is returned.
B _img = bitmap. createbitmap (IMG, 0, 0, width, height, matrix, true );
Paint = new paint ();

}
// This method must be implemented when customizing a view
Public void ondraw (canvas ){
// When rewriting the parent class method, you must first call the parent class Method
Super. ondraw (canvas );
// Use canvas to draw a bitmap on The View and set its style and color
Canvas. drawbitmap (B _img, 10, 10, paint );

// This method is used to update the view. It is mostly used in combination with threads.
// This. invalidate ()
// The following code is used to draw a solid rectangle on The View and set the color to green,
// Paint. setcolor (color. Green );
// Paint. setantialias (true );
// Canvas. drawrect (New rect (30,30, 100,100), paint );

}
}

}

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.