Android scaling using gestures to process instance code

Source: Internet
Author: User

Internet rumors that HTC's HERO-ROM supports multi-touch arguments mostly stem from browsing web pages and pictures, like the IPhone through gestures to control the size of the page. The following example uses the existing API to scale HERO images and webpages.

The main principle is the parameter MotionEvent in the onTouchEvent event, which has a getSize () method. At one point, this method returns 0 forever, while at two electric shocks, this method returns different values based on the relative position of the two points. We only need to calculate the distance variation between two points. The size of the distance indicates the trend we want the target to change. The getX () and getY () methods are always located between two contacts, so that we have both the trend and the target. The rest is to zoom in or out the target based on the trend.

Copy codeThe Code is as follows :/**
*...
*
* @ Author vlinux
*
*/
Public class MultiTouchTestActivity extends Activity {
/** Called when the activity is first created .*/
@ 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 (Context context ){
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 = 50> = r? 50: r;

Paint paint = new Paint ();
Paint. setColor (Color. BLUE );
Canvas. drawCircle (x1, y1, r, paint );

}

}

}

Related Article

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.