如何在屏幕上独立移动两个圆圈?

发布于 2024-12-02 22:36:38 字数 1730 浏览 2 评论 0原文

我有一个应用程序可以在屏幕上画两个圆圈。绘制完成后,我可以移动其中一个圆圈并将其放置在我想要的位置。有没有办法确定我触摸了哪个圆圈,以便我可以移动该特定圆圈?目前我只能在 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 技术交流群。

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

发布评论

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

评论(1

回眸一笑 2024-12-09 22:36:38

有以下方法:

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 field circleToBeMoved). 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 in circleToBeMoved (if any).

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