Viewpager Picture preview Image Zoom out, move, toggle (Lesson two) serial

Source: Internet
Author: User

Step Two: Add the ability to support finger-touch scaling for your custom control: (finger touch magnification is supported)

Because it involves gesture touch events, you implement Onscalegesturelistener,ontouchlistener these two interfaces.

Declaring member variables: private Scalegesturedetector mscalegesturedetector;//captures the scale of the user's multi-fingered touch zoom

Initialize in constructor:

Mscalegesturedetector = new Scalegesturedetector (context, this);
Setontouchlistener (this);


To add a method:

/** * 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];}


Implement the methods in the interface:


Scaled interval, Initscale maxscale@overridepublic boolean Onscale (Scalegesturedetector detector) {//TODO auto-generated Method Stubfloat scale = Getscale (); Float scalefactor = Detector.getscalefactor ();//Get 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 Mscalematrix.postscale (Scalefactor, Scalefactor, GetWidth ()/2, GetHeight ()/2); Setimagematrix (MScaleMatrix);} Return true;//set complete returns true to ensure event is able to proceed} @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 to process return true;// Must return True}



The full code is as follows:

Package Com.example.viewpagerimage;import Android.content.context;import Android.graphics.matrix;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.viewtreeobserver.ongloballayoutlistener;import Android.widget.ImageView; Import android.view.view.ontouchlistener;//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;//When the value of the scale is initialized private float mmidscale;//double-click to enlarge the value of the private float mmaxscale;//the maximum value of 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); Setontouchlistener (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 in to the screen size. }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 picture as well as width and height 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, and it is scaled 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];} Scaled interval, Initscale maxscale@overridepublic boolean Onscale (Scalegesturedetector detector) {//TODO auto-generated method Stubfloat scale = Getscale (); float scalefactor = de Tector.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 Mscalematrix.postscale (Scalefactor, Scalefactor, GetWidth ()/2, GetHeight ()/2); Setimagematrix (MScaleMatrix);} Return true;//set complete returns true to ensure event is able to proceed} @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 process return true;//must return True}} 

The result of the current implementation: wherever the finger touches, it starts to scale with the center Point Center.

The following effect is achieved by centering the zoom at any point in the finger touch





Viewpager Picture preview Image Zoom out, move, toggle (Lesson two) 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.