Android 23rd step drag and Zoom

Source: Internet
Author: User
Tags gety

 PackageCom.example.dragscale;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.graphics.Matrix;ImportAndroid.graphics.PointF;ImportAndroid.util.FloatMath;ImportAndroid.view.Menu;Importandroid.view.MotionEvent;ImportAndroid.view.View;ImportAndroid.view.View.OnTouchListener;ImportAndroid.widget.ImageView; Public classMainactivityextendsActivity {ImageView ImageView; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); ImageView= (ImageView) This. Findviewbyid (R.id.imageview); Imageview.setontouchlistener (NewTouchlistener ()); }    Private Final classTouchlistenerImplementsontouchlistener{PrivatePointF StartPoint =NewPointF (); PrivateMatrix Matrix =NewMatrix (); PrivateMatrix Cuuentmatrix =NewMatrix (); Private intMode= 0; Private Static Final intDrag=1; Private Static Final intzoom=2; Private floatStartdis; PrivatePointF midpoint; @Override Public BooleanOnTouch (View arg0, motionevent arg1) {Switch(Arg1.getaction () &motionevent.action_mask) {              CaseMotionEvent.ACTION_DOWN:mode=drag;                Cuuentmatrix.set (Imageview.getimagematrix ());                Startpoint.set (Arg1.getx (), arg1.gety ());  Break;  CaseMotionevent.action_move:if(Mode = =drag) {                    floatDx=arg1.getx ()-Startpoint.x; floatDy=arg1.gety ()-Startpoint.y;                    Matrix.set (Cuuentmatrix);                Matrix.posttranslate (dx, dy); }                Else{                    floatenddis=distance (arg1); if(Startdis >10f) {                        floatScale = enddis/Startdis;                        Matrix.set (Cuuentmatrix);                    Matrix.postscale (scale, scale, midpoint.x, MIDPOINT.Y); }                }                 Break;  CaseMotionEvent.ACTION_UP:mode= 0;  Break;  CaseMotionEvent.ACTION_POINTER_UP:mode= 0;  Break;  CaseMotionEvent.ACTION_POINTER_DOWN:mode=Zoom; Startdis=distance (arg1); if(Startdis >10f) {Midpoint=mid (arg1);                Cuuentmatrix.set (Imageview.getimagematrix ()); }                 Break;            } imageview.setimagematrix (matrix); return true; }        Private floatdistance (motionevent arg1) {floatdx= arg1.getx (1)-arg1.getx (0); floatdy= arg1.gety (1)-arg1.gety (0); returnFLOATMATH.SQRT (dx*dx+dy*dy); }        PrivatePointF Mid (motionevent arg1) {floatMidx= (Arg1.getx (1) +arg1.getx (0))/2; floatMidy= (arg1.gety (1) +arg1.gety (0))/2; return NewPointF (Midx,midy); }            }

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.