Android Zoom processing Instance code via gesture _android

Source: Internet
Author: User

Rumors on the web that HTC's Hero-rom support for multi-touch are mostly rooted in the ability to control the size of a page by gestures, like the iphone, when browsing Web pages and images. The following example is the use of the existing API to achieve Hero Browsing pictures and Web page zoom function.

The main principle is the parameter Motionevent in the Ontouchevent event, which has a getsize () method. At one point, the method always returns 0, and when two is electrocuted, the method returns different values based on the relative position of two points. We only need to figure out the distance between two points, the size of the distance indicates the trend we want to change the target. The Getx () and Gety () methods are always located between the two contacts, so we have both the trend and the target. The rest is to enlarge or shrink the target according to the trend.

Copy Code code as follows:

/**
* ...
*
* @author Vlinux
*
*/
public class Multitouchtestactivity extends activity {
/** called the activity is a. */
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
View view = new Multitouchview (this);
Setcontentview (view);
}

Class Multitouchview extends View {

private float x1;
private float Y1;
private float x2;
private float y2;

Public Multitouchview {
Super (context);
TODO auto-generated Constructor stub
}

@Override
public boolean ontouchevent (Motionevent event) {
TODO auto-generated Method Stub

Float size = event.getsize ();

int szi = (int) size;
int dxi = Szi >> 12;
int dyit = ((1 << 12)-1);
int dyi = Szi & dyit;

Displaymetrics metrics = getresources (). Getdisplaymetrics ();
FLOAT dx = metrics.widthpixels * DXi/(float) dyit;
float dy = metrics.heightpixels * Dyi/(float) dyit;

X1 = Event.getx ();
Y1 = Event.gety ();

x2 = x1 + dx;
y2 = y1 + dy;

Invalidate ();

return true;
}

@Override
protected void OnDraw (Canvas Canvas) {
TODO auto-generated Method Stub
Super.ondraw (canvas);

float R = (float) math.sqrt ((x1-x2) * (X1-X2) + (Y1-Y2)
* (Y1-Y2))/2;
R = >= r? 50:r;

Paint Paint = new Paint ();
Paint.setcolor (Color.Blue);
Canvas.drawcircle (x1, y1, R, Paint);

}

}

}


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.