首页 > 代码库 > 控件跟随手指移动(心得)

控件跟随手指移动(心得)

        /**	 * 初始坐标	 * 	 * 都是相对于控件本身的坐标	 * 	 * x y 手指按下的坐标	 */	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();			x = event.getX();			y = 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 - x;			float currentY = this.getY() + changeY - y;			this.setX(currentX);			this.setY(currentY);			// 将移动后的坐标置为按下的位置			downX = moveX;			downY = moveY;			break;		}		return true;	}    

  

控件跟随手指移动(心得)