How to display part of an image in android

Source: Internet
Author: User

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

 

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.