Android: how to prevent an object crossing a line from moving

enter image description here

 @Override protected void onDraw(Canvas canvas) {
                //canvas.drawPicture();
                canvas.drawColor(Color.CYAN);
                Paint p= new Paint();
                p.setColor(Color.RED);
                p.setStrokeWidth(20);
                canvas.drawLine(100, 200, 400, 500, p);
                if(movingObjects.size() > 0){
                for (MovingObject ball : movingObjects) {
                    canvas.drawBitmap(ball.getBitmap(), ball.getX(), ball.getY(), null);
                  }
                }

            }

            public boolean onTouchEvent(MotionEvent event) {
                int eventaction = event.getAction(); 

                int X = (int)event.getX(); 
                int Y = (int)event.getY(); 
                boolean redraw = false;
                switch (eventaction ) { 

                case MotionEvent.ACTION_DOWN:
                    currentMovingObjectId = 0;
                    for (MovingObject movingObject : movingObjects) {

                        int topLeftX = movingObject.getX() ;
                        int topLeftY = movingObject.getY() ;

                        double imageSize = movingObject.getSize();
                        double radCircle  = Math.sqrt( (double) (((topLeftX-X)*(topLeftX-X)) + (topLeftY-Y)*(topLeftY-Y)));

                        if ( (radCircle < imageSize) && 
                                (X- topLeftX) < movingObject.getWidth() && (Y - topLeftY) <  movingObject.getHeight() && 
                                (X- topLeftX) > 0 &&  (Y- topLeftY) > 0){
                            currentMovingObjectId = movingObjects.indexOf(movingObject) + 1;
                            offsetX = X- topLeftX;
                            offsetY = Y - topLeftY;
                            redraw = true;
                            break;
                        }
                      }

                     break; 


                case MotionEvent.ACTION_MOVE:   

                    if ( currentMovingObjectId > 0) {
                        drawAtX = X - offsetX;
                        drawAtY = Y - offsetY;

                        if(drawAtX < 0 || (drawAtX + movingObjects.get(currentMovingObjectId-1).getWidth()) > MovingObject.screenWidth)
                            drawAtX = movingObjects.get(currentMovingObjectId-1).getX();
                        if(drawAtY < 0 || (drawAtY + movingObjects.get(currentMovingObjectId-1).getHeight()) > MovingObject.screenHeight)
                            drawAtY = movingObjects.get(currentMovingObjectId-1).getY();

                        if(drawAtX != movingObjects.get(currentMovingObjectId-1).getX() || drawAtY != movingObjects.get(currentMovingObjectId-1).getY()){
                            movingObjects.get(currentMovingObjectId-1).setX(drawAtX);
                            movingObjects.get(currentMovingObjectId-1).setY(drawAtY);
                            redraw = true;
                        }
                    }

                    break; 

                case MotionEvent.ACTION_UP: 

                     break; 
                } 
                if(redraw)
                    invalidate(); 
                return true; 

            }

The code above describes the onTouch and onDraw method in my custom view class, and I have to prevent the image line from intersecting. How can we implement a matrix in which we can move the object horizontally and vertically, avoiding the object, 4 sides of the cell, but I have to freely move the object inside the puzzle. Any logic would be helpful, I'm stuck. thanks in advance

+3
source share

All Articles