Android for picture multi-Touch free scaling

Source: Internet
Author: User

Introduction

As Android Developers, we often need to customize the control, such as the following we say the implementation of the image of multi-touch and scaling release, which is also due to the user already have such common sense, that is to see the picture where you can click to see the larger image, And the image can be stretched and moved by the finger, if the application does not achieve this, it will be a bad experience for users, users are very "angry". So as an Android Developer, our task is to make the user "cool". Hahaha .... Below we will Implement these functions through custom ImageView.

Technology involved

One,matrix,Android is The matrix to control the image of the telescopic and pan.

Second, Scalegesturedetector(telescopic gesture detector), to achieve the user operation of the image scaling monitoring.

Implementation Principle

First, create the Zoomimageview class by overriding the ImageView Onattachedtowindow () Register Global Layout Listener getviewtreeobserver (). Addongloballayoutlistener (this)toinitialize the image control

Second, in the global layout listener through the Matrix control to initialize the image center display and zoom.

Third, register OnTouch() event, through code mscalegesturedetector.ontouchevent (motionevent); Implement Scalegesturedetector listening gestures.

Iv. callback method in scalegesturedetector onscale (scalegesturedetector detector) Within the implementation of the user's operation, to achieve the expansion and movement of the picture.

Code
Package Com.liujun.liujunzoomimagedemo.view;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.widget.imageview;import Android.view.viewtreeobserver;public Class Zoomimageview extends ImageView implements Viewtreeobserver.ongloballayoutlistener, Onscalegesturelistener, Ontouchlistener {private Scalegesturedetector mscalegesturedetector = null;private Matrix matrix;private float[] Matrixvalues = Nine value of the new float[9];//matrix private Boolean isoncelayout = true;//Set zoom ratio private float Initscale = 1.0f;private fl Oat Midscale = 2.0f;private float Maxscale = 4.0f;public Zoomimageview (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle); super.Setscaletype (Scaletype.matrix);//Set the picture through the matrix control//instantiation of the telescopic gesture detector mscalegesturedetector = new Scalegesturedetector (context, The Matrix = new Matrix (); This.setontouchlistener (this);} Public Zoomimageview (context context, AttributeSet Attrs) {This (context, attrs, 0);} Public Zoomimageview (Context context) {This (context, null);} @Overrideprotected void Onattachedtowindow () {Super.onattachedtowindow ();//register Global Layout Listener getviewtreeobserver (). Addongloballayoutlistener (this);} @SuppressWarnings ("deprecation") @Overrideprotected void Ondetachedfromwindow () {Super.ondetachedfromwindow ();// Cancels the global Layout listener getviewtreeobserver (). Removeglobalonlayoutlistener (this);}  /** * Gets the parameters of the layout (this method calls two times) */@Overridepublic void Ongloballayout () {if (isoncelayout) {//center the picture, and the telescopic picture drawable drawable = This.getdrawable (); if (drawable = = null) {return;} Gets the parent control of the wide-height int parentwidgetwidth = this.getwidth (); int parentwidgetheight = This.getheight ();//Gets the width of the picture int Drawableheight = Drawable.getintrinsicheight (); int drawablewidth = Drawable.getintriNsicwidth ();//define scaling than float scale = 1.0f;//when the width of the picture is greater than the width of the parent control when the height is less than the height of the parent control (zoom Out) if (Drawablewidth > Parentwidgetwidth && Amp Drawableheight <= parentwidgetheight) {scale = Parentwidgetwidth * 1.0f/drawablewidth;} When the picture height is higher than the parent control, but the width is less than the parent control (zoom Out) if (Drawableheight > Parentwidgetheight && drawablewidth <= parentwidgetwidth ) {scale = Parentwidgetheight * 1.0f/drawableheight;} When the picture width and height are greater than the parent control (zoom Out) if (Drawableheight > Parentwidgetheight && drawablewidth > Parentwidgetwidth) { Scale = Math.min (Parentwidgetheight * 1.0f/drawableheight, Parentwidgetwidth * 1.0f/drawablewidth);} When the picture width and height are less than the parent control (enlarge) if (Drawableheight < parentwidgetheight && Drawablewidth < parentwidgetwidth) {scale = Math.min (Parentwidgetheight * 1.0f/drawableheight, Parentwidgetwidth * 1.0f/drawablewidth);} Set the scaling of the initialization to Initscale = scale;//to scale the picture and move the picture to the middle of the parent control float dx = (parentwidgetwidth-drawablewidth)/2;float dy = (parentwi Dgetheight-drawableheight)/2;matrix.posttranslate(dx, dy);//Zoom Picture Matrix.postscale (scale, scales, PARENTWIDGETWIDTH/2, PARENTWIDGETHEIGHT/2);// Apply the matrix to the image This.setimagematrix (matrix); isoncelayout = false;}} /** * Get Picture Current zoom ratio * * @return */private float Getcurrentimagescale () {matrix.getvalues (matrixvalues); return matrixvalues[ MATRIX.MSCALE_X];} @Overridepublic Boolean Onscale (Scalegesturedetector detector) {//Get picture current zoom ratio float Currentscalse = Getcurrentimagescale ();//Get the picture to be scaled by the scale float scalefactor = Detector.getscalefactor (); if (this.getdrawable () = = null) { return true;} The user will enlarge the image or the user will be zoomed out if ((Scalefactor > 1.0f && currentscalse < Maxscale) | | (Scalefactor < 1.0f && currentscalse > Initscale)) {//Zoom out if (Scalefactor * Currentscalse < Initscale) {scalefactor = Initscale/currentscalse;} When zoomed in if (Scalefactor * currentscalse > Maxscale) {scalefactor = Maxscale/currentscalse;} Matrix.postscale (Scalefactor, Scalefactor, Detector.getfocusx (), detector.getfocusy ());// Check the boundary and center point Checkborderandcenterwhenscale (); Setimagematrix (matrix);} return true;} @Overridepublic Boolean onscalebegin (Scalegesturedetector detector) {return true;} @Overridepublic void Onscaleend (Scalegesturedetector detector) {}/** * When zooming, controls the bounds and center of the picture */private Void Checkborderandcenterwhenscale () {//Gets the rectangle of the picture in the current zoom process RECTF RECTF = GETMATRIXRECTF (); float DeltaX = 0;float DeltaY = 0;//Gets the parent The control's wide height int parentwidth = getwidth (), int parentheight = GetHeight (),//If the width is greater than the screen width if (rectf.width () >= parentwidth) {if (re  Ctf.left > 0) {//left blank DeltaX =-rectf.left;//left to move}if (Rectf.right < Parentwidth) {//On the right appears blank deltax = Parentwidth -rectf.right;//move to the right}}//if the height is greater than the screen height if (rectf.height () >= parentheight) {if (Rectf.top > 0) {///top appears blank DeltaY =-re ctf.top;//Move Down}if (Rectf.bottom < Parentheight) {//below appears blank DeltaY = parentheight-rectf.bottom;//Move Down}}//if the width is less than the parent control's width if (rectf.width () < Parentwidth) {//to base display DeltaX = Parentwidth * 0.5f-rectf.right + 0.5f * rectf.width ();} If the height message is less than the height of the parent control if (Rectf.height () < ParenthEight) {//requires base display DeltaY = Parentheight * 0.5f-rectf.bottom + 0.5f * rectf.height ();} Move the picture to the parent control center Matrix.posttranslate (DeltaX, DeltaY);} /** * Get the picture through the matrix control the rectangle after zooming * * @return */private RECTF GETMATRIXRECTF () {Matrix matrix2 = matrix; RECTF RECTF = new RECTF ();D rawable drawable = this.getdrawable (); if (drawable! = null) {rectf.set (0, 0, Drawable.getintrin Sicwidth (), Drawable.getintrinsicheight ()); Matrix2.maprect (RECTF);} return RECTF;} @Overridepublic Boolean OnTouch (View view, Motionevent motionevent) {// User-scaled phone probe handles touch event mscalegesturedetector.ontouchevent (motionevent); return true;}}
Implement

Summarize

The expansion of the image release has been achieved, but I believe the reader can also know that the only implementation of multi-touch free scaling is not enough, the user also want to be able to swipe left and right to implement the picture switch browsing, the implementation of this part will be released in the near future, and look forward to.


Code






Android for picture multi-Touch free scaling

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.