private float x = 0, y = 0; @Override public boolean onTouchEvent(MotionEvent event) { // 手指按下的坐标 float downX = 0, downY = 0; // 移动后的坐标 改变的值 float moveX = 0, moveY = 0, changeX = 0, changeY = 0; switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: downX = event.getX(); downY = event.getY(); break; case MotionEvent.ACTION_MOVE: moveX = event.getX(); moveY = event.getY(); changeX = moveX - downX; changeY = moveY - downY; //this.getX() this.getY() 是在父控件的坐标 float currentX = this.getX() + changeX; float currentY = this.getY() + changeY; this.setX(currentX); this.setY(currentY); // 将移动后的坐标置为按下的位置 downX = moveX; downY = moveY; break; } return true; }