Android Multi-Touch (Image Zoom Demo)

Source: Internet
Author: User

This article mainly describes the Android multi-touch, using a picture scaling example, to better explain its principle. You need to implement the Ontouchlistener interface, overriding the Ontouch method.

Realize:


Source:

Layout file:

Activity_main:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:id=" @+id/layout "    android:layout_width=" Match_parent "    android: layout_height= "Match_parent" >    <imageview        android:id= "@+id/imageview"        android:layout_width= " Wrap_content "        android:layout_height=" wrap_content "        android:src=" @drawable/ic_launcher "/></ Relativelayout>
Code:

Package Com.multitouch;import Android.app.activity;import Android.os.bundle;import android.util.log;import Android.view.motionevent;import Android.view.view;import Android.view.view.ontouchlistener;import Android.widget.imageview;import Android.widget.relativelayout;import Android.widget.RelativeLayout.LayoutParams /** * Multi-Touch Demo instance: Image zoom. * */public class Mainactivity extends Activity {private Relativelayout layout;protected String TAG = "Zhongyao";p rivate I Mageview imageview;private float currentdistance;private float lastdistance =-1; @Overrideprotected void OnCreate ( Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); layout = ( Relativelayout) Findviewbyid (r.id.layout); ImageView = (ImageView) Findviewbyid (R.id.imageview); Layout.setontouchlistener (New Ontouchlistener () {@Overridepublic Boolean onTouch (View V, motionevent event) {switch ( Event.getaction ()) {/** * finger press */case motionevent.action_down:log.d (TAG, "down!!!"); break;/** * FingerMobile */case MOTIONEVENT.ACTION_MOVE:LOG.D (TAG, "Move!!!"); * * * First to determine whether the number of fingers is greater than two. * If greater than two, do the following (that is, the zoom action of the picture). */if (Event.getpointercount () >= 2) {Float OffsetX = event.getx (0)-event.getx (1); float OffsetY = event.gety (0)-even T.gety (1);/** * Distance difference between origin and sliding point */currentdistance = (float) math.sqrt (OffsetX * offsetx+ OffsetY * OffsetY); if (Lastdistance & Lt 0) {lastdistance = currentdistance;} else {/** * If the current sliding distance (currentdistance) is greater than 5 inches (or other dimensions) compared to the last recorded distance (lastdistance). * Then real picture magnification */if (Currentdistance-lastdistance > 5) {log.d (TAG, "Zoom In!!! "); Relativelayout.layoutparams LP = (layoutparams) imageview.getlayoutparams ();/** * The picture is enlarged one time to 1.1 times times the original picture (and of course, it can be a different value). */lp.width = (int) (Imageview.getwidth () * 1.1); lp.height = (int) (Imageview.getheight () * 1.1); IMAGEVIEW.SETLAYOUTPARAMS (LP); lastdistance = currentdistance;/** * If the last recorded distance (lastdistance) is less than 5 inches compared to the current sliding distance (currentdistance), * then the picture shrinks. */} else if (Lastdistance-currentdistance > 5) {log.d (TAG, "Zoom Out!!! "); Relativelayout.layoutparams LP = (Layoutparams) imageview.getlayoutparams ();/** * Picture width is 0.9 times times smaller than the original picture. */lp.width = (int) (Imageview.getwidth () * 0.9); lp.height = (int) (Imageview.getheight () * 0.9); IMAGEVIEW.SETLAYOUTPARAMS (LP); lastdistance = Currentdistance;}}} break;/** * Finger lift */case motionevent.action_up:log.d (TAG, "UP!!!"); break;} return true;}});}}

Source code Download:

Click to download the source code

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.