Two methods of image rotation for Android mobile phone Development

Source: Internet
Author: User

Image Rotation for Android mobile development
1. Create a New bitmap after rotation from the original bitmap based on the rotation matrix each time. However, the disadvantage is to use a new bitmap for the ship.
// Createa matrix for the manipulation
Matrix matrix = new matrix ();
// Rotate the bitmap
Matrix. postrotate (45 );

// Recreate the new bitmap
Bitmap resizedbitmap = bitmap. createbitmap (bitmaporg, 0, 0,
Width, height, matrix, true );

// Make a drawable from Bitmap to allow to set the bitmap
// To the imageview, imagebutton or what ever
Bitmapdrawable BMI = new bitmapdrawable (resizedbitmap );

2. Convert the rotating image into a rotating canvas. High efficiency. Official Android Demo: lunarlander rotates the canvas to rotate the image, but the effect is somewhat distorted. (When rotating an image, the center is usually the center of the image .)
(The rotation direction is clockwise. If the angle is negative, it is clockwise)

// Note that the canvas status is saved and restored.
Canvas. Save ();
// The parameters are as follows: rotation angle, image x center, and image y center.
Canvas. Rotate (angle, getcenterx (), getcentery ());
Spriteimage. setbounds (bounds );
Spriteimage. Draw (canvas );
Canvas. Restore ();

Objectives:

This article describes how to use matrix in Android to scale and rotate images. Through this article, you will learn how to operate images through matrix.

 

 

Sample Code:

Directly go to the code. I have attached a detailed explanation to the Code. The Code is as follows:

Package com. eoeandroid. 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. viewgroup. layoutparams;
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 the width and height of the image.
Int width = bitmaporg. getwidth ();

Int Height = bitmaporg. getheight ();

// Define the 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 of imageview to 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 );
}

}

 

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.