Xamarin.Android之Splash的几种简单实现
对现在的APP软件来说,基本上都会有一个Splash页面,类似大家常说的欢迎页面、启动界面之类的。
正常来说这个页面都会有一些相关的信息,比如一些理念,Logo,版本信息等
下面就来看看在Xamarin.Android是如何简单实现的吧。
一、新建一个空白Android项目
二、添加一个layout,splash.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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Splash页面"
android:textSize="50sp"
android:layout_marginTop="150dp"
android:gravity="center" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:id="@+id/tv_version"
android:gravity="center" />
</LinearLayout>
</LinearLayout>
内容比较简单,几个文字和app的版本信息。
三、添加一个Activity ,SplashActivity
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.OS;
using Android.Widget;
using System.Threading;
using System.Threading.Tasks; namespace Catcher.AndroidDemo.SplashDemo
{
[Activity(Label = "SplashActivity", MainLauncher = true,NoHistory =true ,Theme = "@android:style/Theme.Black.NoTitleBar.Fullscreen")]
public class SplashActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.splash);
//version's infomation
var tvVersion = FindViewById<TextView>(Resource.Id.tv_version);
tvVersion.Text ="Version "+ PackageManager.GetPackageInfo(this.PackageName,PackageInfoFlags.MatchAll).VersionName;
//Method 1:
//用过java写Android的应该比较熟悉
new Handler().PostDelayed(() =>
{
Intent intent = new Intent(this, typeof(MainActivity));
StartActivity(intent);
this.Finish();
}, );
//Method 2:
//这种写法只是休眠5秒然后就把这个页面闪现一下就跳转到主页面了
//Thread.Sleep(5000);
//this.StartActivity(typeof(MainActivity));
//this.Finish();
//Method 3:
//这种写法改进了第二种写法的出现的问题
//Thread thread = new Thread(() =>
//{
// Thread.Sleep(5000);
// Intent intent = new Intent(this, typeof(MainActivity));
// StartActivity(intent);
// this.Finish();
//});
//thread.Start();
//Method 4:
//用Task来实现
//Task task = new Task(() =>
//{
// Task.Delay(5000);
//});
//task.ContinueWith(t =>
//{
// StartActivity(new Intent(this, typeof(MainActivity)));
// this.Finish();
//},TaskScheduler.FromCurrentSynchronizationContext());
//task.Start();
}
}
}
在SplashActivity中,写了几种简单的实现。还是从头到尾讲一下吧。
MainLauncher = true 表明我们这个Activity是第一个启动的,同时记得把新建项目生成的MainActivity的这个属性改为false或者去掉
NoHistory =true 见名知意啦~~
Theme = "@android:style/Theme.Black.NoTitleBar.Fullscreen"
这个是主题,默认的有好多好多,为了方便就没有自己去写一个,而是在默认的主题中挑了一个没有标题栏,全屏显示的
可以打开设计页面看看有多少

PackageManager.GetPackageInfo(this.PackageName,PackageInfoFlags.MatchAll).VersionName
这一句是取出app的版本信息。
postdelayed方法有两个重载
public bool PostDelayed(Action action, long delayMillis);
[Register("postDelayed", "(Ljava/lang/Runnable;J)Z", "")]
public bool PostDelayed(IRunnable r, long delayMillis);
第一个是直接用的委托来实现,本文中用的也正是这个方法。
第二个用的是Runnable,具体的写法可参考如下:
new Handler().PostDelayed(new Java.Lang.Runnable(() =>
{
Intent intent = new Intent(this, typeof(MainActivity));
StartActivity(intent);
this.Finish();
}), );
来看看Runnable
[Register("mono/java/lang/Runnable")]
public sealed class Runnable : Object, IRunnable, IJavaObject, IDisposable
{
public Runnable(Action handler);
public void Run();
}
最后我们要用的话还是得传一个委托过去,效果是与文中的方法一一致的。
第二种方法是直接明了,休眠一段时间在出来,不过这个方法貌似用的并不爽,因为无论休眠时间
设置多长,都是在休眠时间快结束时,突然闪现一下Splash页面然后就到主页面了。如果我在启动
页面那里还有动画在展现,那不是很坑。
第三种方法,可以说是在第二种方法的基础上改进的,能达到和其他方法一样的效果。
第四种方法,使用Task来实现。这也是官网示例里面用到的一种方法。不过这个方法得到的效果
不是很理想,Splashd页面出现不到5秒(大概停了2,3秒)就跳转到主页面了。
在这几种方法中也给出了启动单个Activity的多种方法,可以随个人喜好来选择。
最后放一张效果图

