Rotating bitmap Bitmap bitmap of Android learning notes

Source: Internet
Author: User

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

 

 

 

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.