Android Image Zoom

Source: Internet
Author: User

Import Android.content.context;import Android.graphics.matrix;import Android.graphics.rect;import Android.graphics.rectf;import Android.graphics.drawable.drawable;import Android.util.attributeset;import Android.view.gesturedetector;import Android.view.motionevent;import Android.view.scalegesturedetector;import Android.view.scalegesturedetector.onscalegesturelistener;import Android.view.view;import Android.view.viewconfiguration;import Android.view.viewtreeobserver.ongloballayoutlistener;import Android.widget.imageview;import android.view.view.ontouchlistener;/** * Zoom class * * * * * * * Matrix's set,pre and post differences * * The pre indicates that inserting a method post in the team header means inserting a method set at the end of the queue to empty the current queues and always in the middle of the queue. * * Once set is executed: The Pre method is always inserted at the front of the queue at the front of the set, and the Post method is always inserted at the end of the queue at the back of the set * * * * * * Scalegesturedetector class and multi-touch related * *///interface for capturing picture loading complete public class Myimageview extends ImageView implements Ongloballayoutlistener,onscalegesturelistener, Ontouchlistener { -------------------------------------------//Zoom//------------------------------------------private Boolean isonce = true;public static final float basable_scale_rate = 1.0f;//The scaling value at initialization, and also the minimum scaling value PR Ivate float minitscale;//Double-click to zoom to the value of the private float mmidscale;//magnify the limit of the private float mmaxscale;//Bureau for the private Matrix Mmatrix; Scale for user multi-touch zoom private Scalegesturedetector mscalegesturedetector;//------------------------------------------- Freedom to move//------------------------------------------//Record the number of previous multi-touch private int mlastpointercount;private float MLASTX , mlasty;//is used for the center point of the reserved private int mtouchslop;//Whether the private Boolean iscandrag;//can be moved to determine the private Boolean ischeckleftandright; Private Boolean ischecktopandbottom;//-------------------------------------------//double-click to enlarge and shrink/-------------------- ----------------------Private Gesturedetector mgesturedetector;private boolean isautoscale;//--------------------- ----------------------//Construction class//------------------------------------------public Myimageview (context context, AttributeSet attrs, int defstyleattr) {Super (context, Attrs, DefstyleaTTR); Mmatrix = new Matrix (); Setscaletype (Scaletype.matrix); mscalegesturedetector = new Scalegesturedetector (context, This), Setontouchlistener (this); Mtouchslop = Viewconfiguration.get (context). Getscaledtouchslop (); mgesturedetector = new Gesturedetector (context,new Gesturedetector.simpleongesturelistener () {@Overridepublic Boolean Ondoubletap ( Motionevent e) {if (Isautoscale) {return true;} float x = E.getx (); Float y = e.gety (); if (Getscale () < Mmidscale) {//mmatrix.postscale (Mmidscale/getscale (), mmidscal e///Getscale (), x, y),//setimagematrix (Mmatrix);p ostdelayed (New autoscalerunnable (Mmidscale, x, y), +); Isautoscale = true;} else {//mmatrix.postscale (Minitscale/getscale (),//minitscale/getscale (), x, y);//setimagematrix (MMatrix); Postdelayed (New Autoscalerunnable (Minitscale, x, y), +); Isautoscale = true;} return true;});} Public Myimageview (context context, AttributeSet Attrs) {This (context, attrs, 0);} Public Myimageview (Context context) {This (context, null);} Picture loading to Window completion @overrideprotected void Onattachedtowindow () {Super.onattachedtowindow ();//view has implemented this interface, so use Thisgetviewtreeobserver (). Addongloballayoutlistener (this);} @SuppressWarnings ("deprecation") @Override//Picture disappears from window protected void Ondetachedfromwindow () { Super.ondetachedfromwindow ();//compatible with 161 under version Getviewtreeobserver (). Removeglobalonlayoutlistener (this);} Get ImageView load completed picture @overridepublic void Ongloballayout () {if (isonce) {//Get control's wide height int width = getwidth (); int height = get Height ();//or picture and width height drawable d = getdrawable (); if (d = = null) return;int DW = D.getintrinsicwidth (); int DH = d.getintrinsic Height ();//scaling float scale = 1.0f;//width exceeds screen if (DW > width && dh < height) {scale = width * Basable_scale_rat E/DW;} High out of screen if (dh > Height && DW < width) {scale = height * BASABLE_SCALE_RATE/DH;} At the same time exceeding the screen or not exceeding should be adjusted to have one side is full margin if (DW < width && dh < height) | | (DW > Width && dh > height)) {scale = math.min (width * basable_scale_rate/dw, height* basable_scale_rate /DH);} Scale scaling at initialization Minitscale = Scale;mmaxscale = 4 * Minitscale;mmidscale = 2 * minitscale;/** * * Move picture to center of control *///height int to move DX = width/2-Dw/2;int dy = HEIGHT/2-dh/2;//uses the matrix, moves the image and other operations mmatrix.posttranslate (dx, dy); Mmatrix.postscale (MI Nitscale, Minitscale, WIDTH/2, HEIGHT/2); Setimagematrix (Mmatrix); isonce = false;} else {}}/** * Gets the current zoom scale * * This method is not well understood * */public float Getscale () {float values[] = new Float[9];mmatrix.getvalues (values); return values[matrix.mscale_x];} Zoom In @overridepublic boolean Onscale (Scalegesturedetector detector) {//Scalefactor is the value of the scale float scalefactor = Detector.getscalefactor (); float scale = Getscale (); if (getdrawable () = = null) {return true;} Control of the zoom range if (Scale < Mmaxscale && scalefactor > 1.0f) | | (Scale > Minitscale && scalefactor < 1.0f)) {//Shrink to special small if (scale * Scalefactor < Minitscale) {scalefactor = Minitscale/scale;} Put in a particularly large if (scale * scalefactor > Mmaxscale) {size = Mmaxscale/scale;} Zoom MMATRIX.POstscale (Scalefactor, Scalefactor, Detector.getfocusx (), detector.getfocusy ());//Prevent white edges and center checkborderandcenter () ; Setimagematrix (Mmatrix);} return false;} This should be set to True@overridepublic Boolean Onscalebegin (Scalegesturedetector detector) {return true;} @Overridepublic void Onscaleend (Scalegesturedetector detector) {}//takes advantage of touch events, Mscalegesturedetector obtains multiple focus coordinates, For multi-Touch @overridepublic boolean OnTouch (View V, motionevent event) {if (Mgesturedetector.ontouchevent (event)) return True;mscalegesturedetector.ontouchevent (event);//x, Y is the location of the center point float x = 0, y = 0;//multi-touch number int pointercount = Event.getpoin Tercount (); for (int i = 0; i < Pointercount; i++) {x + = Event.getx (i); y + = event.gety (i); iscandrag = false;} X/= pointercount;y/= pointercount;//Touch Point number change if (mlastpointercount! = pointercount) {mlastx = X;mlasty = Y;mLastPointer Count = Pointercount;} Switch (event.getaction ()) {case MotionEvent.ACTION_MOVE:float dx = x-mlastx;float dy = y-mlasty;if (!iscandrag) {IsCa Ndrag = ismoveaction (dx, dy);} If (Iscandrag) {RECTF RECTF = GETMATRIXRECTF (); if (getdrawable () = null) {ischeckleftandright = Ischecktopandbottom = true;//width is less than the width of the control, not Allow move if (Rectf.width () < GetWidth ()) {dx = 0;ischeckleftandright = false;} Height less than control height, not allowed to move if (Rectf.height () < GetHeight ()) {dy = 0;ischecktopandbottom = false;} Mmatrix.posttranslate (dx, dy); Checkborderwhentranslate (); Setimagematrix (Mmatrix);}} MLASTX = X;mlasty = y;break;case MotionEvent.ACTION_UP:case MotionEvent.ACTION_CANCEL:mLastPointerCount = 0;break;} return true;} /** * Boundary Check when moving * */private void Checkborderwhentranslate () {RECTF RECTF = GETMATRIXRECTF (); float deltax = 0;float delt AY = 0;int width = getwidth (); int height = getheight (); if (rectf.top > 0 && ischecktopandbottom) {DeltaY =-rec Tf.top;} if (Rectf.bottom < height && ischecktopandbottom) {deltay = Height-rectf.bottom;} if (Rectf.left > 0 && ischeckleftandright) {deltax =-rectf.left;} if (Rectf.right < width && ischeckleftandright) {DeltAX = Width-rectf.right;} Mmatrix.posttranslate (DeltaX, DeltaY);} /** * is the move */private boolean ismoveaction (float dx, float dy) {return math.sqrt (DX * dx + dy * dy) > mtouchslop;} Rect is a rectangle, the boundary is float, so it is rectf//this method is used to rectf the object to get zoomed-out after the wide-height public rectf getmatrixrectf () {Matrix Matrix = Mmatrix; RECTF RECTF = new RECTF ();D rawable d = getdrawable (), if (d! = null) {rectf.set (0, 0, d.getintrinsicwidth (), D.getintrinsic Height ()); Matrix.maprect (RECTF);} return RECTF;} Control of the bounds at scale public void Checkborderandcenter () {RECTF RECTF = GETMATRIXRECTF ();//Beyond the length of the screen float deltax = 0;float DeltaY = 0;int width = getwidth (); int height = getheight ();//boundary detection to prevent white edge if (rectf.width () >= width) {//left blank if (Rectf.left &G T 0) {deltax =-rectf.left;} The blank if (Rectf.right < width) {deltax = Width-rectf.right;}} appears to the right if (rectf.height () >= height) {if (Rectf.top > 0) {deltay =-rectf.top;} if (Rectf.bottom < width) {deltay = Height-rectf.bottom;}} If the width height of the picture is less than the width of the control, let it be centered to show if (Rectf.width () < width){deltax = width/2f-rectf.right + rectf.width ()/2f;} if (Rectf.height () < height) {DeltaY = Height/2f-rectf.bottom + rectf.height ()/2f;} Mmatrix.posttranslate (DeltaX, DeltaY);} /** * Autoscale * */private class Autoscalerunnable implements Runnable {//Zoom target value Private float mtargetscale;//Zoom Center Point Priva Te float x;private float y;private final float bigger = 1.07f;private final float SMALL =0.93f;private float TMPSCALE;PUBL IC autoscalerunnable (float mtargetscale, float x, float y) {super (); This.mtargetscale = Mtargetscale;this.x = X;this.y = y ; if (Getscale () > Mtargetscale) {tmpscale = bigger;} if (Getscale () < Mtargetscale) {Tmpscale = SMALL;}} public void Run () {//scaled Mmatrix.postscale (Tmpscale, Tmpscale, x, y); Checkborderandcenter (); Setimagematrix (Mmatrix); float Currentscale = Getscale (); if (Tmpscale > 1.0f && currentscale < Mtargetscale) | | (Tmpscale < 1.0f && Currentscale > Mtargetscale)) {postdelayed (this, 16);} else {Float scale = MtargetscaLe/currentscale;mmatrix.postscale (scale, scale, x, y); Checkborderandcenter (); Setimagematrix (Mmatrix); Isautoscale = false;}}}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Image Zoom

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.