Android image scaling and rotation implementation

Source: Internet
Author: User

This document uses Matrix to scale and rotate images in Android. The sample code is as follows: Copy codeThe Code is as follows: package com. android. matrix;
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;
/**
* Android scales and rotates images.
* @ Author Administrator
*
*/
Public class MatixActivity extends Activity {
Public void onCreate (Bundle icicle ){

Super. onCreate (icicle );

SetTitle ("Android: Resize and rotate images. ");
LinearLayout linLayout = new LinearLayout (this );

// Load the image to be operated. Here is an image.
Bitmap bitmapOrg = BitmapFactory. decodeResource (getResources (), R. drawable. r );

// 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 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 );
}
}

In the preceding example, the image is scaled statically. In the following example, the image can be dynamically zoomed in or out through the mouse pulley and arrow keys.
The program structure is as follows:

Code in the Zoom. java file:
Copy codeThe Code is as follows: package com. android. zooming;
Import android. view. View;
Import android. content. Context;
Import android. graphics. Canvas;
Import android. graphics. drawable. Drawable;
Import android. view. KeyEvent;
Public class Zoom extends View {
Private Drawable image;
Private int zoomControler = 20;

Public Zoom (Context context ){
Super (context );
Image = context. getResources (). getDrawable (R. drawable. x );
SetFocusable (true );
}

@ Override
Protected void onDraw (Canvas canvas ){
// TODO Auto-generated method stub
Super. onDraw (canvas );
// Control the image width and height
Image. setBounds (getWidth ()/2)-zoomControler, (getHeight ()/2)-zoomControler, (getWidth ()/2) + zoomControler, (getHeight ()/2) + zoomControler );
Image. draw (canvas );
}

@ Override
Public boolean onKeyDown (int keyCode, KeyEvent event ){

If (keyCode = KeyEvent. KEYCODE_DPAD_UP) // enlarge
ZoomControler + = 10;

If (keyCode = KeyEvent. KEYCODE_DPAD_DOWN) // zoom out
ZoomControler-= 10;

If (zoomControler <10)
ZoomControler = 10;

Invalidate ();
Return true;
}
}

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.