Xamarin.Android之Splash的幾種簡單實現

來源:互聯網
上載者:User

標籤:

對現在的APP軟體來說,基本上都會有一個Splash頁面,類似大家常說的歡迎頁面、啟動介面之類的。

正常來說這個頁面都會有一些相關的資訊,比如一些理念,Logo,版本資訊等

下面就來看看在Xamarin.Android是如何簡單實現的吧。

一、建立一個空白Android項目二、添加一個layout,splash.axml
 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:orientation="vertical" 4     android:layout_width="match_parent" 5     android:layout_height="match_parent"> 6     <TextView 7         android:layout_width="match_parent" 8         android:layout_height="wrap_content" 9         android:text="Splash頁面"10         android:textSize="50sp"11         android:layout_marginTop="150dp"12         android:gravity="center" />13     <LinearLayout14         android:orientation="vertical"15         android:layout_width="match_parent"16         android:layout_height="match_parent"17         android:gravity="bottom">18         <TextView19             android:layout_width="match_parent"20             android:layout_height="wrap_content"21             android:textSize="20sp"22             android:id="@+id/tv_version"23             android:gravity="center" />24     </LinearLayout>25 </LinearLayout>  

 

內容比較簡單,幾個文字和app的版本資訊。

 

三、添加一個Activity ,SplashActivity
 1 using Android.App; 2 using Android.Content; 3 using Android.Content.PM; 4 using Android.OS; 5 using Android.Widget; 6 using System.Threading; 7 using System.Threading.Tasks; 8  9 namespace Catcher.AndroidDemo.SplashDemo10 {11     [Activity(Label = "SplashActivity", MainLauncher = true,NoHistory =true ,Theme = "@android:style/Theme.Black.NoTitleBar.Fullscreen")]12     public class SplashActivity : Activity13     {14         protected override void OnCreate(Bundle savedInstanceState)15         {16             base.OnCreate(savedInstanceState);17             18             SetContentView(Resource.Layout.splash);19             //version‘s infomation20             var tvVersion = FindViewById<TextView>(Resource.Id.tv_version);    21             tvVersion.Text ="Version "+ PackageManager.GetPackageInfo(this.PackageName,PackageInfoFlags.MatchAll).VersionName;22            //Method 1:23             //用過java寫Android的應該比較熟悉24             new Handler().PostDelayed(() =>25             {26                 Intent intent = new Intent(this, typeof(MainActivity));27                 StartActivity(intent);28                 this.Finish();29             }, 5000);30             //Method 2:31             //這種寫法只是休眠5秒然後就把這個頁面閃現一下就跳轉到首頁面了32             //Thread.Sleep(5000);33             //this.StartActivity(typeof(MainActivity));34             //this.Finish();35             //Method 3:36             //這種寫法改進了第二種寫法的出現的問題37             //Thread thread =  new Thread(() => 38             //{39             //    Thread.Sleep(5000);                40             //    Intent intent = new Intent(this, typeof(MainActivity));41             //    StartActivity(intent);42             //    this.Finish();43             //});            44             //thread.Start();45             //Method 4:46             //用Task來實現47             //Task task = new Task(() =>48             //{49             //    Task.Delay(5000);            50             //});51             //task.ContinueWith(t =>52             //{53             //    StartActivity(new Intent(this, typeof(MainActivity)));54             //    this.Finish();55             //},TaskScheduler.FromCurrentSynchronizationContext());56             //task.Start();  57         }58     }59 }  

 

在SplashActivity中,寫了幾種簡單的實現。還是從頭到尾講一下吧。

 MainLauncher = true  表明我們這個Activity是第一個啟動的,同時記得把建立項目產生的MainActivity的這個屬性改為false或者去掉 

 NoHistory =true    見名知意啦~~

 Theme = "@android:style/Theme.Black.NoTitleBar.Fullscreen"  

 這個是主題,預設的有好多好多,為了方便就沒有自己去寫一個,而是在預設的主題中挑了一個沒有標題列,全螢幕顯示的

可以開啟設計頁面看看有多少

 

PackageManager.GetPackageInfo(this.PackageName,PackageInfoFlags.MatchAll).VersionName 

這一句是取出app的版本資訊。

 

第一種方法是通過執行個體化一個Handler對象,調用這個對象的postdelayed方法來實現的。這種方法是比較貼近Java開發的那種寫法。

 

 postdelayed方法有兩個重載

1         public bool PostDelayed(Action action, long delayMillis);2         [Register("postDelayed", "(Ljava/lang/Runnable;J)Z", "")]3         public bool PostDelayed(IRunnable r, long delayMillis);  

 

第一個是直接用的委託來實現,本文中用的也正是這個方法。

第二個用的是Runnable,具體的寫法可參考如下:

1           new Handler().PostDelayed(new Java.Lang.Runnable(() =>2             {3                 Intent intent = new Intent(this, typeof(MainActivity));4                 StartActivity(intent);5                 this.Finish();6             }), 5000);  

 

來看看Runnable

1     [Register("mono/java/lang/Runnable")]2     public sealed class Runnable : Object, IRunnable, IJavaObject, IDisposable3     {4         public Runnable(Action handler);5         public void Run();6     }  

 

最後我們要用的話還是得傳一個委託過去,效果是與文中的方法一一致的。

 

 第二種方法是直接明了,休眠一段時間在出來,不過這個方法貌似用的並不爽,因為無論休眠時間

設定多長,都是在休眠時間快結束時,突然閃現一下Splash頁面然後就到首頁面了。如果我在啟動

頁面那裡還有動畫在展現,那不是很坑。

 

第三種方法,可以說是在第二種方法的基礎上改進的,能達到和其他方法一樣的效果。

 

第四種方法,使用Task來實現。這也是官網樣本裡面用到的一種方法。不過這個方法得到的效果

不是很理想,Splashd頁面出現不到5秒(大概停了2,3秒)就跳轉到首頁面了。

 

在這幾種方法中也給出了啟動單個Activity的多種方法,可以隨個人喜好來選擇。

 

最後放一張

 

 

範例程式碼:

https://github.com/hwqdt/Demos/tree/master/src/Catcher.AndroidDemo/Catcher.AndroidDemo.SplashDemo

 

Xamarin.Android之Splash的幾種簡單實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.