Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

SurfaceView, drawn objects moves arround

$
0
0

Hello

I am playing around with the surface view. I am trying just to Draw circles at random Places on the screen every second, but the behaviour is very strange. The first couple of times, there is only a single circle that moves, then there is two for a couple of updated (they moves around to), then Theres 3 circles for a couple of updates (Again, no of the circles stays in the same place)

The surface view: public class MySurfaceView : SurfaceView, ISurfaceHolderCallback { Paint painter; ISurfaceHolder surfaceHolder; Color bgColor; Context context;

        int itr;
        Timer timer = null;

        public MySurfaceView (Context context, IAttributeSet attr)
            :base(context, attr)
        {
            this.context = context;
            painter = new Paint ();
            painter.SetARGB (255, 200, 0, 0);
            painter.TextSize = 160;

            surfaceHolder = Holder;
            surfaceHolder.AddCallback (this);

            itr = 0;
            bgColor = new Color (255, 0,255,0);
        }


        Random rnd = new Random ();
        public void SurfaceCreated (ISurfaceHolder holder)
        {
            timer = new Timer (1000);
            timer.AutoReset = true;
            timer.Elapsed += (object sender, ElapsedEventArgs e) => {
                itr++;
                Canvas c = null;
                try{
                    if(surfaceHolder.Surface.IsValid)
                    {
                        c = surfaceHolder.LockCanvas();
                        int x = (int)(rnd.NextDouble()*c.Width);
                        int y = (int)(rnd.NextDouble()*c.Height);

                        lock(surfaceHolder){
                            c.DrawCircle(x, y, 20, painter);
                        }
                    }

                }
                finally
                {
                    if(c != null)
                        surfaceHolder.UnlockCanvasAndPost(c);
                }

            };
            timer.Start ();
        }


        public void SurfaceDestroyed (ISurfaceHolder holder)
        {
            if (timer != null) {
                timer.Stop ();
                timer.Dispose ();
            }
        }
    }

Which is added to my layout like: <SurfaceViewTest.MySurfaceView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/surfaceView1" />

Shouldnt the surface view keep its state, so every second there would just be added a new circle? I have attached the project


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>