This example describes the implementation of the controls that the Android programming control can drag. Share to everyone for your reference, specific as follows:
What is the difference between clicking and touching?
Click: A set of movements of a set of fingers press the button finger to stay in the button for a while the finger left button
private static final String TAG = "dragviewactivity";
Private ImageView Iv_dv_view;
Private TextView Tv_drag_view;
private int startx;
private int starty;
Private Sharedpreferences sp;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Requestwindowfeature (Window.feature_no_title);
Setcontentview (R.layout.dragview);
drawable drawable = new colordrawable (color.transparent);
GetWindow (). setbackgrounddrawable (drawable);
Iv_dv_view = (ImageView) This.findviewbyid (R.id.iv_dv_view);
Tv_drag_view = (TextView) This.findviewbyid (R.id.tv_drag_view);
SP = this.getsharedpreferences ("config", context.mode_private);
Iv_dv_view.setontouchlistener (this);
} @Override protected void Onresume () {super.onresume ();
int x = Sp.getint ("Lastx", 0);
int y = sp.getint ("Lasty", 0); Iv_dv_view.layout (Iv_dv_view.getleft () + x, iv_dv_view.gettop () + y,//Iv_dv_view.getright () + x, IV_DV_VIEW.G EtbottOm () + y);
Iv_dv_view.invalidate ()//interface Layoutparams params = (layoutparams) iv_dv_view.getlayoutparams ();
Params.leftmargin = x;
Params.topmargin = y;
Iv_dv_view.setlayoutparams (params);
@Override public boolean Ontouch (View V, motionevent event) {switch (V.getid ()) {//If you drag the finger on the ImageView Case R.id.iv_dv_view://EVENT.GETRAWX (); Get Finger First Contact screen in x direction of coordinate switch (event.getaction ()) {case motionevent.action_down://get finger first touch screen startx =
(int) event.getrawx ();
Starty = (int) Event.getrawy ();
Break
Case motionevent.action_move://finger on the screen move the corresponding event int x = (int) event.getrawx ();
int y = (int) Event.getrawy (); if (Y < 400) {//Set TextView below the form tv_drag_view.layout (Tv_drag_view.getleft (), 420, t
V_drag_view.getright (), 440);
else {tv_drag_view.layout (Tv_drag_view.getleft (), Tv_drag_view.getright (), 80); //Get the finger moving distance int dx = X-STARTX;
int dy = Y-starty;
Get the coordinates int l = iv_dv_view.getleft () of the ImageView apex of the first vertex;
int r = iv_dv_view.getright ();
int t = iv_dv_view.gettop ();
int b = Iv_dv_view.getbottom ();
Change the position of the ImageView in the form iv_dv_view.layout (l + dx, T + dy, r + dx, B + dy);
Gets the moved position startx = (int) event.getrawx ();
Starty = (int) Event.getrawy ();
Break
Case motionevent.action_up://finger left screen corresponding event log.i (TAG, "finger left screen");
Record the last picture at the position of the form int lasty = Iv_dv_view.gettop ();
int lastx = Iv_dv_view.getleft ();
Editor Editor = Sp.edit ();
Editor.putint ("Lasty", lasty);
Editor.putint ("Lastx", LASTX);
Editor.commit ();
Break
} break;
Return true;//does not interrupt touch event returns}
The XML is as follows:
<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android=
"http://schemas.android.com/apk" /res/android "
android:layout_width=" match_parent "
android:layout_height=" Match_parent "
android: Background= "#cc000000"
>
<imageview
android:layout_width= "160dip"
android:layout_height= "60dip"
android:background= "@drawable/button_background_selected"
android:id= "@+id/iv_dv_view"
>
<textview
android:id= "@+id/tv_drag_view"
android:layout_margintop= "80dip"
android: Layout_width= "Fill_parent"
android:layout_height= "20dip"
android:text= "hold down the green bar to drag the location displayed by the attribution"
/>
</RelativeLayout>
Touch: Move your finger away from the screen with a finger on the screen
More interested readers of Android-related content can view this site: "Introduction to Android Development and advanced Course", "Android Communication Summary", "Android Basic Components Usage Summary", "Android View Summary", " Android Layout layout Tips and a summary of the use of Android controls
I hope this article will help you with the Android program.