Android image rotation

Source: Internet
Author: User

In Android, Matrix is used for image rotation. It contains a 3*3 Matrix, which is used for image transformation matching. Matrix is called a Matrix in Chinese. It is introduced in advanced mathematics. In terms of image processing, it is mainly used for operations such as plane scaling, translation, and rotation. Matrix has no body. It must be initialized and then implemented through the reset method and set method.
First, we will introduce matrix operations. Addition and subtraction are not required. It is too simple to add the corresponding bits. Image processing mainly uses multiplication. The following is a multiplication formula:

In Android, Matrix is a 3*3 Matrix consisting of 9 float values. For example.

 
Without professional tools, the painting is ugly. The sinX and cosX above indicate the cos and sin values of the rotation angle. Note that the rotation angle is calculated clockwise. TranslateX and translateY indicate the translation quantity of x and y. Scale is the scale ratio, 1 is the same, 2 is the scale 1/2, this way.
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. Set is to directly set the value of the Matrix. Each time a set is set, the array of the entire Matrix is changed.
Now we use setRotate to set the rotation angle, use creatBitmap to create a Bitmap object that has been rotated and processed, and then draw the Bitmap to the screen, so we can implement the rotation operation.
The following uses an example to describe the use of Matix, the rotation method, and the running effect.
 

Package cn.edu. pku;
 
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. KeyEvent;
 
Public class PictureRotateActivity extends Activity {
/** Called when the activity is first created .*/

Private GameRotateView1 gameview = null;

@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
Gameview = new GameRotateView1 (this );
SetContentView (gameview );
}
@ Override
Public boolean onKeyDown (int keyCode, KeyEvent event ){
// TODO Auto-generated method stub
If (gameview = null)
{
Return false;
}
If (keyCode = KeyEvent. KEYCODE_BACK)
{
This. finish ();
Return true;
}
Return gameview. onKeyDown (keyCode, event );
}
@ Override
Public boolean onKeyUp (int keyCode, KeyEvent event ){
// TODO Auto-generated method stub
Super. onKeyUp (keyCode, event );
Return true;
}
}
Package cn.edu. pku;

Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. KeyEvent;

Public class PictureRotateActivity extends Activity {
/** Called when the activity is first created .*/
 
Private GameRotateView1 gameview = null;
 
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
Gameview = new GameRotateView1 (this );
SetContentView (gameview );
}
@ Override
Public boolean onKeyDown (int keyCode, KeyEvent event ){
// TODO Auto-generated method stub
If (gameview = null)
{
Return false;
}
If (keyCode = KeyEvent. KEYCODE_BACK)
{
This. finish ();
Return true;
}
Return gameview. onKeyDown (keyCode, event );
}
@ Override
Public boolean onKeyUp (int keyCode, KeyEvent event ){
// TODO Auto-generated method stub
Super. onKeyUp (keyCode, event );
Return true;
}
}
 

 

The specific image rotation code is as follows:

 


[Java] package cn.edu. pku;
 
Import android. content. Context;
Import android. graphics. Bitmap;
Import android. graphics. Canvas;
Import android. graphics. Matrix;
Import android. graphics. drawable. BitmapDrawable;
Import android. view. KeyEvent;
Import android. view. MotionEvent;
Import android. view. View;
 
