天天看点

Xamarin.Android之Splash的几种简单实现

Xamarin.Android之Splash的几种简单实现

Xamarin.Android添加Splash页面(启动页,欢迎页)的几种简单实现。

对现在的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     <LinearLayout     14         android:orientation="vertical"     15         android:layout_width="match_parent"     16         android:layout_height="match_parent"     17         android:gravity="bottom">     18         <TextView     19             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.SplashDemo     10 {     11     [Activity(Label = "SplashActivity", MainLauncher = true,NoHistory =true ,Theme = "@android:style/Theme.Black.NoTitleBar.Fullscreen")]     12     public class SplashActivity : Activity     13     {     14         protected override void OnCreate(Bundle savedInstanceState)     15         {     16             base.OnCreate(savedInstanceState);     17                  18             SetContentView(Resource.Layout.splash);     19             //version's infomation     20             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"  

 这个是主题,默认的有好多好多,为了方便就没有自己去写一个,而是在默认的主题中挑了一个没有标题栏,全屏显示的

可以打开设计页面看看有多少

Xamarin.Android之Splash的几种简单实现
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, IDisposable     3     {     4         public Runnable(Action handler);     5         public void Run();     6     }      

最后我们要用的话还是得传一个委托过去,效果是与文中的方法一一致的。

 第二种方法是直接明了,休眠一段时间在出来,不过这个方法貌似用的并不爽,因为无论休眠时间

设置多长,都是在休眠时间快结束时,突然闪现一下Splash页面然后就到主页面了。如果我在启动

页面那里还有动画在展现,那不是很坑。

第三种方法,可以说是在第二种方法的基础上改进的,能达到和其他方法一样的效果。

第四种方法,使用Task来实现。这也是官网示例里面用到的一种方法。不过这个方法得到的效果

不是很理想,Splashd页面出现不到5秒(大概停了2,3秒)就跳转到主页面了。

在这几种方法中也给出了启动单个Activity的多种方法,可以随个人喜好来选择。

最后放一张效果图

Xamarin.Android之Splash的几种简单实现

示例代码:

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

Xamarin.Android之Splash的几种简单实现

如果您认为这篇文章还不错或者有所收获,可以点击右下角的【推荐】按钮,因为你的支持是我继续写作,分享的最大动力!

作者:Catcher Wong ( 黄文清 )

来源:http://catcher1994.cnblogs.com/

声明:

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。如果您发现博客中出现了错误,或者有更好的建议、想法,请及时与我联系!!如果想找我私下交流,可以私信或者加我微信。