Android Multi-Touch (image zoom in and zoom out)

Source: Internet
Author: User

You don't have to say anything, there are notes, basic, you can read them.

Welcome to my communication group: 386451316

Main.xml file

<framelayout 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"
Android:id= "@+id/mo_i"
Tools:context= ". Mainactivity ">

<imageview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:id= "@+id/im"
android:src= "@drawable/ic_launcher"/>

</FrameLayout>

Java code

public class Mainactivity extends Activity {

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);

Tm_i = (framelayout) Findviewbyid (r.id.mo_i);
im = (ImageView) Findviewbyid (r.id.im);

Touch events
Tm_i.setontouchlistener (New Ontouchlistener () {
Float current;//first time distance
float Ladtcuss =-1; The last distance

@Override
public boolean OnTouch (View V, motionevent event) {
Switch (event.getaction ()) {//Get Event
Case motionevent.action_down://pressed action

Break
Case motionevent.action_move://Mobile
/**
* Getting multiple touch points requires a real machine debug
*/
if (Event.getpointercount () >= 2) {
float OffsetX = event.getx (0)-event.getx (1);
float OffsetY = event.gety (0)-event.gety (0);

Using the Pythagorean theorem to get two points distance
Current = (float) math.sqrt (OffsetX * OffsetX + OffsetY
* OffsetY);
if (ladtcuss<0) {
Ladtcuss = current;
}else{
if (current-ladtcuss>5) {
System.out.println ("1");
Framelayout.layoutparams LP = (layoutparams) im.getlayoutparams ();
lp.width= (int) (1.1F * im.getwidth ());
lp.height= (int) (1.1F * im.getheight ());
IM.SETLAYOUTPARAMS (LP);
Ladtcuss = current;
}else if (ladtcuss-current>5) {
System.out.println ("0");
Framelayout.layoutparams LP = (layoutparams) im.getlayoutparams ();
lp.width= (int) (0.9f * im.getwidth ());
lp.height= (int) (0.9f * im.getheight ());
IM.SETLAYOUTPARAMS (LP);
Ladtcuss = current;
}
}
}
Get the number of touch points
System.out.println ("motoll---->" + event.getpointercount ());
System.out.println (String.Format ("X1:%f y1:%f x2:%f y2%f",
Event.getx (0), event.gety (0), Event.getx (1),
Event.gety (1)));
/**
* Below is a touch point to get
*/
The picture is in Framelayout
Framelayout.layoutparams LP = (layoutparams)
Im.getlayoutparams ();
Lp.leftmargin = (int) event.getx ();
Lp.topmargin = (int) event.gety ();
IM.SETLAYOUTPARAMS (LP);
Output Touch Point
System.out.println (String.Format ("X:%f, y:%f", Event.getx (), Event.gety ()));
Break
Case motionevent.action_up://Bounce Up

Break

}
False indicates that no subsequent events were triggered by the event
return true;
}
});
}

Private Framelayout tm_i;
Private ImageView im;

}

Android Multi-Touch (image zoom in and 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.