Search code examples
androidviewondrawontouch

Android: Circle moves to random location when tapping it


I am writing a program that draws a circle at a random location on the phone screen. What it is supposed to do is when the circle is touched, it removes that circle and moves it to another random location.

Currently, when the screen is tapped the old circle is removed, and redrawn at another random location on the screen. It is very close, but I was wondering how I get over this hurdle.

public class Circle extends View {
    private float x = 300;
    private float y = 300;
    private int r = 150;
    private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Random random = new Random();


    // draws circle
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(Color.RED);
        canvas.drawCircle(x, y, r, mPaint);
    }

    // constructors
    public Circle(Context context) {
        super(context);
        init();
    }

    public Circle(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public Circle(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    void init() {
        // might be used later for some data collection
    }


    // gets random number,,
    void generateRandom() {

        int w = getWidth() - r - 50;
        int h = getHeight()- r - 50;

        int border = r + 50;

        this.x = border + random.nextInt(w-border);
        this.y = border + random.nextInt(h-border);
    }



    // when screen is tapped, old circle removed, new circle drawn
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        generateRandom();
        invalidate();
        return super.onTouchEvent(event);
    }
}

Solution

  • Change the code like this ,

    @Override
        public boolean onTouchEvent(MotionEvent event) {
            if (isInsideCircle(event.getX(), event.getY())) {
                generateRandom();
                invalidate();
            }
            return super.onTouchEvent(event);
        }
    
    
        boolean isInsideCircle(float xPoint, float yPoint) {
            float dx = (x - xPoint);
            float dxPow = (float) Math.pow(dx, 2);
            float dy = (y - yPoint);
            float dyPow = (float) Math.pow(dy, 2);
            float radPow = (float) Math.pow(r, 2);
            return (dxPow + dyPow) < radPow || (dxPow + dyPow == radPow);
        }