MainActivity is as follows:
Package CN. dragtest; import android. app. activity; import android. graphics. rect; import android. OS. bundle; import android. util. displaymetrics; import android. view. motionevent; import android. view. view; import android. view. view. ontouchlistener; import android. view. window; import android. widget. button; import android. widget. imageview; public class mainactivity extends activity implements ontouchlistener {private button mbutton; private imageview mimageview; private displaymetrics; private float lastx = 0; private float lasty = 0; private int screenwidth = 0; private int screenheight = 0; private int left; private int top; private int right; private int bottom; private Boolean isfirst = true; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); initview ();} private void initview () {mbutton = (button) findviewbyid (R. id. button); mimageview = (imageview) findviewbyid (R. id. imageview); mbutton. setontouchlistener (this); mimageview. setontouchlistener (this);} public Boolean ontouch (view, motionevent event) {If (isfirst) {// obtain the screen width displaymetrics = getresources (). getdisplaymetrics (); screenwidth = displaymetrics. widthpixels; // obtain the height rect = new rect (); window = getwindow (); mimageview of the title bar and status bar. getwindowvisibledisplayframe (rect); int statusbarheight = rect. top; int contentviewtop = Window. findviewbyid (window. id_android_content ). gettop (); int titlebarheight = contentviewtop-statusbarheight; // obtain the screen height screenheight = displaymetrics. heightpixels-(statusbarheight + titlebarheight); isfirst = false;} int action = event. getaction (); Switch (Action) {// press case motionevent. action_down: // The coordinate lastx = event. getrawx (); lasty = event. getrawy (); break; // move case motionevent. action_move: // float distancex = event. getrawx ()-lastx; float distancey = event. getrawy ()-lasty; // coordinate left = (INT) (view. getleft () + distancex); Top = (INT) (view. gettop () + distancey); Right = (INT) (view. getright () + distancex); bottom = (INT) (view. getbottom () + distancey); // If (left <0) {left = 0; Right = view when the screen is dragged out. getwidth () ;}if (Right> screenwidth) {right = screenwidth; left = screenWidth-view.getWidth () ;}if (top <0) {Top = 0; bottom = view. getheight () ;}if (bottom> screenheight) {Bottom = screenheight; Top = screenHeight-view.getHeight () ;}// display the Image view. layout (left, top, right, bottom); lastx = event. getrawx (); lasty = event. getrawy (); break; // lift case motionevent. action_up: break; default: break;} return false ;}}
Main. xml is as follows:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center_horizontal" > <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" android:clickable="true" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="please drag" android:clickable="true" /></LinearLayout>