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