Forum Xamarin.Android

timer freeze

shaywshayw Member ✭✭

Hi
I tried to write an app timer/stopwatch where each digits are shown as seven segments.
minutes secondes tenth of a second and hundredth of a second are displayed.
I have a class that draws the digit .
In the main activity i add a timer which interval is set to 1 milli.
When i run the app using the emulator pixel 2 pie 9.0 api 28 or other emulator the digit of hundredth of a second freeze a little about each 5 seconds.
How can we improve ?
Thanks

Tagged:

Answers

  • YelinzhYelinzh Member, Xamarin Team Xamurai
    edited February 8

    When i run the app using the emulator pixel 2 pie 9.0 api 28 or other emulator the digit of hundredth of a second freeze a little about each 5 seconds.

    How did you create the stopwatch? I created a basic demo about the function, it works fine. Here is the related code, you could refer to:

    Activity class

    public class MainActivity : AppCompatActivity
    {
        Button btnStart, btnPause, btnLap;
        TextView txtTimer;
        LinearLayout container;
        Timer timer;
        int mins = 0, secs = 0, millisecond = 1;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
    
            SetContentView(Resource.Layout.activity_main);
            btnStart = FindViewById<Button>(Resource.Id.btnStart);
            btnPause = FindViewById<Button>(Resource.Id.btnPause);
            btnLap = FindViewById<Button>(Resource.Id.btnLap);
            container = FindViewById<LinearLayout>(Resource.Id.container);
            txtTimer = FindViewById<TextView>(Resource.Id._timer);
            btnStart.Click += delegate
            {
                timer = new Timer();
                timer.Interval = 1; // 1 Milliseconds  
                timer.Elapsed += Timer_Elapsed;
                timer.Start();
            };
            btnPause.Click += delegate
            {
                timer.Stop();
                timer = null;
            };
            btnLap.Click += delegate
            {
                LayoutInflater inflater = (LayoutInflater)BaseContext.GetSystemService(Context.LayoutInflaterService);
                View addView = inflater.Inflate(Resource.Layout.itemView, null);
                TextView textContent = addView.FindViewById<TextView>(Resource.Id.text);
                textContent.Text = txtTimer.Text;
                container.AddView(addView);
            };
        }
        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            millisecond++;
            if (millisecond > 1000)
            {
                secs++;
                millisecond = 0;
            }
            if (secs == 59)
            {
                mins++;
                secs = 0;
            }
            RunOnUiThread(() =>
            {
                txtTimer.Text = string.Format("{0}:{1:00}:{2:000}", mins, secs, millisecond);
            });
        }
    }
    

    activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        android:orientation="vertical"  
        android:layout_width="match_parent"  
        android:layout_height="match_parent">  
        <TextView  
            android:text="0:00:000"  
            android:textAppearance="?android:attr/textAppearanceLarge"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:id="@+id/_timer"  
            android:textSize="40sp"  
            android:layout_gravity="center_horizontal"  
            android:layout_marginTop="20dp"  
            android:layout_marginBottom="10dp" />  
        <LinearLayout  
            android:orientation="horizontal"  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content">  
            <Button  
                android:text="Start"  
                android:layout_width="0dp"  
                android:layout_weight="1"  
                android:layout_height="wrap_content"  
                android:id="@+id/btnStart" />  
            <Button  
                android:text="Pause"  
                android:layout_width="0dp"  
                android:layout_weight="1"  
                android:layout_height="wrap_content"  
                android:id="@+id/btnPause" />  
            <Button  
                android:text="Lap"  
                android:layout_width="0dp"  
                android:layout_weight="1"  
                android:layout_height="wrap_content"  
                android:id="@+id/btnLap" />  
        </LinearLayout>  
          <LinearLayout  
            android:orientation="vertical"  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content">  
            <ScrollView  
                android:layout_width="match_parent"  
                android:layout_height="wrap_content">  
              <LinearLayout      
                android:id="@+id/container"
                android:orientation="vertical"  
                android:layout_width="match_parent"  
                android:layout_height="wrap_content">  
              </LinearLayout>  
            </ScrollView>  
          </LinearLayout>  
    </LinearLayout>
    

    itemView.xml

    <LinearLayout ...>
    
        <TextView
            android:id="@+id/text"
            android:textAppearance="?android:attr/textAppearanceLarge"  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"/>  
    </LinearLayout> 
    

    You could google with the keyword as Xamarin.Android - Stop Watch Seconds And Milliseconds to check the document.


    Xamarin forums are migrating to a new home on Microsoft Q&A!
    We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
    For more information, please refer to this sticky post.

  • shaywshayw Member ✭✭

    I get the same result

    maybe because i draw the digit using drawpath method and paint

  • YelinzhYelinzh Member, Xamarin Team Xamurai

    maybe because i draw the digit using drawpath method and paint

    Will it work well in your project if you just use the TextView to to display the digit? Please test to check that.

  • shaywshayw Member ✭✭

    Yes using textview it works well
    I also wrote the app in uwp using graphic and it works well
    I haven't tried it yet with a real device.

  • YelinzhYelinzh Member, Xamarin Team Xamurai

    maybe because i draw the digit using drawpath method and paint

    The situation seems to be the same as you imagined, drawing the digit may take some time. Try to use async-await to wait for the drawing. If it still doesn't work, you could just use the textview first. Then seek for another way of painting.

Sign In or Register to comment.