/** * Picture browsing, zooming, dragging, centering automatically */ public class touch extends activity implements Ontouchlistener { Matrix matrix = new Matrix (); Matrix Savedmatrix = new Matrix (); Displaymetrics DM; ImageView Imgview; Bitmap Bitmap; Float minscaler;//Minimum zoom ratio Static final Float Max_scale = 4f;//maximum Scaling static final int NONE = 0;//initial state static final int DRAG = 1;//drag static final int ZOOM = 2;//Scaling int mode = NONE; PointF prev = new PointF (); PointF mid = new PointF (); Float dist = 1f; @Override public void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.scale); Imgview = (ImageView) Findviewbyid (R.ID.IMAG);//Get control Bitmap = Bitmapfactory.decoderesource (Getresources (), This.getintent () . Getextras (). GetInt ("IMG");//Get Picture resources Imgview.setimagebitmap (bitmap)//Fill control Imgview.setontouchlistener (this);/Set Touch screen monitor DM = new Displaymetrics (); Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM);//Get Resolution Minzoom (); Center (); Imgview.setimagematrix (matrix); } /** * Touch screen Monitor */ public boolean Ontouch (View V, motionevent event) { Switch (event.getaction () & Motionevent.action_mask) { The main point is pressed Case Motionevent.action_down: Savedmatrix.set (matrix); Prev.set (Event.getx (), event.gety ()); mode = DRAG; Break Click on the secondary point Case Motionevent.action_pointer_down: Dist = spacing (event); If the two-point distance is greater than 10, then the multiple point pattern is determined. if (Spacing (event) > 10f) { Savedmatrix.set (matrix); Midpoint (Mid, event); mode = ZOOM; } Break Case MOTIONEVENT.ACTION_UP: Case MOTIONEVENT.ACTION_POINTER_UP: mode = NONE; Break Case Motionevent.action_move: if (mode = = DRAG) { Matrix.set (Savedmatrix); Matrix.posttranslate (Event.getx ()-prev.x, Event.gety () -PREV.Y); else if (mode = = ZOOM) { float newdist = spacing (event); if (Newdist > 10f) { Matrix.set (Savedmatrix); float TScale = newdist/dist; Matrix.postscale (TScale, TScale, mid.x, MID.Y); } } Break } Imgview.setimagematrix (matrix); Checkview (); return true; } /** * Limit maximum minimum scaling, auto Center */ private void Checkview () { float p[] = new FLOAT[9]; Matrix.getvalues (P); if (mode = = ZOOM) { if (P[0] < Minscaler) { Matrix.setscale (Minscaler, Minscaler); } if (P[0] > Max_scale) { Matrix.set (Savedmatrix); } } Center (); } /** * Minimum zoom ratio, maximum is 100% */ private void Minzoom () { Minscaler = Math.min ( (float) dm.widthpixels/(float) bitmap.getwidth (), (float) dm.heightpixels/(float) bitmap.getheight ()); if (Minscaler < 1.0) { Matrix.postscale (Minscaler, Minscaler); } } private void Center () { Center (true, true); } /** * Horizontal, Vertical Center */ protected void Center (Boolean horizontal, Boolean vertical) { Matrix M = new Matrix (); M.set (matrix); RECTF rect = new RECTF (0, 0, bitmap.getwidth (), Bitmap.getheight ()); M.maprect (rect); float height = rect.height (); Float width = rect.width (); Float deltax = 0, DeltaY = 0; if (vertical) { If the picture is smaller than the screen size, it is centered. Larger than screen, left above left blank to move up, drop down int screenheight = Dm.heightpixels; if (height < screenheight) { DeltaY = (screenheight-height)/2-rect.top; else if (Rect.top > 0) { DeltaY =-rect.top; else if (Rect.bottom < screenheight) { DeltaY = Imgview.getheight ()-rect.bottom; } } if (horizontal) { int screenwidth = Dm.widthpixels; if (Width < screenwidth) { DeltaX = (screenwidth-width)/2-rect.left; else if (Rect.left > 0) { DeltaX =-rect.left; else if (Rect.right < screenwidth) { DeltaX = Screenwidth-rect.right; } } Matrix.posttranslate (DeltaX, DeltaY); } /** * Two-point distance */ Private float spacing (motionevent event) { float x = event.getx (0)-event.getx (1); Float y = event.gety (0)-event.gety (1); return FLOATMATH.SQRT (x * x + y * y); } /** * Midpoint of two points */ private void Midpoint (PointF point, motionevent event) { float x = event.getx (0) + event.getx (1); Float y = event.gety (0) + event.gety (1); Point.set (X/2, Y/2); } } |