Xamarin.Android中不确定的自定义ProgressBar

时间:2018-08-02 11:45:03

标签: c# xamarin xamarin.android

要使用精美的设计和类似于Android原生动画的动画来创建个性化的,不确定的ProgressBar,我找到了解决方案并看上去非常不错:

enter image description here

示例:

  1. 将此代码添加到您的Main.axml文件中:

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#4286f4" android:minWidth="25px" android:minHeight="25px"> <Button android:text="Show progress Bar" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btnShow" /> <ProgressBar android:layout_marginTop="40dp" android:id="@+id/progressBar" style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal" android:layout_width="match_parent" android:layout_height="4dp" android:progressDrawable="@drawable/progress_bar" /> </LinearLayout>


  1. 在Resources / draeable中添加一个名为progress_bar.xml的新xml文件,其中包含:

    <?xml version="1.0" encoding="utf-8" ?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@android:id/background"> <shape> <corners android:radius="5dip" /> <gradient android:startColor="#ffffff" android:centerColor="#ffffff" android:endColor="#ffffff"/> </shape> </item> <item android:id="@android:id/secondaryProgress"> <clip> <shape> <gradient android:startColor="#84b1f9" android:endColor="#84b1f9" android:angle="90" /> </shape> </clip> </item> <item android:id="@android:id/progress"> <clip> <shape> <gradient android:startColor="#ffffff" android:endColor="#ffffff" /> </shape> </clip> </item> </layer-list>


  1. 将以下代码添加到MainActivity中:

    public class MainActivity : AppCompatActivity
    {
        public Button buttonShow;
        ProgressBar progressBar;
        bool StopCondition=true;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
    
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            progressBar = FindViewById<ProgressBar>(Resource.Id.progressBar);
    
            buttonShow = FindViewById<Button>(Resource.Id.btnShow);
    
            buttonShow.Click += ButtonShow_Click;
        }
    
        private void ButtonShow_Click(object sender, System.EventArgs e)
        {
            int progressBarStatus = 0;
    
            ///run thread for increase progress bar
            new Thread(new ThreadStart(delegate {
                while (StopCondition)
                {
                    if (progressBarStatus == 100) progressBarStatus = 0;
    
                    progressBarStatus++;
                    progressBar.Progress =progressBarStatus;
                    progressBar.SecondaryProgress = 2*progressBarStatus;
                        Thread.Sleep(10);//// slep foe 100 ms
    
                }     
            })).Start();
        }
    }
    

您可以在链接中访问源代码:

https://github.com/IonCojucovschi/progressbarXamarin

0 个答案:

没有答案