2

--> a number of circles will be drawn over a period of time but only one will be shown on screen at a time. --> user will click on the circle when the program registers the click, it will show the next circle. --> the position of each circle is known, only they will appear randomly at different execution.

I have considered a linear layout filled with a lot of imageview(30 of them), every imageview has the same source, a small red dot, generated and stored in the res/drawable folder. code for the dot is:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Large red circle -->
    <item>
        <shape
            android:shape="oval">
            <stroke
                android:width="0dp"
                android:color="#000000" />
            <solid android:color="#FF0000" />
            <size
                android:width="30dp"
                android:height="30dp"/>
        </shape>
    </item>

I am thinking about using isVisible() on these imageviews to hide and show them at different times. this doesn't seem very efficient to me. any suggestions?

VPK
  • 3,010
  • 1
  • 28
  • 35

1 Answers1

0

The best way to achieve that is to create a single custom View with onDraw overridden.

Then, inside onDraw you can simply draw as many circles as you want on the Canvas object. There are thousands of threads here on StackOverflow describing how to do that. E.g. this one.

Remember that you will have to call setWillNotDraw(false) on your View to cause your custom onDraw to be executed.

When the user clicks on the circle, simply record that even and call invalidate(). onDraw will be called once again and you can draw proper number of circles for the updated state.

Community
  • 1
  • 1
Bartek Lipinski
  • 30,698
  • 10
  • 94
  • 132
  • thanks for the answer, i will try it out :) could you please tell me what kind of performance improvement i am getting from using "Canvas" object instead of lots of imageview? – Tahir Islam Mar 15 '17 at 10:48
  • A major one. You're utilising a single relatively light custom view instead of using a ton of `ImageViews`. Which will be much lighter for the device. Additionally you have a single place where you can easily calculate coordinates of circles. Inserting new `Views` in some layout and making sure they are added correctly will be a pain in the ass. – Bartek Lipinski Mar 15 '17 at 10:52
  • I am using canvas.drawCircle to get the circles. but how do i know my circle is clicked ? this "canvas.drawCircle" returns a void so i actually don't have any object to add a listening method. any idea how can i register the user clicks? – Tahir Islam Mar 15 '17 at 20:34
  • Add an `OnTouchListener` that is using a `GestureDetector` to detect clicks. Based on the coordinates of the `MotionEvent` with action `MotionEvent.ACTION_DOWN` you can determine if the click was within a particular circle bounds. – Bartek Lipinski Mar 15 '17 at 21:15
  • is there any special function for finding "action MotionEvent.ACTION_DOWN you can determine if the click was within a particular circle bounds." ? or do i just do a comparison in a IF statement? like if point.x > 250 & point.x<150 ? – Tahir Islam Mar 16 '17 at 11:10
  • You will have to start trying to figure out stuff by yourself, I'm not here to write the code for you. Check `MotionEvent` methods. There are methods `getAction()`, `getX()` and `getY()`. Those three should get you going. – Bartek Lipinski Mar 16 '17 at 11:13