Android Drag Control to change its location

Source: Internet
Author: User

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>

 

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.