Bitmap rotation can also be achieved through matrix or canvas.
The postrotate method is used to set the rotation angle. Then, the createbitmap method is used to create a bitmap object that has been rotated. Finally, the drawbitmap method is used to draw the object to the screen. Therefore, the rotation operation is implemented.
In the following example, both the original bitmap and the rotated bitmap are drawn to the screen for comparison.
package xiaosi.bitmap;import android.app.Activity;import android.os.Bundle;public class mianActivity extends Activity{private BitmapView bitmapView = null;@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);bitmapView = new BitmapView(this);setContentView(bitmapView);}}
Bitmapview. Java
Package xiaosi. bitmap; import android. content. context; import android. content. res. resources; import android. graphics. bitmap; import android. graphics. bitmapfactory; import android. graphics. canvas; import android. graphics. color; import android. graphics. matrix; import android. view. view; public class bitmapview extends view {public bitmapview (context) {super (context);} // rewrite the ondraw method public void ondraw (canvas) {// obtain the resource file reference resresources res = getresources (); // obtain the image resource file bitmap BMP = bitmapfactory. decoderesource (Res, R. drawable. h); // set the canvas background to a white canvas. drawcolor (color. black); // draw a bitmap before scaling on the canvas for comparison. // The coordinates of the position on the screen are 0 and 0 canvas. drawbitmap (BMP, 0, 0, null); // defines the matrix object matrix = new matrix (); // scales the original matrix. postscale (1f, 1f); // rotate 45 degrees to the left, and the parameter is regular rotation of matrix to the right. postrotate (-45); // BMP. getwidth () and 500 respectively indicate the width and height of the repainted bitmap. Bitmap dstbmp = bitmap. createbitmap (BMP, 0, 0, BMP. getwidth (), 500, matrix, true); // draw the rotated bitmap on the canvas // place it in the position where the coordinates are 0,200 canvas. drawbitmap (dstbmp, 0,200, null );}}
Source code download: Click the open link