private void rectifyBoundaries(Canvas canvas, Box box) { if(box.getLeft() < strokeWidth){ box.getrect().left = strokeWidth; } if(box.getTop() < strokeWidth){ box.getrect().top = strokeWidth; } if(box.getRight() > canvas.getWidth() - strokeWidth){ box.getrect().right = canvas.getWidth() - strokeWidth; } if(box.getBottom() > canvas.getHeight() - strokeWidth){ box.getrect().bottom = canvas.getHeight() - strokeWidth; } }
@Override protected void onDraw(Canvas canvas) { canvas.drawRect(outBox.getLeft(), outBox.getTop(), outBox.getRight(), outBox.getBottom(), outBox.getPaint()); dirChange = box.bounce(canvas, strokeWidth); rectifyBoundaries(canvas, box); canvas.drawRect(box.getLeft(), box.getTop(), box.getRight(), box.getBottom(), box.getPaint()); handler.postDelayed(r, dirChange ? FRAME_RATE * 20 : FRAME_RATE); }