使用Splash界面


使用Splash界面前,新建一个Activity(注意在清单文件中注册)。并将入口Activity设置到此Activity(因为第一个显示的就是它嘛!),即将

   <intent-filter>
                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>


此段剪切到此Activity的节点下。



A.定义一个布局文件,此文件布局主要为静态,一般不需要业务逻辑控制。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/splash"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/red"
    android:gravity="center_horizontal"
    android:orientation="vertical" >


    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="200dp"
    >


        <ImageView
            android:id="@+id/iv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/easytodo" />


        <TextView
            android:layout_marginTop="10dp"
            android:layout_marginLeft="10dp"
            android:layout_toRightOf="@+id/iv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="我的应用"
            android:textColor="@color/white"
            android:textSize="35sp" />
    </RelativeLayout>
<TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="bottom"
    android:id="@+id/version"
    android:text="版本号"
    />
</LinearLayout>


①一般来说,Splash界面都是全屏的。此处有两种解决办法。



1、在清单文件中将Activity的样式设置为@android:style/Theme.NoTitleBar.Fullscreen




android:name="包名.类名"

android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >

2、另外一种就是在界面中使用代码控制

requestWindowFeature(Window.FEATURE_NO_TITLE);//取消标题栏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//取消状态栏

在Activity中onCreate方法中,setContentView()调用之前加入前面两句。



②在首次打开界面时,如果设置一个特效的话,会给用户一个良好的第一印象。

此处使用了一个两秒钟的渐变特效。

AlphaAnimation animation = new AlphaAnimation(0.5F, 1.0F);
animation.setDuration(2000);
使用此特效的方法很简单。将需要设置特效的控件找到,比如这里为布局文件的根节点一个线性布局设置了一个id。在Activity中找到这个LinearLayout。

LinearLayout layout = (LinearLayout) this.findViewById(R.id.splash);
layout.setAnimation(animation);
③然后使用一个多线程,开启一个新线程执行网络连接操作。在此之前。使用一个工具类NetTools判断网络连接状况。

如果网络连接正常,则执行登录操作。登录操作需要从本地SharedPrefrences中读取以前登录用户的用户名和密码。

user = MyApp.sp.getString("loginName", null);
password = MyApp.sp.getString("passWord", null);
判断是否其数据存在。如果存在的话,执行登录操作。因为在此线程中使用了消息处理器Handler.在需要执行的语句使用looper.prepare()和looper.loop()包裹一下。

RequestParams params = new RequestParams();
params.addBodyParameter("loginName", user);
params.addBodyParameter("passWord", password);
Looper.prepare();
ConnHelper.CreateConn(MyApp.LOGIN, params, handler, 1);
Looper.loop();
④所有消息接收都到handler中处理。

a、如果成功登录。解析返回的数据,将部分内容持久化到SharedPrefrences当中。

editor.putString("loginName",
MyApp.user.getLoginName());
editor.putString("passWord",
MyApp.user.getPassWord());
editor.putString("nickName",
MyApp.user.getNickName());
editor.putString("phone", MyApp.user.getPhone());
editor.putInt("id", MyApp.user.getId());
editor.putInt("groupId", MyApp.user.getGroupId());
editor.putString("groupName",
MyApp.user.getGroupName());
editor.commit();
JPushInterface.setAlias(SplashActivity.this,
MyApp.user.getLoginName(), null);


然后跳转到MainActivity中。

private void LoadMain() {

Intent intent = new Intent(SplashActivity.this, MainActivity.class);
JPushInterface.init(SplashActivity.this);
startActivity(intent);
finish();
}
b、如果登录不成功。则跳转到MainActivity的同时清除保存在SharedPrefrences中的数据。

} else {
// 如果登录不成功,则将SharedPreferences中数据清空
clearSp();
LoadMain();
}









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值