示例代码:
https://github.com/hwqdt/Demos/tree/master/src/Catcher.AndroidDemo/Catcher.AndroidDemo.SplashDemo
Xamarin.Android之Splash的几种简单实现的更多相关文章
- Xamarin.Android和UWP之MVVM的简单使用(二)
0x01 前言 前面一篇,Xamarin.Android和UWP之MVVM的简单使用(一),主要讲了MvvmLight的简单使用 这篇主要讲讲MvvmCross的简单使用,例子的话,还是和上篇的一样. ...
- Android传递Bitmap的两种简单方式及其缺陷
Android传递Bitmap的几种简单方式 一,通过Intent的Bundle. 比如有两个activity,A,B,从A进入B.先在A中将Bitmap写进去: Resources res=getR ...
- Xamarin.Android和UWP之MVVM的简单使用(一)
0x01 前言 就目前而言,MVVM可以说是挺流行的,无论是web端还是移动端,web端的主要代表angularjs,avalonjs等, 移动端(xamarin,uwp)的代表应该是mvvmligh ...
- Xamarin Android -创建Splash Screen (一)
......(空话少说) Xamarin 开发的技术资料很少,通过学习,把自己的学习过程及其中碰到的问题与大家分享. Splash Screen 就是在应用程序启动时,出现的一张图片,一般App的Sp ...
- Android: 自定义Tab样式,一种简单的方式。
之前看到过论坛里已经有人发过自定义Tab样式的帖子,感觉有些复杂了,这里分享个简单的方法. 1.制作4个9patch的tab样式,可参考android默认的资源 tab_unselected.9.pn ...
- Xamarin android spinner的使用方法
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- Xamarin Android 中Acitvity如何传递数据
在xamarin android的开发中,activity传递数据非常常见,下面我也来记一下在android中activity之间传递数据的几种方式, Xamarin Android中Activity ...
- Xamarin.Android之引导页的简单制作
0x01 前言 对于现在大部分的APP,第一次打开刚安装或更新安装的APP都会有几个引导界面,通常这几个引导页是告诉用户 APP有些什么功能或者修改了什么bug.新增了什么功能等等等. 下面就用Xam ...
- Xamarin.Android之封装个简单的网络请求类
一.前言 回忆到上篇 <Xamarin.Android再体验之简单的登录Demo> 做登录时,用的是GET的请求,还用的是同步, 于是现在将其简单的改写,做了个简单的封装,包含基于Http ...
随机推荐
- 关于全排列 next_permutation() 函数的用法
这是一个c++函数,包含在头文件<algorithm>里面,下面是基本格式. 1 int a[]; 2 do{ 3 4 }while(next_permutation(a,a+n)); 下 ...
- 循序渐进做项目系列(3):迷你QQ篇(1)——实现客户端互相聊天
<循序渐进做项目系列迷你QQ篇>将陆续介绍客户端聊天,文件传输,加好友,群聊,包括语音聊天,视频聊天,远程桌面等等需求如何实现,感兴趣的朋友可以持续关注.考虑到某些需求较为复杂,本系列采用 ...
- io.js入门(一)—— 初识io.js
io.js可以说是彻底从NodeJS里分离出来的一条分支,其事情始末可以查看这篇报道,此处便也不赘言.既然是分支,io.js便也基本兼容NodeJS的各种API,连执行指令也依旧兼容Node的 nod ...
- jQuery编程的最佳实践
好像是feedly订阅里看到的文章,读完后觉得非常不错,译之备用,多看受益. 加载jQuery 1.坚持使用CDN来加载jQuery,这种别人服务器免费帮你托管文件的便宜干嘛不占呢.点击查看使用CDN ...
- 微冷的雨ASP.NET MVC之葵花宝典(MVC)
微冷的雨ASP.NET MVC之葵花宝典 By:微冷的雨 第一章 ASP.NET MVC的请求和处理机制. 在MVC中: 01.所有的请求都要归结到控制器(Controller)上. 02.约定优于配 ...
- (转)webHttpBinding、basicHttpBinding和wsHttpBinding区别
(1)webHttpBinding与basicHttpBinding / wsHttpBinding的区别: webHttpBinding is the REST-style binding, w ...
- Optimistic Concurrency VS. Pessimistic Concurrency Control
原创地址:http://www.cnblogs.com/jfzhu/p/4009918.html 转载请注明出处 (一)为什么需要并发控制机制 并发控制机制是为了防止多个用户同时更改同一条数据,也 ...
- Elasticsearch Mantanence Lessons Learned Today
Today I troubleshooted an Elasticsearch-cluster-down issue. Several lessons were learned: When many ...
- T型及Fly_by拓扑之应用总结
前面的文章有分别介绍过T型拓扑及Fly_by拓扑结构,这两种拓扑结构应用最多的应该是在DDR3里面,说到这里,小编又想开始聊聊DDR3的设计了,我想很多人都比较有兴趣. 因为DDR3的设计还是比较复杂 ...
- Entity Framework Code First实体关联数据加载
在项目过程中,两个实体数据之间在往往并非完全独立的,而是存在一定的关联关系,如一对一.一对多及多对多等关联.存在关联关系的实体,经常根据一个实体的实例来查询获取与之关联的另外实体的实例. Entity ...