This article describes the method of Android programming to only display part of the picture. Share it for your reference, as follows:
It is very easy to load an image in an Android application and then display it. How can you display a small part of an image? One way is to ps the image, save the part you want to display separately into a picture, then load it in the program and display it. But this will increase the number of pictures of the program. It is also very easy to use the program to cut the part you want for a complete picture.
The following program is to load a picture, then change the picture to fill the entire screen of the phone, and then display the 100*100 part of the picture in the middle of the screen.
ShowPoritionPictureActivity Code:
package ; import ; import ; import ; import ; import ; import ; import ; public class ShowPoritionPictureActivity extends Activity { /** Called when the activity is first created. */ Bitmap picRes; Bitmap showPic; //Get the width and height of the original image int picWidth; int picHeight; private PoritionView poritonView = null; @Override public void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(); // The status bar is not displayed ().setFlags(.FLAG_FULLSCREEN,.FLAG_FULLSCREEN); DisplayMetrics dm = new DisplayMetrics(); ().getDefaultDisplay().getMetrics(dm); // Get the length and width of the screen int screenWidth = ; //Horizontal resolution int screenHeight = ; //Vertical resolution picRes = ((), ); // Get the length and width of the picture picWidth = (); picHeight = (); // Calculate the scaling rate, and the new size is separated from the original size float scaleWidth = ((float) screenWidth ) / picWidth; float scaleHeight = ((float) screenHeight ) / picHeight; // Create matrix object for operation pictures Matrix matrix = new Matrix(); // Zoom the image action (scaleWidth, scaleHeight); // The newly obtained picture is the picture where the original picture has been changed and filled to the entire screen Bitmap picNewRes = (picRes, 0, 0,picWidth, picHeight, matrix, true); // bitmap = (400, 480, .ARGB_8888); // canvas=new Canvas(); // (bitmap); showPic = (picNewRes, screenWidth/2-50, screenHeight/2-50, 100, 100); poritonView = new PoritionView(this); (showPic, screenWidth/2-50, screenHeight/2-50); setContentView(poritonView); }
Create a new PoritView class code:
package ; import ; import ; import ; import ; 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 (canvas); (showPic, startX, startY, null); } public void setBitmapShow(Bitmap b, int x, int y) { showPic = b; startX = x; startY = y; } }
Add pictures in the project res/drawable. The effect of running the program is to only display the part of the picture with the middle 100*100.
For more information about Android related content, please check out the topic of this site:Summary of Android graphics and image processing skills》、《Android development introduction and advanced tutorial》、《Android debugging skills and solutions to common problems》、《Android multimedia operation skills summary (audio, video, recording, etc.)》、《Summary of the usage of basic Android components》、《Android View View Tips Summary》、《Android layout layout tips summary"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.