Search code examples
androidrandomontouch

Android: Draw random circles on screen on touch


I'm working on a project that requires me to draw a circle at a random location on the screen when the user touches the screen. When the touch event occurs it also needs to remove the last circle I drew, so that there can only be one circle on the screen at one time.

I have a class Circle, that I use to create a circle:

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

    public Circle(Context context, float x, float y, int r) {
        super(context);
        mPaint.setColor(0x000000);
        this.x = x;
        this.y = y;
        this.r = r;
    }

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

This is the activity from which a new Circle is created. It will also monitor touch events. Right now it is drawing a circle in the upper left hand corner of the screen, but that code will need to be moved into the onTouch method. I will likely calculate the random number using Random.

public class FingerTappingActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_finger_tapping);


        LinearLayout circle = (LinearLayout) findViewById(R.id.lt);
        View circleView = new Circle(this, 300, 300, 150);
        circleView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        circle.addView(circleView);


        circle.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                return true;
            }
        });

    }
}

I have never worked with touch events before such as these and have no idea how to go about this. Some tips would be greatly appreciated.


Solution

  • Do some thing like this ,

    public class DrawCircles extends View {
    
        private float x = 100;
        private float y = 100;
        private int r = 50;
        private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            mPaint.setStyle(Paint.Style.FILL);
            mPaint.setColor(Color.RED);
            canvas.drawCircle(x, y, r, mPaint);
        }
    
        public DrawCircles(Context context) {
            super(context);
            init();
        }
    
        public DrawCircles(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }
    
        public DrawCircles(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init();
        }
    
        void init() {
            mPaint.setColor(0x000000);
            // logic for random call here;
        }
    
        Random random = new Random();
    
        void generateRandom() {
            int minRadius = 100;
            int w = getWidth();
            int h = getHeight();
            this.x = random.nextInt(w);
            this.y = random.nextInt(h);
            this.r = minRadius + random.nextInt(100);
        }
    
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            generateRandom();
            invalidate();
            return super.onTouchEvent(event);
        }
    }
    

    This view will draw random circles inside the view . make some adjustments for generating x & y coordinates . add this view to xml then it will work.