Android uses gestures to drag images

Source: Internet
Author: User

In this example, Android uses gestures to drag and drop images. The running effect is as follows:

 

 

The Code is as follows:

Package com. android. drag;

Import android. app. Activity;
Import android. content. Context;
Import android. graphics. Canvas;
Import android. graphics. Color;
Import android. graphics. Paint;
Import android. OS. Bundle;
Import android. util. DisplayMetrics;
Import android. view. MotionEvent;
Import android. view. View;

/**
* Android uses gestures to drag images.
* @ Author Administrator
*
*/
Public class DragActivity 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 ImageDragView (this );

SetContentView (view );
}

Class ImageDragView extends View {

Private float x1;
Private float y1;
Private float x2;
Private float y2;

Public ImageDragView (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 = 100> = r? 100: r;

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

}


Finally, I hope to reprint friends can respect the author's labor results, plus reprint address: http://www.cnblogs.com/hanyonglu/archive/2012/02/13/2348553.html thank you.

 

Complete. Pai_^

 

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.