Viewpager Picture preview Image Zoom out, move, switch (lesson three) serial

Source: Internet
Author: User

Lesson three (step three): Supports zooming in at any point centered on finger touch

The key part is the continuous boundary detection when zooming, to prevent the white edge when zoomed out:

/** * Border control range position control when zooming */private void Checkborderandcenterwhenscale () {//TODO auto-generated method STUBRECTF rect = ge TMATRIXRECTF (); float DeltaX = 0;float DeltaY = 0;float width = getwidth (); Float height = getheight ();//boundary detection when scaling, placed on white edge if ( Rect.width () >= width) {if (Rect.left > 0) {//processing left blank deltax =-rect.left;} if (Rect.right < width) {//processing the right blank deltax = (int) (width-rect.right);}} if (rect.height () >= height) {if (Rect.top > 0) {deltay =-rect.top;} if (Rect.bottom < height) {deltay = Height-rect.bottom;}} If the width or height is less than the width or high of the control, let it be centered if (Rect.width () < width) {deltax = Width/2f-rect.right + rect.width ()/2f;} if (Rect.height () < height) {DeltaY = Height/2f-rect.bottom + rect.height ()/2f;} Mscalematrix.posttranslate (DeltaX, DeltaY);}

All code:

Package Com.example.viewpagerimage;import Android.content.context;import Android.graphics.matrix;import Android.graphics.rectf;import Android.graphics.drawable.drawable;import Android.util.attributeset;import Android.view.motionevent;import Android.view.scalegesturedetector;import Android.view.scalegesturedetector.onscalegesturelistener;import Android.view.view;import Android.view.view.ontouchlistener;import Android.view.viewtreeobserver.ongloballayoutlistener;import android.widget.imageview;//implement listener Ongloballayoutlistener, monitor whether the picture is loaded complete public class Myimageview extends ImageView Implements Ongloballayoutlistener, Onscalegesturelistener,ontouchlistener{private Boolean monce;//determines whether to initialize private Float minitscale;//The value to scale when initializing private float mmidscale;//Double-click to zoom in to the value of private float mmaxscale;//magnified maximum private Scalegesturedetector mscalegesturedetector;//capture user multi-fingered zoom ratio private Matrix mscalematrix;public Myimageview (Context Context, AttributeSet attrs, int defstyleattr) {Super (context, attrs, defstyleattr);//initmscAlematrix = new Matrix (); Setscaletype (Scaletype.matrix); mscalegesturedetector = new Scalegesturedetector (context, this);///////When the picture is loaded, the picture may be large or small, it needs to adapt the picture to the screen size, automatically shrinks to the screen size when the picture is too large, and when the picture is zoomed to the screen size setontouchlistener. }public Myimageview (Context context, AttributeSet Attrs) {This (context, attrs,0);//TODO auto-generated constructor stub }public Myimageview (Context context) {this (context,null);//TODO auto-generated constructor stub} @Overrideprotected void Onattachedtowindow () {//TODO auto-generated method Stubsuper.onattachedtowindow ();//When view Called Getviewtreeobserver () when displayed on the screen. Addongloballayoutlistener (this);//Register interface} @SuppressWarnings ("deprecation") @ overrideprotected void Ondetachedfromwindow () {//TODO auto-generated method Stubsuper.ondetachedfromwindow ();// Call Getviewtreeobserver () when view is removed from the screen. Removeglobalonlayoutlistener (this);//Remove Interface}/** * Get ImageView loading completed picture */@ overridepublic void Ongloballayout () {//Global layout complete after call if (!monce) {//Get control width and height int width = getwidth (); int height = getheight () ;//Get our pictures as well as the wide and high DRAWable d = getdrawable (), if (d = = null) return;int DW = D.getintrinsicwidth (); int dh = d.getintrinsicheight (); float scale = 1 .0f;//Zoom Value//If the width of the picture is greater than the height of the control, but the width is less than the width of the control, narrow it down if (DW > width && dh < height) {scale = WIDTH*1.0F/DW;} else if (dh > Height && DW < width) {scale = HEIGHT*1.0F/DH;} else if (DW > Width && dh > height) {scale = Math.min (WIDTH*1.0F/DW, HEIGHT*1.0F/DH);} else if (DW < width && DH < height) {scale = Math.min (width *1.0f/dw, height*1.0f/dh);} /* Get scale scaling at initialization * */minitscale = Scale;mmaxscale = Minitscale * 4;mmidscale = Minitscale * 2;//moves the picture to the center of the current control int dx = GETW Idth ()/2-dw/2;int dy = getheight ()/2-dh/2;mscalematrix.posttranslate (dx, dy);//Pan Mscalematrix.postscale (MInitScale , MINITSCALE,WIDTH/2,HEIGHT/2);//zoom, the following two parameters are the center point of the scaling Setimagematrix (mscalematrix); monce = True;}} /** * Gets the zoom value of the current picture * @return */public float Getscale () {float[] values = new float[9];mscalematrix.getvalues (values); return V ALUES[MATRIX.MSCALE_X];} Range of scaling, Initscale mAxscale@overridepublic Boolean Onscale (Scalegesturedetector detector) {//TODO auto-generated method Stubfloat scale = ge TScale (); Float scalefactor = Detector.getscalefactor ();//Gets the scaled value if (getdrawable () = = null) {return true;} Control of the zoom range if (Scale < Mmaxscale && scalefactor > 1.0f) | | (Scale > Minitscale && scalefactor < 1.0f))  {if (scale * Scalefactor < Minitscale) {scalefactor = minitscale/scale;//when finger scaling is less than the minimum, the default is to show the smallest proportion}if (scales * scalefactor > Mmaxscale) {//When the finger is zoomed to the maximum, the largest scale is displayed by default = Mmaxscale/scale;} Zoom, zoom Center is the finger Touch place Mscalematrix.postscale (Scalefactor, Scalefactor, Detector.getfocusx (), detector.getfocusy ()); Checkborderandcenterwhenscale (); Setimagematrix (Mscalematrix);} Return true;//set to complete returns true to ensure the event can be}/** * Get the picture zoomed in and out after the width and height as well as l R T b * @return */private RECTF GETMATRIXRECTF () {Matrix Matrix = Mscalematrix; RECTF RECF = new RECTF ();D rawable d = getdrawable (), if (d! = null) {recf.set (0, 0, d.getintrinsicwidth (), D.getintrinsicheig HT ()); Matrix.maprect (RECF);} Return RECF;} /** * Border control range position control when zooming */private void Checkborderandcenterwhenscale () {//TODO auto-generated method STUBRECTF rect = ge TMATRIXRECTF (); float DeltaX = 0;float DeltaY = 0;float width = getwidth (); Float height = getheight ();//boundary detection when scaling, placed on white edge if ( Rect.width () >= width) {if (Rect.left > 0) {//processing left blank deltax =-rect.left;} if (Rect.right < width) {//processing the right blank deltax = (int) (width-rect.right);}} if (rect.height () >= height) {if (Rect.top > 0) {deltay =-rect.top;} if (Rect.bottom < height) {deltay = Height-rect.bottom;}} If the width or height is less than the width or high of the control, let it be centered if (Rect.width () < width) {deltax = Width/2f-rect.right + rect.width ()/2f;} if (Rect.height () < height) {DeltaY = Height/2f-rect.bottom + rect.height ()/2f;} Mscalematrix.posttranslate (DeltaX, DeltaY);} @Overridepublic Boolean onscalebegin (Scalegesturedetector detector) {//TODO auto-generated method Stubreturn true;// Must return true} @Overridepublic void Onscaleend (Scalegesturedetector detector) {//TODO auto-generated method stub}@ Overridepublic Boolean OnTouch (View V, motionevent event) {//TODO auto-generated Method Stubmscalegesturedetector.ontouchevent (event) ;//pass event to Mscalegesturedetector handle return true;//must return True}}

: On the left is the original, the right is the upper right corner of the image



Viewpager Picture preview Image Zoom out, move, switch (lesson three) serial

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.