- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想在 View 上的点之间画线,然后将这些点拉到所需的位置,即使形状会改变。
我知道如何在两点之间画线 canvas.drawLine(10, 10, 90, 10, paint);
通过使用它我可以在点之间画线。
EDIT
:这里我附上图片以便清楚解释,从 Paul 回答现在我能够在点之间画线,仍然有拉点的问题......
最佳答案
这是它是如何完成的。假设你有你的观点,让这些成为全局性的:
PointF topLeft = new PointF(10,10);
PointF topRight = new PointF(90,10);
PointF bottomLeft = new PointF(10,90);
PointF bottomRight = new PointF(90,90);
您需要做的是围绕每个点创建一个 RectF。 RectF越大,点的触摸面积越大。
float sizeOfRect = 5f;
RectF topLeftTouchArea = new RectF(topLeft.x - sizeOfRect, topLeft.y - sizeOfRect, topLeft.x + sizeOfRect, topLeft.y + sizeOfRect);
//Do this for the other points too
定义一些全局变量来跟踪用户在 onTouch 中所做的事情。一个int是被触摸的角,另外四个是角的标识符。
private final int NONE = -1, TOUCH_TOP_LEFT = 0, TOUCH_TOP_RIGHT = 1, TOUCH_BOT_LEFT = 2, TOUCH_BOT_RIGHT = 3;
int currentTouch = NONE;
现在,在您的 onTouch 事件中,您可以像这样检查您的用户正在触摸哪个点:
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
//The user just put their finger down.
//We check to see which corner the user is touching
//And set our global, currentTouch, to the appropriate constant.
case MotionEvent.ACTION_DOWN:
if (topLeftTouchArea.contains(event.getX(), event.getY()) {
currentTouch = TOUCH_TOP_LEFT;
} else if (topRightTouchArea.contains(event.getX(),event.getY()) {
currentTouch = TOUCH_TOP_RIGHT;
} else if (botLeftTouchArea.contains(event.getX(),event.getY()) {
currentTouch = TOUCH_BOT_LEFT;
} else if (botRightTouchArea.contains(event.getX(), event.getY()) {
currentTouch = TOUCH_BOT_RIGHT;
} else {
return false; //Return false if user touches none of the corners
}
return true; //Return true if the user touches one of the corners
//Now we know which corner the user is touching.
//When the user moves their finger, we update the point to the user position and invalidate.
case MotionEvent.ACTION_MOVE:
switch (currentTouch) {
case TOUCH_TOP_LEFT:
topLeft.x = event.getX();
topLeft.y = event.getY();
//The bottom left x position has to move with the top left corner
bottomLeft.x = topLeft.x;
//The top right y position has to move with the top left corner
topRight.y = topLeft.y;
invalidate();
return true;
case TOUCH_TOP_RIGHT:
topRight.x = event.getX();
topRight.y = event.getY();
//The top left y position has to move with the top right corner
topLeft.y = topRight.y;
//The bottom right x position has to move with the top right corner
bottomRight.x = topRight.x;
invalidate();
return true;
case TOUCH_BOT_LEFT:
bottomLeft.x = event.getX();
bottomLeft.y = event.getY();
bottomRight.y = bottomLeft.y;
topLeft.x = bottomLeft.x;
invalidate();
return true;
case TOUCH_BOT_RIGHT:
bottomRight.x = event.getX();
bottomRight.y = event.getY();
topRight.x = bottomRight.x;
bottomLeft.y = bottomRight.y;
invalidate();
return true;
}
//We returned true for all of the above cases, because we used the event
return false; //If currentTouch is none of the above cases, return false
//Here the user lifts up their finger.
//We update the points one last time, and set currentTouch to NONE.
case MotionEvent.ACTION_UP:
switch (currentTouch) {
case TOUCH_TOP_LEFT:
topLeft.x = event.getX();
topLeft.y = event.getY();
//The bottom left x position has to move with the top left corner
bottomLeft.x = topLeft.x;
//The top right y position has to move with the top left corner
topRight.y = topLeft.y;
invalidate();
currentTouch = NONE;
return true;
case TOUCH_TOP_RIGHT:
topRight.x = event.getX();
topRight.y = event.getY();
//The top left y position has to move with the top right corner
topLeft.y = topRight.y;
//The bottom right x position has to move with the top right corner
bottomRight.x = topRight.x;
invalidate();
currentTouch = NONE;
return true;
case TOUCH_BOT_LEFT:
bottomLeft.x = event.getX();
bottomLeft.y = event.getY();
bottomRight.y = bottomLeft.y;
topLeft.x = bottomLeft.x;
invalidate();
currentTouch = NONE;
return true;
case TOUCH_BOT_RIGHT:
bottomRight.x = event.getX();
bottomRight.y = event.getY();
topRight.x = bottomRight.x;
bottomLeft.y = bottomRight.y;
invalidate();
currentTouch = NONE;
return true;
}
return false;
}
}
这样做是围绕您的点创建一个矩形。想象一下在图片中围绕您的点绘制框。这些是由 Rect 对象创建的“触摸板”。矩形的大小由 sizeOfRect 设置。在 onTouchEvent 中,它检查每个矩形对象以查看用户的触摸是否在矩形内部,并向用户发出尝试触摸该点的信号。
关于android - 如何在点之间画线并拉动这些点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10860041/
我有一个包含 5 个选项卡的选项卡栏,每个选项卡都包含一些数据。当用户下拉刷新时,我想显示一个刷新指示器,并且选项卡内的数据应该通过 API 调用进行更新。下面是我到目前为止尝试过的代码。 帮助我了解
我正在尝试使用 CollapsingToolbarLayout 和 AppBarLayout 底部的其他 View 来创建以下行为,但是当我滚动时栏没有折叠/展开/拉动 PullView,这意味着在使
我正在尝试在 React Native 中构建类似于 IMessage 和 WhatsApp 标题的内容,用户可以向下拉以显示标题中的搜索栏。 我已经能够向下拉以显示隐藏的输入,但由于 ScrollV
我希望让我的页面具有响应性,添加了 Bootstrap 并将我的两个 div 排成一行,放在一个流体容器中。 我尝试向左按钮添加向右拉/向右浮动,向右图像向左拉/向左浮动,但它们没有移动。 我没有使用
如何使用简单的饼图从下面的 s:variant 块中获取标题标签? product_variants-96590662 Default Title 10.00 002 0 我已
我已经编写了一些代码,使用 Instagram 图形 API 从 Instagram 中提取图像。一切工作正常,但性能很慢。我正在使用 JavaScript,但也遇到了同样的问题,但使用 PHP 时情
我是一名优秀的程序员,十分优秀!