如何在屏幕上独立移动两个圆圈?
我有一个应用程序可以在屏幕上画两个圆圈。绘制完成后,我可以移动其中一个圆圈并将其放置在我想要的位置。有没有办法确定我触摸了哪个圆圈,以便我可以移动该特定圆圈?目前我只能在 centerX centerY 的坐标处移动圆。
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN: {
if(xyFound == false) {
centreX = (int) ev.getX()-70;
centreY = (int) ev.getY()-70;
xyFound = true;
} else {
centreA = (int) ev.getX()-70;
centreB = (int) ev.getY()-70;
abFound = true;
bothCirclesInPlace = true;
invalidate();
}
}
case MotionEvent.ACTION_MOVE: {
if(xyFound == false){
centreX = (int) ev.getX()-70;
centreY = (int) ev.getY()-70;
xyFound = true;
}else{
centreA = (int) ev.getX()-70;
centreB = (int) ev.getY()-70;
bothCirclesInPlace = true;
invalidate();
} break;
}
。
[更新1]
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN: {
float circ1Val = centreX + centreY;
float circ2Val = centreA + centreB;
float choice1 = circ1Val - (ev.getX() + ev.getY());
float choice2 = circ2Val - (ev.getX() + ev.getY());
float circleToBeMoved = choice1 < choice2 ? ;
。 我不确定计算每个圆圈和触摸事件之间的距离的最佳方法。这是正确的路线吗?或者有更好的方法吗?谢谢
I've an app that paints 2 circles on the screen. once drawn I can move one of the circles around and place where i want. Is there a way to determin which circle I have touched so that I can move that particular circle? At the moment I can only move the circle at co-ords at centreX centreY.
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN: {
if(xyFound == false) {
centreX = (int) ev.getX()-70;
centreY = (int) ev.getY()-70;
xyFound = true;
} else {
centreA = (int) ev.getX()-70;
centreB = (int) ev.getY()-70;
abFound = true;
bothCirclesInPlace = true;
invalidate();
}
}
case MotionEvent.ACTION_MOVE: {
if(xyFound == false){
centreX = (int) ev.getX()-70;
centreY = (int) ev.getY()-70;
xyFound = true;
}else{
centreA = (int) ev.getX()-70;
centreB = (int) ev.getY()-70;
bothCirclesInPlace = true;
invalidate();
} break;
}
.
[update1]
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN: {
float circ1Val = centreX + centreY;
float circ2Val = centreA + centreB;
float choice1 = circ1Val - (ev.getX() + ev.getY());
float choice2 = circ2Val - (ev.getX() + ev.getY());
float circleToBeMoved = choice1 < choice2 ? ;
.
I'm not sure the best way to calculate the distance between each of the circles and the touch event. is this on the correct lines? or is there a better way? thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有以下方法:
在
ACTION_DOWN
上,您确定哪个圆位于触摸点旁边。因此,您可以计算从(centreX,centreY)
到(ev.getX(),ev.getY())
以及从(centreA,centreB)< /code> 到
(ev.getX(),ev.getY())
。如果第一个小于第二个,您将移动第一个圆,否则移动第二个圆(将此选择保存在字段circleToBeMoved
中)。如果两个距离都超出阈值(例如圆的半径),您可能想拒绝任何移动。其次,在
ACTION_MOVE
上仅移动circleToBeMoved
中包含的圆(如果有)。There is the following approach:
On
ACTION_DOWN
you determine which circle lies next to the point touched. Therefore you calculate the distance from(centreX,centreY)
to(ev.getX(),ev.getY())
and from(centreA,centreB)
to(ev.getX(),ev.getY())
. If the first is smaller than the second you'll move the first circle otherwise the second circle (save this choice in a fieldcircleToBeMoved
). Maybe you want to reject any movement if both distances are beyond a threshold (e.g. radius of the circles).Second, on
ACTION_MOVE
only move the circle contained incircleToBeMoved
(if any).