我正在尝试使用onTouchListener使imageview可拖动。这是我迄今为止的代码拖动Android如何在relativelayout中拖动imageview,然后将其放回原位
switch (ME.getAction()){
case MotionEvent.ACTION_DOWN://IF USER PRESSED ON THE IMAGE
origix = v.getLeft(); //save original x coordinate calculated with the offset
origiy = v.getTop();
break;
case MotionEvent.ACTION_MOVE://IF USER IS MOVING THE IMAGE
System.out.println("MOVED");
int x_cord = (int)ME.getRawX(); //get coordinate of the user touch
int y_cord = (int)ME.getRawY();
//set the image to the new coordinates based on where the user is touching and dragging
MarginLayoutParams marginParams = new MarginLayoutParams(v.getLayoutParams());
marginParams.setMargins(x_cord - origix, y_cord - origiy,0, 0);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
v.setLayoutParams(layoutParams);
MOVED = true; //mark that the image has been move
break;
变量'v'是imageview。我得到的问题是,getRawX和getRawY没有返回正确的坐标(我认为...)。当我尝试触摸图像然后尝试拖动它时,图像开始从另一个位置拖动。通常比原来的位置更高,更多。的ImageView的处于RelativeLayout的和当移动开始我使用API 10
喜看看这个样本http://androidrox.wordpress.com/2011/05/13/android-sample-app-拖放图像使用触摸/ – itsrajesh4uguys