Android Single Touch mobile, multi-touch Zoom Out

Source: Internet
Author: User
Tags gety

Activity_main.xml:

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"Xmlns:tools="Http://schemas.android.com/tools"Android:layout_width="match_parent"Android:layout_height="match_parent"Tools:context=". Mainactivity"> <ImageView Android:id="@+id/imgage"android:src="@drawable/liying"Android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:layout_centerhorizontal="true"android:layout_centervertical="true"/></relativelayout>

Mainactivity.class:

Package Com.example.day4_one;import android.os.bundle;import android.app.activity;import android.view.Menu; Import Android.app.activity;import Android.content.res.resources;import Android.util.displaymetrics;import Android.util.log;import Android.view.menu;import Android.view.motionevent;import Android.view.View;import Android.view.view.ontouchlistener;import Android.widget.imageview;import Android.widget.relativelayout;import Android.widget.Toast; Public classMainactivity extends Activity {PrivateImageView image; floatLastdistance =-1; floatx0 =0; floatY0 =0; Private intScreenWidth; Private intScreenHeight; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);                Setcontentview (R.layout.activity_main); Image=(ImageView) Findviewbyid (r.id.imgage); //get the screen width high to keep out of boundsDisplaymetrics DM =getresources (). Getdisplaymetrics (); ScreenWidth=Dm.widthpixels; ScreenHeight= dm.heightpixels- -; Image.setontouchlistener (NewOntouchlistener () {Private floatx; Private floaty; @Override PublicBoolean OnTouch (View v,final motioneventEvent) {                //TODO auto-generated Method Stub                intAction =Event. Getaction (); LOG.I ("TAG","Touch Start ..."); Switch(action) { CaseMotionevent.action_down://GetX () is the x-coordinate of the widget relative to its upper-left corner, and Getrawx () is the x-coordinate value relative to the upper-left corner of the screen (note: The upper-left corner of the screen is the upper-left corner of the phone screen, regardless of whether the activity is titlebar or full screen) x =Event. GETRAWX (); Y=Event. GETRAWX ();  Break;  CaseMotionevent.action_move://get the number of touch points                    intCount =Event. Getpointercount (); LOG.I ("TAG","number of touch points:"+count); if(Count >1) {log.i ("TAG",Event. GetX (0)+","+Event. GetY (0)+"|"+Event. GetX (1)+","+Event. GetY (1)); //get two points of coordinate difference                        floatDistancex =Event. GetX (0) -Event. GetX (1); floatDistancey =Event. GetY (0) -Event. GetY (1); //get the distance between two points                        floatBetweendistance = (float) math.sqrt (Distancex*distancex + distancey*Distancey); LOG.I ("TAG","new distance between the current two points:"+betweendistance); if(Betweendistance <1) {lastdistance=betweendistance; }Else if((betweendistance-lastdistance) >5){                            //Zoom inLastdistance =betweendistance; //Get layout ParametersRelativelayout.layoutparamsparams=(Android.widget.RelativeLayout.LayoutParams) image.getlayoutparams (); params. Width = (int) (Image.getwidth () *1.1f); params. Height = (int) (Image.getheight () *1.1f); Image.setlayoutparams (params); }Else if((lastdistance-betweendistance) >5) {lastdistance=betweendistance; //Get layout ParametersRelativelayout.layoutparamsparams=(Android.widget.RelativeLayout.LayoutParams) image.getlayoutparams (); params. Width = (int) (Image.getwidth () *0.9f); params. Height = (int) (Image.getheight () *0.9f); Image.setlayoutparams (params); }                                            }Else if(Count = =1){                                                //Moving Distance                        floatRAWX =Event. GETRAWX ()-x; floatRawy =Event. Getrawy ()-y; //Define a new                        intleft = (int) (Image.getleft () +rawx); inttop = (int) (Image.gettop () +Rawy); intright = (int) (Image.getright () +rawx); intBottom = (int) (Image.getbottom () +Rawy); //setting cannot be out of bounds                          if(Left <0) { left=0; Right= left +v.getwidth (); }                                      if(Right >screenwidth) { Right=ScreenWidth; Left= Right-v.getwidth (); }                                      if(Top <0) {Top=0; Bottom= Top +v.getheight (); }                                      if(Bottom >screenheight) {Bottom=ScreenHeight; Top= Bottom-v.getheight (); }                                                                         //Assign ValueImage.layout (left, top, right, bottom); //Change to a new labelx=Event. GETRAWX (); Y=Event. Getrawy (); /*return false;*/                    }                     Break; }                                return true;    }        }); }    //Global Move@Override PublicBoolean ontouchevent (motioneventEvent) {    //TODO auto-generated Method Stub      /*Image.setx (Event.getx ()); Image.sety (Event.gety ()); */    return true;} }

Android Single Touch mobile, multi-touch Zoom Out

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.