Drag and Drop controls
Yes. The position of the ImageView changes when the ImageView is moved, and the ImageView is horizontally centered when the image view is double-clicked.
Package com.org. demo. youku; import com.org. wangfeng. r; import android. app. activity; import android. content. sharedPreferences; import android. content. sharedPreferences. editor; import android. OS. bundle; import android. OS. systemClock; import android. view. motionEvent; import android. view. view; import android. view. view. onTouchListener; import android. widget. imageView; import android. widget. relativeLayout; import android. widget. relativeLayout. layoutParams; import android. widget. textView; public class MainDrag extends Activity {private TextView TV _top; private TextView TV _bottom; private ImageView iv_drag; // private ViewPager dPager; private int startX; private int startY; private SharedPreferences sPreferences; long [] mHits = new long [2]; // array Length indicates the number of clicks @ Override protected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stub super. onCreate (savedInstanceState); setContentView (R. layout. maindrag); TV _top = (TextView) findViewById (R. id. TV _top); TV _bottom = (TextView) findViewById (R. id. TV _bottom); iv_drag = (ImageView) findViewById (R. id. iv_drag); // calculate the width and height of the mobile phone screen. final int winWidth = getWindowManager (). getdefadisplay display (). getWidth (); final int winHeight = getWindowManager (). getdefadisplay display (). getHeight (); // used to record coordinates. The coordinate sPreferences = getSharedPreferences ("config", Activity. MODE_PRIVATE); int getLastX = sPreferences. getInt ("lastX", 0); int getLastY = sPreferences. getInt ("lastY", 0); // onMeasure (measurement view), onLayout (placement), onDraw (Painting) // ivDrag. layout (getLastX, getLastY, getLastX + ivDrag. getWidth (), // getLastY + ivDrag. getHeight (); // you cannot use this method. if (getLastY> winHeight/2) cannot be placed before measurement is completed, hide TV _top.setVisibility (View. VISIBLE); TV _bottom.setVisibility (View. INVISIBLE);} else {TV _top.setVisibility (View. invisibility); TV _bottom.setVisibility (View. VISIBLE);} RelativeLayout. layoutParams layoutParams = (LayoutParams) iv_drag. getLayoutParams (); layoutParams. leftMargin = getLastX; // set the left margin of layoutParams. topMargin = getLastY; // set the top margin iv_drag.setLayoutParams (layoutParams); // reset the position // set the double-click event iv_drag.setOnClickListener (new View. onClickListener () {@ Override public void onClick (View arg0) {// TODO Auto-generated method stub System. arraycopy (mHits, 1, mHits, 0, mHits. length-1); mHits [mHits. length-1] = SystemClock. uptimeMillis (); // The time after startup. if (mHits [0]> = (SystemClock. uptimeMillis ()-500) {// center the image in the System. out. println ("double-click to center"); iv_drag.layout (winWidth/2-iv_drag.getWidth ()/2, iv_drag.getTop (), winWidth/2 + iv_drag.getWidth ()/2, iv_drag.getBottom () ;}}); // sets the iv_drag.setOnTouchListener (new OnTouchListener () {@ Override public boolean onTouch (View v, MotionEvent event) {// TODO Auto-generated method stub switch (event. getAction () {case MotionEvent. ACTION_DOWN: // initialize the starting point coordinate startX = (int) event. getRawX (); startY = (int) event. getRawY (); break; case MotionEvent. ACTION_MOVE: // The Moving Coordinate int endX = (int) event. getRawX (); int endY = (int) event. getRawY (); // calculate the offset int dx = endX-startX; int dy = endY-startY; // The distance from the upper left and lower right after the update int l = iv_drag.getLeft () + dx; int t = iv_drag.getTop () + dy; int r = iv_drag.getRight () + dx; int B = iv_drag.getBottom () + dy; // determine whether the screen boundary is exceeded, note the height of the status bar if (l <0 | r> winWidth | t <0 | B> winHeight) {System. out. println ("not required"); break;} // determines whether to display and hide the if (t> winHeight/2) in the prompt Box Based on the Image Position {// display above, hide TV _top.setVisibility (View. VISIBLE); TV _bottom.setVisibility (View. INVISIBLE);} else {TV _top.setVisibility (View. invisibility); TV _bottom.setVisibility (View. VISIBLE);} // update the iv_drag.layout (l, t, r, B); // reinitialize the startX = (int) event. getRawX (); startY = (int) event. getRawY (); System. out. println ("ACTION_MOVE coordinate x" + startX + "--- iv_drag.getLeft () ---" + iv_drag.getLeft (); break; case MotionEvent. ACTION_UP: // records the coordinate System after moving. out. println ("ACTION_UP" + "iv_drag.getLeft () ---" + iv_drag.getLeft () + "--- iv_drag.getTop () ---" + iv_drag.getTop (); Editor edit = sPreferences. edit (); edit. putInt ("lastX", iv_drag.getLeft (); edit. putInt ("lastY", iv_drag.getTop (); edit. commit (); break; default: break;} return false; // event to be passed down so that onclick (double-click event) can respond }});}}
<? 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"> <TextView android: id = "@ + id/TV _top" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: Rule = "true" android: layout_centerHorizontal = "true" android: background = "@ drawable/png_3" android: gravity = "center" android: padding = "10dp" android: text = "hold down the prompt box and drag it to any location \ n press the return key of the mobile phone to take effect immediately" android: textColor = "#000" android: textSize = "16sp"/> <TextView android: id = "@ + id/TV _bottom" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: layout_alignParentBottom = "true" android: layout_centerHorizontal = "true" android: background = "@ drawable/png_3" android: gravity = "center" android: padding = "10dp" android: text = "hold down the prompt box and drag it to any location \ n press the return key of the mobile phone to take effect immediately" android: textColor = "#000" android: textSize = "16sp" android: visibility = "invisible"/> <ImageView android: id = "@ + id/iv_drag" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_marginTop = "90dp" android: contentDescription = "@ null" android: src = "@ drawable/drag"/> </RelativeLayout>