How to display part of a picture in Android
Category: Android app development Series Tutorials Android app development tips 2012-04-25 23:42 1728 people read reviews (0) Collection Report
AndroidMatrixfloatconstructorclassnull
Zhang Zongji
Loading a picture in an Android app and then showing it is a very easy thing to do, how can you show a small part of a picture, one way is to put the picture PS, to the part that is displayed is saved as a single picture, then the program loads it and displays it. However, this will increase the picture volume of the program. It's also easy to use a program to cut the part you want for a complete picture.
The following implementation of a program, is loaded with a picture, and then transformed to fill the picture to the phone's entire screen, and then in the middle of the screen to display the middle of the picture of the 100*100 part.
Showporitionpictureactivity Code:
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;//get the width and height of the original picture int picWidth; int picheight; private poritionview poritonview = null; @Override public void oncreate (Bundle savedinstancestate) { super.oncreate ( Savedinstancestate); setcontentview (R.layOut.main); // does 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);// get the length and width of the screen int screenwidth = dm.widthpixels; //Horizontal Resolution int screenheight = dm.heightpixels; //Vertical Resolution picres = bitmapfactory.decoderesource (This.getresources (), r.drawable.girl); // get pictures of long and wide &nbsP; picwidth = picres.getwidth (); picheight = picres.getheight ( ); // Calculate the zoom rate, the new size in addition to the original size float scaleWidth = ((float) screenWidth ) / picWidth; float scaleHeight = ((float) screenHeight ) / picHeight; // Create a Matrix object for manipulating pictures matrix matrix = new matrix (); // Zoom picture Action matrix.postscale (ScaleWidth, scaleheight); // new picture is a picture of the original image that has been transformed to fill the entire screen 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); }
New Poritionview Class Code:
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;priv ate int starty = 0;public Poritionview (context context) {super (context);//TODO auto-generated constructor stub} @Override protected void OnDraw (canvas canvas) {//TODO auto-generated method Stubsuper.ondraw (canvas); Canvas.drawbitmap (showpic , StartX, starty, null);} public void Setbitmapshow (Bitmap b, int x, int y) {showpic = B;startx = X;starty = y;}}
In the project res/drawable add picture Gir.png, the effect of running the program is to display only the picture of the middle 100*100 that part of the picture.
How to display part of a picture in Android