检测Android中"拖动"的开始和结束位置,并在它们之间画一条线

And*_*llo 6 java android paint

我想制作一个简单的涂鸦应用程序,用于android,但我不知道如何从android获取一些数据!我需要:

拖动前后鼠标的位置或者如果是简单的触摸如何获得触摸的位置.

我该如何画线?

有人能帮帮我吗?

dby*_*rne 10

您可以覆盖以下onTouchEvent方法中的方法View:

@Override
public boolean onTouchEvent (MotionEvent event) {

  if (event.getAction() == MotionEvent.ACTION_DOWN) {

    start_x = event.getX();
    start_y = event.getY();     

  } else if (event.getAction() == MotionEvent.ACTION_MOVE) {

    //create the line from start_x and start_y to the current location
    //don't forget to invalidate the View otherwise your line won't get redrawn

  } else if (event.getAction() == MotionEvent.ACTION_UP) {

    //might not need anything here

  }
  return true;
}
Run Code Online (Sandbox Code Playgroud)

我假设您想从拖动的开始画一条直线到端点并且您不想"涂鸦",但是修改此代码以处理它们非常简单.