Android realize ImageView picture double click Zoom in and Zoom out _android

Source: Internet
Author: User
Tags gety

This article introduces the Android realization ImageView picture Double click enlarge and reduce the related skill, share to everybody for everybody reference, the concrete content is as follows

public class Doublescaleimageview extends ImageView implements Ontouchlistener, Ongloballayoutlistener {private Boolea
  n Isfirst = false;
  Private float doublescale;//Double click the magnified value private Matrix Mscalematrix;
  Private float defaultscale;//Default zoom value private int mlastpintercount;//record the number of times a multi-touch is private float mlastx;
  private float mlasty;
  private int mtouchslop;
  Private Boolean Iscandrag;
  Private Boolean ischeckleft;
  Private Boolean ischecktop;
  Private Gesturedetector Mgesturedetector;
  Public Doublescaleimageview {This (context, NULL);
  Public Doublescaleimageview (context, AttributeSet attrs) {This (context, attrs, 0); @SuppressLint ("clickableviewaccessibility") public Doublescaleimageview (context, AttributeSet attrs, int def
    STYLEATTR) {Super (context, attrs, defstyleattr);
    Mscalematrix = new Matrix ();
    Setscaletype (Scaletype.matrix);
    Setontouchlistener (this); Getscaledtouchslop is a distance that indicates the movement of the hand when it slidesTo move the control before it is greater than this distance.
    If it is less than this distance, the mobile control Mtouchslop = Viewconfiguration.get (context) is not triggered. Getscaledtouchslop (); Mgesturedetector = new Gesturedetector (context, new Gesturedetector.simpleongesturelistener () {@Override Publ
        IC Boolean Ondoubletap (Motionevent e) {float x = E.getx ();
        Float y = e.gety ();  if (Getscale () < Doublescale) {Mscalematrix.postscale (Doublescale/getscale (), Doublescale/getscale (), X, y);//enlarge} else {Mscalematrix.postscale (Defaultscale/getscale (), Defaultscale/getscale (),
        x, y);//Shrink} setimagematrix (Mscalematrix);
      Return Super.ondoubletap (e);
  }
    });
    This method is invoked Super.onattachedtowindow () @Override protected void Onattachedtowindow () {//view is attached to the form.
  Getviewtreeobserver (). Addongloballayoutlistener (this);
    The method is called @SuppressWarnings ("deprecation") @Override protected void Ondetachedfromwindow () {//When the view is detached from the form. Super.ondetachedfromwindow ();
  Getviewtreeobserver (). Removeglobalonlayoutlistener (this); @Override public void Ongloballayout () {//In this method gets the picture of the ImageView load complete if (!isfirst) {//Get the width and height of the control I
      NT width = getwidth ();
      int height = getheight ();
      Get our picture as well as the width and height of the picture drawable drawable = getdrawable ();
      if (drawable = = null) {return;}
      int imagewidth = Drawable.getintrinsicwidth ();//Picture width int imageheight = Drawable.getintrinsicheight ();//height of picture
      float scale = 1.0f; If the picture is wider than the width of the control, but the picture height is less than the height of the control, we want to shrink the picture if (ImageWidth > Width && imageheight < height) {scale
      = width * 1.0f/imagewidth;
        //If the picture width is less than the control width, but the picture height is greater than the control height, we want to shrink the picture if (ImageWidth < width && imageheight > height) {
      Scale = height * 1.0f/imageheight; //If the width of the picture is greater than or less than the width of the control, we will scale the picture to ensure that the picture occupies the control if (ImageWidth > Width && imageheight > height) || (ImageWidth < width && ImageheigHT < height) {scale = Math.min (width * 1.0f/imagewidth, Height * 1.0f/imageheight);
      }//Initialize the corresponding scaling value Defaultscale = scale;
      Doublescale = Defaultscale * 2;
      After the picture is scaled, move the picture to the control center int dx = WIDTH/2-IMAGEWIDTH/2;
      int dy = HEIGHT/2-IMAGEHEIGHT/2;
      Mscalematrix.posttranslate (dx, dy);
      Mscalematrix.postscale (Defaultscale, Defaultscale, WIDTH/2, HEIGHT/2);
      Setimagematrix (Mscalematrix);
    Isfirst = true;
    @SuppressLint ("clickableviewaccessibility") @Override public boolean Ontouch (View V, motionevent event) {
    if (Mgesturedetector.ontouchevent (event)) {return true;}
    float x = 0;
    Float y = 0; int pointercount = Event.getpointercount ()///Get the number of fingers placed on the screen for (int i = 0; i < Pointercount; i++) {x = = even
      T.getx (i);
    Y + + event.gety (i);
    } x/= Pointercount;
    Y/= Pointercount;
      if (Mlastpintercount!= pointercount) {Iscandrag = false; MLASTX = x;

    Mlasty = y;
    } mlastpintercount = Pointercount;
        Switch (event.getaction ()) {case MotionEvent.ACTION_MOVE:float dx = X-MLASTX;
        float dy = y-mlasty;
        Iscandrag = ismove (dx, dy);
          if (Iscandrag) {RECTF RECTF = GETMATRIXRECTF ();
            if (null!= getdrawable ()) {Ischeckleft = Ischecktop = true;
              if (Rectf.width () < GetWidth ()) {//If the picture width is less than the width of the control (screen width) does not allow lateral movement of dx = 0;
            Ischeckleft = false;
              } if (Rectf.height () < GetHeight ()) {//If the picture height is less than the height of the control (screen height) does not allow longitudinal movement dy = 0;
            Ischecktop = false;
            } mscalematrix.posttranslate (dx, dy);
            Checktranslatewithborder ();
          Setimagematrix (Mscalematrix);
        } MLASTX = x;
        Mlasty = y;
      Break
   Case MotionEvent.ACTION_UP:case MotionEvent.ACTION_CANCEL:mLastPinterCount = 0;     Break
  return true; /** * Border Check when moving pictures * @description: * @date 2016-1-8 PM 4:02:24 * * private void Checktranslatewithborder ()
    {RECTF RECTF = GETMATRIXRECTF ();
    float Delx = 0;
    float dely = 0;
    int width = getwidth ();
    int height = getheight ();
    if (rectf.top > 0 && ischecktop) {dely =-rectf.top;
    } if (Rectf.bottom < height && ischecktop) {dely = Height-rectf.bottom;
    } if (Rectf.left > 0 && ischeckleft) {delx =-rectf.left;
    } if (Rectf.right < width && ischeckleft) {delx = Width-rectf.right;
  } mscalematrix.posttranslate (Delx, dely);
  //Determine if there is a move private Boolean ismove (float x, float y) {return math.sqrt (x * x + y * y) > mtouchslop; /** * Get Picture location * @description: * @date 2016-1-8 a.m. 9:02:10/Private RECTF GETMATRIXRECTF () {Matrix
    Matrix = Mscalematrix;
    RECTF recft = new RECTF (); If (getdrawable ()!= null)
      {recft.set (0, 0, getdrawable (). Getintrinsicwidth (), getdrawable (). Getintrinsicheight ());
    Matrix.maprect (RECFT);
  return RECFT;
    //Get the current picture's zoom value private float Getscale () {float values[] = new FLOAT[9];
    Mscalematrix.getvalues (values);
  return values[matrix.mscale_x];
 }
}

The above is an android to achieve ImageView picture Double-click Enlarge and shrink all the code, I hope to help you learn.

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.