Loading an image in the android Application and displaying it is very easy. How can we display a small part of an image, one way is to ps the image, save the part to be displayed as an image separately, load it in the program, and display it. However, this will increase the program's image volume. For a complete graph, it is very easy to use a program to cut the part you want.
The following program loads an image, changes the image to fill the entire screen of the mobile phone, and then displays the Part 100*100 in the middle of the screen.
ShowPoritionPictureActivity code:
[Java] <span style = "font-size: 16px;"> package com. iwin. zzs;
Import android. app. Activity;
Import android. graphics. Bitmap;
Import android. graphics. BitmapFactory;
Import android. graphics. Matrix;
Import android. OS. Bundle;
Import android. util. DisplayMetrics;
Import android. view. WindowManager;
Public class ShowPoritionPictureActivity extends Activity {
/** Called when the activity is first created .*/
Bitmap picRes;
Bitmap showPic;
// Obtain the width and height of the original image
Int picWidth;
Int picHeight;
Private PoritionView poritonView = null;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
// Do not display the status bar
This. getWindow (). setFlags (WindowManager. LayoutParams. FLAG_FULLSCREEN, WindowManager. LayoutParams. FLAG_FULLSCREEN );
DisplayMetrics dm = new DisplayMetrics ();
This. getWindowManager (). getDefaultDisplay (). getMetrics (dm );
// Obtain the screen length and width.
Int screenWidth = dm. widthPixels; // horizontal resolution
Int screenHeight = dm. heightPixels; // vertical resolution
PicRes = BitmapFactory. decodeResource (this. getResources (), R. drawable. girl );
// Obtain the length and width of the image.
PicWidth = picRes. getWidth ();
PicHeight = picRes. getHeight ();
// Calculate the zooming rate. The new size excludes the original size.
Float scaleWidth = (float) screenWidth)/picWidth;
Float scaleHeight = (float) screenHeight)/picHeight;
// Create a matrix object for image operations
Matrix matrix = new Matrix ();
// Scaling the image
Matrix. postScale (scaleWidth, scaleHeight );
// The new image is the image filled with the entire screen after the original image is transformed.
Bitmap picNewRes = Bitmap. createBitmap (picRes, 0, 0, picWidth, picHeight, matrix, true );
// Bitmap = Bitmap. createBitmap (400,480, Bitmap. Config. ARGB_8888 );
// Canvas = new Canvas ();
// Canvas. setBitmap (bitmap );
ShowPic = Bitmap. createBitmap (picNewRes, screenWidth/2-50, screenHeight/2-50,100,100 );
PoritonView = new PoritionView (this );
PoritonView. setBitmapShow (showPic, screenWidth/2-50, screenHeight/2-50 );
SetContentView (poritonView );
}
</Span>
<Span style = "font-size: 16px;"> package com. iwin. zzs;
Import android. app. Activity;
Import android. graphics. Bitmap;
Import android. graphics. BitmapFactory;
Import android. graphics. Matrix;
Import android. OS. Bundle;
Import android. util. DisplayMetrics;
Import android. view. WindowManager;
Public class ShowPoritionPictureActivity extends Activity {
/** Called when the activity is first created .*/
Bitmap picRes;
Bitmap showPic;
// Obtain the width and height of the original image
Int picWidth;
Int picHeight;
Private PoritionView poritonView = null;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
// Do not display the status bar
This. getWindow (). setFlags (WindowManager. LayoutParams. FLAG_FULLSCREEN, WindowManager. LayoutParams. FLAG_FULLSCREEN );
DisplayMetrics dm = new DisplayMetrics ();
This. getWindowManager (). getDefaultDisplay (). getMetrics (dm );
// Obtain the screen length and width.
Int screenWidth = dm. widthPixels; // horizontal resolution
Int screenHeight = dm. heightPixels; // vertical resolution
PicRes = BitmapFactory. decodeResource (this. getResources (), R. drawable. girl );
// Obtain the length and width of the image www.2cto.com
PicWidth = picRes. getWidth ();
PicHeight = picRes. getHeight ();
// Calculate the zooming rate. The new size excludes the original size.
Float scaleWidth = (float) screenWidth)/picWidth;
Float scaleHeight = (float) screenHeight)/picHeight;
// Create a matrix object for image operations
Matrix matrix = new Matrix ();
// Scaling the image
Matrix. postScale (scaleWidth, scaleHeight );
// The new image is the image filled with the entire screen after the original image is transformed.
Bitmap picNewRes = Bitmap. createBitmap (picRes, 0, 0, picWidth, picHeight, matrix, true );
// Bitmap = Bitmap. createBitmap (400,480, Bitmap. Config. ARGB_8888 );
// Canvas = new Canvas ();
// Canvas. setBitmap (bitmap );
ShowPic = Bitmap. createBitmap (picNewRes, screenWidth/2-50, screenHeight/2-50,100,100 );
PoritonView = new PoritionView (this );
PoritonView. setBitmapShow (showPic, screenWidth/2-50, screenHeight/2-50 );
SetContentView (poritonView );
}
</Span>
Code for creating a PoritionView class:
[Java] <span style = "font-size: 16px;"> package com. iwin. zzs;
Import android. content. Context;
Import android. graphics. Bitmap;
Import android. graphics. Canvas;
Import android. view. View;
Public class PoritionView extends View {
Private Bitmap showPic = null;
Private int startX = 0;
Private int startY = 0;
Public PoritionView (Context context ){
Super (context );
// TODO Auto-generated constructor stub
}
@ Override
Protected void onDraw (Canvas canvas ){
// TODO Auto-generated method stub
Super. onDraw (canvas );
Canvas. drawBitmap (showPic, startX, startY, null );
}
Public void setBitmapShow (Bitmap B, int x, int y)
{
ShowPic = B;
StartX = x;
StartY = y;
}
}
</Span>
<Span style = "font-size: 16px;"> package com. iwin. zzs;
Import android. content. Context;
Import android. graphics. Bitmap;
Import android. graphics. Canvas;
Import android. view. View;
Public class PoritionView extends View {
Private Bitmap showPic = null;
Private int startX = 0;
Private int startY = 0;
Public PoritionView (Context context ){
Super (context );
// TODO Auto-generated constructor stub
}
@ Override
Protected void onDraw (Canvas canvas ){
// TODO Auto-generated method stub
Super. onDraw (canvas );
Canvas. drawBitmap (showPic, startX, startY, null );
}
Public void setBitmapShow (Bitmap B, int x, int y)
{
ShowPic = B;
StartX = x;
StartY = y;
}
}
</Span>
In the res/drawableproject, run gir.png to display only the part of the image 100*100 in the middle of the image.
From Peking University-Google Android lab