0

Apologies if this has been asked, but I can't find anything similar.

I'm trying to create a simple game in which a circle appears in a random position, but I'm not entirely sure how to even go about it. This circle also needs to handle an OnClickListener.

Any advice or links to references which might help with this would be greatly appreciated.

Kieran
  • 314
  • 1
  • 4
  • 18

2 Answers2

1

Just use Canvas.drawCircle() method and use Random class to random generate coordinates from specified range, for example:

Random random = new Random();
float x =random.nextFloat() * MAX_X_VALUE;
float y =random.nextFloat() * MAX_Y_VALUE;

Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setStyle(Paint.Style.STROKE);

int RADIUS = 100;

Canvas canvas = new Canvas();
canvas.drawCircle(x, y, RADIUS, paint);

EDIT :

You can get a screen size by:

Display display = getWindowManager().getDefaultDisplay();
Point screenSize= new Point();
display.getSize(screenSize);
int width = size.x;
int height = size.y;
sswierczek
  • 810
  • 1
  • 12
  • 19
0

Generate a random Number for x and y position for your image. Then draw your image bitmap in that position,

To generate random number follow this link.

And for setting your image in position do this,

        canvas.drawBitmap(img, x,y, paint);
Community
  • 1
  • 1
hemantsb
  • 2,049
  • 2
  • 20
  • 29
  • 1
    Thanks. One thing - how do I ensure the generated numbers are within the screen dimensions? Do I just check it against the parent container getHeight() and getWidth()? – Kieran Sep 26 '13 at 13:13
  • 1
    good question, Yes, first get the height and width of the screen and then according generate random number between that limit, Use above link to generate random number between any some specific numbers – hemantsb Sep 27 '13 at 05:31