I want to make three images randomly appear on screen at certain position in android.
And I want to make those images clickable. If you click on a single image which is appeared the counter will be increased by one.
How do I go about it??
I want to make three images randomly appear on screen at certain position in
Share
You can draw an image on a Canvas using following code.
myBitmap is a Bitmap, bitmapFilterSettings is a Paint. Place this code in your
onDraw()method.In order to place the image randomly, you have to randomize the
xandyyou pass to thedst Rect. In order to select a random image, you can put you Bitmaps in aListor an array and use thenextInt(listSize)method of Random. In order to make the image appear and disappear randomly, use thenextBoolean()method ofRandomand only draw the image if it returnstrue. Do not do this too often (once every X frames), or your image will flicker.EDIT : To do this you can declare a counter in the
Activityand the number of frames between the decisions. You will also need abooleanfield to switch drawing on and off. In youronDraw()it could look like this:To make the times between the decisions less predictable you could also randomize the
timeBetweenDecision./EDIT
You can handle the touch event in your listener, calling the random image selection on each click. If you want to make only the image part clickable, check the location of your MotionEvent (you can use the getX() and getY() methods) to lie inside the same Rect you are using to draw you image using it’s
contains(x, y)method.This is not the only way to achieve this, but a quite straightforward one.