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">
- Matrix M =
New
Matrix ();
- M. postrotate (30
);
- 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">
- Matrix M =
New
Matrix ();
- M. settranslate (100
,
100
);
- 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.
- Package com. EOE
Android
. Demo. testcode;
-
- Import Android. app
. Activity;
- Import Android. Graphics. Bitmap;
- Import Android. Graphics. bitmapfactory;
- Import Android. Graphics. matrix;
- Import Android. Graphics. drawable. bitmapdrawable;
- Import Android. OS. Bundle;
- Import Android. View. View
Group. Layout
Params;
- Import Android. widget. imageview;
- Import Android. widget. linearlayout;
- Import Android. widget. imageview. scaletype;
-
- Public class bitmaptest extends activity {
- Public void oncreate (bundle icicle ){
- Super. oncreate (icicle );
- Settitle ("eoeandroid Tutorial: Scaling and rotating images-by: iceskysl ");
- Linearlayout linlayout = new linearlayout (this );
-
- // Load the image to be operated. Here is the logo image of eoeandroid.
- Bitmap bitmaporg = bitmapfactory. decoderesource (getresources (),
- R. drawable. eoe_android );
-
- // Obtain
Width and height of the image
- Int width = bitmaporg. getwidth ();
- Int Height = bitmaporg. getheight ();
-
- // Define
Width and height of the pre-converted Image
- Int newwidth = 200;
- Int newheight = 200;
-
- // Calculate the zooming rate. The new size excludes the original size.
- Float scalewidth = (float) newwidth)/width;
- Float scaleheight = (float) newheight)/height;
-
- // Create a matrix object for image operations
- Matrix matrix = new matrix ();
-
- // Scaling the image
- Matrix. postscale (scalewidth, scaleheight );
-
- // Rotate the image
- Matrix. postrotate (45 );
-
- // Create a new image
- Bitmap resizedbitmap = bitmap. createbitmap (bitmaporg, 0, 0,
- Width, height, matrix, true );
-
- // Convert the bitmap created above to a drawable object so that it can be used in imageview and imagebutton.
- Bitmapdrawable BMI = new bitmapdrawable (resizedbitmap );
-
- // Create an imageview
- Imageview = new imageview (this );
-
- // Set
The image in imageview is the image converted above.
- Imageview. setimagedrawable (BMI );
-
- // Center the image
- Imageview. setscaletype (scaletype. center );
-
- // Add the imageview to the layout Template
- Linlayout. addview (imageview,
- New linearlayout. layoutparams (
- Layoutparams. fill_parent, layoutparams. fill_parent
- )
- );
-
- // Set this activity
Template
- Setcontentview (linlayout );
- }
- }
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 );
}
}
}