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_^