【问题标题】:how to move 2 circles on the screen independently of each other?如何在屏幕上相互独立地移动 2 个圆圈?
【发布时间】:2011-09-03 12:53:28
【问题描述】:

我有一个在屏幕上绘制 2 个圆圈的应用。绘制后,我可以移动其中一个圆圈并将其放置在我想要的位置。有没有办法确定我触摸了哪个圆圈,以便我可以移动那个特定的圆圈?目前我只能在 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 ? ;

。 我不确定计算每个圆圈与触摸事件之间距离的最佳方法。这是在正确的路线上吗?或者,还有更好的方法?谢谢

【问题讨论】:

    标签: android bitmap touch-event


    【解决方案1】:

    有以下方法:

    ACTION_DOWN 上,您可以确定哪个圆圈位于触摸点旁边。因此,您计算从(centreX,centreY)(ev.getX(),ev.getY()) 以及从(centreA,centreB)(ev.getX(),ev.getY()) 的距离。如果第一个小于第二个,您将移动第一个圆圈,否则移动第二个圆圈(将此选择保存在字段 circleToBeMoved 中)。如果两个距离都超过阈值(例如圆的半径),您可能想拒绝任何移动。

    其次,在ACTION_MOVE 上只移动circleToBeMoved 中包含的圆圈(如果有的话)。

    【讨论】:

    • 嗨,我已经更新了我的帖子。任何进一步的帮助将不胜感激。谢谢
    • @turtleboy 计算距离的正确方法是distance1 = Math.sqrt(Math.pow(ev.getX() - centreX, 2.0) + Math.pow(ev.getY() - centreY, 2.0))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 2019-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多