Public class GameRotateView1 extends View implements Runnable {
 
Bitmap bitmap = null;
Int bitmapWidth = 0;
Int bitmapHeight = 0;

Float angle = 0.0f;

Matrix matrix = new Matrix ();

Public GameRotateView1 (Context context ){
Super (context );
// TODO Auto-generated constructor stub

SetFocusableInTouchMode (true); // you can capture Keyboard Events.
// Obtain image resources
Bitmap = (BitmapDrawable) getResources (). getDrawable (R. drawable. cute). getBitmap ();
BitmapWidth = bitmap. getWidth ();
BitmapHeight = bitmap. getHeight ();
New Thread (this). start ();
}
 
Public void run (){
// TODO Auto-generated method stub
While (! Thread. currentThread (). isInterrupted ()){
Try {
Thread. sleep (100 );
} Catch (InterruptedException e ){
// TODO: handle exception
Thread. currentThread (). interrupt ();
}
PostInvalidate (); // you can directly update the interface in the thread.
}
}
 
@ Override
Protected void onDraw (Canvas canvas ){
// TODO Auto-generated method stub
Super. onDraw (canvas );

Matrix. reset ();
Matrix. setRotate (angle); // sets the rotation.

// Create a new Bitmap Based on the rotation of matrix
Bitmap bitmapcute = Bitmap. createBitmap (bitmap, 0, 0, bitmapWidth, bitmapHeight, matrix, true );

// Draw the rotated image www.2cto.com
GameRotateView1.DrawImage (canvas, bitmapcute, (320-bitmapWidth)/2, 10 );
Bitmapcute = null;
}
 
@ Override
Public boolean onKeyDown (int keyCode, KeyEvent event ){
// TODO Auto-generated method stub
If (keyCode = KeyEvent. KEYCODE_DPAD_LEFT ){
Angle --;
} Else if (keyCode = KeyEvent. KEYCODE_DPAD_RIGHT ){
Angle ++;
}
Return true;
}

@ Override
Public boolean onTouchEvent (MotionEvent event ){
// TODO Auto-generated method stub
Return true;
}
 

@ Override
Public boolean onKeyUp (int keyCode, KeyEvent event ){
// TODO Auto-generated method stub
Return false;
}
 


@ Override
Public boolean onKeyMultiple (int keyCode, int repeatCount, KeyEvent event ){
// TODO Auto-generated method stub
Return true;
}
 
/**
* Draw a Bitmap
* Canvas
* Bitmap image
* X coordinates on the screen
* Y coordinate on the screen
*/
Public static void DrawImage (Canvas canvas, Bitmap _ bitmap, int x, int y)
{
/* Draw an image */
Canvas. drawBitmap (_ bitmap, x, y, null );
}
Package cn.edu. pku;

Import android. content. Context;
Import android. graphics. Bitmap;
Import android. graphics. Canvas;
Import android. graphics. Matrix;
Import android. graphics. drawable. BitmapDrawable;
Import android. view. KeyEvent;
Import android. view. MotionEvent;
Import android. view. View;

Public class GameRotateView1 extends View implements Runnable {

Bitmap bitmap = null;
Int bitmapWidth = 0;
Int bitmapHeight = 0;
 
Float angle = 0.0f;
 
Matrix matrix = new Matrix ();
 
Public GameRotateView1 (Context context ){
Super (context );
// TODO Auto-generated constructor stub

SetFocusableInTouchMode (true); // you can capture Keyboard Events.
// Obtain image resources
Bitmap = (BitmapDrawable) getResources (). getDrawable (R. drawable. cute). getBitmap ();
BitmapWidth = bitmap. getWidth ();
BitmapHeight = bitmap. getHeight ();
New Thread (this). start ();
}

Public void run (){
// TODO Auto-generated method stub
While (! Thread. currentThread (). isInterrupted ()){
Try {
Thread. sleep (100 );
} Catch (InterruptedException e ){
// TODO: handle exception
Thread. currentThread (). interrupt ();
}
PostInvalidate (); // you can directly update the interface in the thread.
}
}

@ Override
Protected void onDraw (Canvas canvas ){
// TODO Auto-generated method stub
Super. onDraw (canvas );

Matrix. reset ();
Matrix. setRotate (angle); // sets the rotation.

// Create a new Bitmap Based on the rotation of matrix
Bitmap bitmapcute = Bitmap. createBitmap (bitmap, 0, 0, bitmapWidth, bitmapHeight, matrix, true );

// Draw the rotated image
GameRotateView1.DrawImage (canvas, bitmapcute, (320-bitmapWidth)/2, 10 );
Bitmapcute = null;
}

@ Override
Public boolean onKeyDown (int keyCode, KeyEvent event ){
// TODO Auto-generated method stub
If (keyCode = KeyEvent. KEYCODE_DPAD_LEFT ){
Angle --;
} Else if (keyCode = KeyEvent. KEYCODE_DPAD_RIGHT ){
Angle ++;
}
Return true;
}
 
@ Override
Public boolean onTouchEvent (MotionEvent event ){
// TODO Auto-generated method stub
Return true;
}

 
@ Override
Public boolean onKeyUp (int keyCode, KeyEvent event ){
// TODO Auto-generated method stub
Return false;
}

 
 
@ Override
Public boolean onKeyMultiple (int keyCode, int repeatCount, KeyEvent event ){
// TODO Auto-generated method stub
Return true;
}

/**
* Draw a Bitmap
* Canvas
* Bitmap image
* X coordinates on the screen
* Y coordinate on the screen
*/
Public static void DrawImage (Canvas canvas, Bitmap _ bitmap, int x, int y)
{
/* Draw an image */
Canvas. drawBitmap (_ bitmap, x, y, null );
}

 

Finally, we can use the left and right keys of the keyboard to select an image. Here we can achieve the right rotation of the image:

 



From Peking University-Google Android lab
 

Related Article

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.