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

发布于 2024-10-19 05:46:26 字数 136 浏览 0 评论 0原文

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

拖动之前和之后鼠标的位置或者如果是简单的触摸如何获取触摸的位置。

我应该如何去绘制 线?

有人可以帮我吗?

I want to make a simple doodle application, for android, but I don't know how to get some data from android! I need:

The position of the mouse before and after the drag or if it is a simple touch how to get the position of the touch.

How should I go about drawing the
lines?

Can somebody please help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽心 2024-10-26 05:46:26

您可以在 View 中重写 onTouchEvent 方法:

@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;
}

我假设您想从拖动的起点到终点绘制一条直线,但您不想“doodle”,但修改此代码来处理其中任何一个都非常简单。

You can override the onTouchEvent method in your 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;
}

I am assuming you want to draw a straight line from the start of the drag to the endpoint and you don't want to "doodle", but its pretty simple to modify this code to handle either.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文