Today, I learned about the Matrix related to images in Android.
First look at the sample code:
Package cn.com. chenzheng_java; </P> <p> Import android. app. activity; <br/> Import android. graphics. bitmap; <br/> Import android. graphics. bitmapfactory; <br/> Import android. graphics. matrix; <br/> Import android. graphics. drawable. bitmapdrawable; <br/> Import android. OS. bundle; <br/> Import android. view. viewgroup. layoutparams; <br/> Import android. widget. imageview; <br/> Import android. widget. linearlayout; <br/> Import android. widget. imageview. scaletype; </P> <p> public class matrixactivity extends activity {</P> <p> @ override <br/> protected void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> settitle ("eoeandroid Tutorial: Zoom and rotate images-by: iceskysl"); <br/> linearlayout linlayout = new linearlayout (this ); </P> <p> // load the image to be operated. Here is the logo image of eoeandroid. <br/> bitmap bitmaporg = bitmapfactory. decoderesource (getresources (), <br/> r. drawable. blackk); </P> <p> // obtain the width and height of the image. <br/> int width = bitmaporg. getwidth (); <br/> int Height = bitmaporg. getheight (); </P> <p> // define the width and height of the pre-converted image. <br/> int newwidth = 200; <br/> int newheight = 200; </P> <p> // calculate the scaling rate. The new size is exclusive to the original size. <br/> float scalewidth = (float) newwidth) /width; <br/> float scaleheight = (float) newheight)/height; </P> <p> // create a matrix object used to operate images <br/> matrix = new matrix (); </P> <p> // scaling image <br/> matrix. postscale (scalewidth, scaleheight); </P> <p> // rotating an image <br/> matrix. postrotate (45); </P> <p> // create an image <br/> bitmap resizedbitmap = bitmap. createbitmap (bitmaporg, 0, 0, <br/> width, height, matrix, true); </P> <p> // converts the bitmap created above to a drawable object, so that it can be used in imageview and imagebutton <br/> bitmapdrawable BMI = new bitmapdrawable (resizedbitmap ); </P> <p> // create an imageview <br/> imageview = new imageview (this ); </P> <p> // set the image to the image converted above the imageview. <br/> imageview. setimagedrawable (BMI); </P> <p> // display the image in the center <br/> imageview. setscaletype (scaletype. center); </P> <p> // Add the imageview to the layout template <br/> linlayout. addview (imageview, <br/> New linearlayout. layoutparams (<br/> layoutparams. fill_parent, layoutparams. fill_parent <br/>); </P> <p> // set this activity template <br/> setcontentview (linlayout ); <br/>}</P> <p>
Result diagram:
Bytes -------------------------------------------------------------------------------------
Now, we will introduce in detail the main applications of the android. Graphics. Matrix class.
Matrix Operations are divided into four types: translate, rotate, scale, and skew, each transformation provides the set, post, and Pre operations in the android API. Except for the translate operation, the center point can be specified for all the other three operations.
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.
For specific methods and usage methods, refer to the API. Here we will not list them one by one. It is too much.