开发者

how to move 2 circles on the screen independently of each other?

开发者 https://www.devze.com 2023-04-01 23:48 出处:网络
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

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_MO开发者_如何学JAVAVE: {
                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


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消