[android]APP启动界面——SplashActivity

简介:

概念

当前应用程序在启动的时候都会有一个展示自己公司LOGO和APP名字的界面。这个界面成为SplashActivity。


布局

<?

xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff"> <TextView android:id="@+id/copy_right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="12dip" android:layout_centerHorizontal="true" android:layout_alignParentBottom="true" android:text="by xxxxx 出品" android:textSize="11sp"/> <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:layout_centerInParent="true"> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal"> <ImageView android:id="@+id/jay_studio_icon" android:layout_width="110dip" android:layout_height="130dip" android:src="@drawable/ic_launcher"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/jay_studio_icon" android:src="@drawable/ic_launcher"/> </RelativeLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal"> <TextView android:id="@+id/app_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="6dip" android:text="appname" android:textSize="24sp"/> <TextView android:id="@+id/version_name" android:layout_width="wrap_content" android:layout_height="fill_parent" android:gravity="bottom" android:paddingBottom="6dip" android:textSize="14sp"/> </LinearLayout> <View android:layout_width="fill_parent" android:layout_height="1px" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:background="#dddddd"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:padding="6dip" android:text="传承历史" android:textSize="13sp"/> <ProgressBar android:id="@+id/refresh_list_footer_progressbar" android:layout_width="24dip" android:layout_height="24dip" android:layout_gravity="center"> </ProgressBar> </LinearLayout> </RelativeLayout> </RelativeLayout>


用异步任务做数据用作初始化

譬如检查网络。载入本地数据库等等。。


class MyAsyncTask extends AsyncTask<Void, Void, Integer> {

		@Override
		protected Integer doInBackground(Void... arg0) {
			int result;
			//请求数据
			result = loadingCache();
			return result;
		}

		@Override
		protected void onPostExecute(Integer result) {
			super.onPostExecute(result);
			//运行操作
		}

		public int loadingCache() {
			//推断网络
			ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo info = manager.getActiveNetworkInfo();
			if (info == null) {
				return OFFLINE;
			}
			return SUCCESS;
		}
	}

增加动画

可是在运行完。须要跳转到第二个界面,比較好的交互效果,是给Activity的切换时加入动画。
流程:
SplashActivity展示1秒后,跳转到下一个界面(主屏),跳转过程使用动画。

	class MyAsyncTask extends AsyncTask<Void, Void, Integer> {

		@Override
		protected Integer doInBackground(Void... arg0) {
			int result;
			result = loadingCache();
			long startTime = System.currentTimeMillis();
			long loadingTime = System.currentTimeMillis() - startTime;
			if (loadingTime < SHOW_TIME_MIN) {
				try {
					Thread.sleep(SHOW_TIME_MIN - loadingTime);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			return result;
		}

		@Override
		protected void onPostExecute(Integer result) {
			super.onPostExecute(result);
			Intent intent = new Intent(SplashActivity.this, NextActivity.class);
			startActivity(intent);
			finish();
			overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
		}

		public int loadingCache() {
			
			ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo info = manager.getActiveNetworkInfo();
			if (info == null) {
				return OFFLINE;
			}
			return SUCCESS;
		}
	}

初始化SplashActivity


	private static final int FAILURE = 0; // 失败
	private static final int SUCCESS = 1; // 成功
	private static final int OFFLINE = 2; // 假设支持离线阅读,进入离线模式
	private static final int SHOW_TIME_MIN = 2000;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		new MyAsyncTask().execute();
	}





跳转动画


进入

<?

xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false"> <alpha android:fromAlpha="0" android:toAlpha="1" android:duration="2000" /> </set>



离开

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false"> 
    <alpha 
        android:fromAlpha="1"
        android:toAlpha="0"
        android:duration="2000" /> 
</set>

结论:

因为 Application的生命周期比Activity长,利用这个特点。把一些载入数据库。复制数据库文件,读取数据库。而网络数据能够放在异步任务里来完毕。




效果:






本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5097820.html,如需转载请自行联系原作者
相关文章
|
20天前
|
存储 Java API
Android 浅度解析:mk预置AAR、SO文件、APP包和签名
Android 浅度解析:mk预置AAR、SO文件、APP包和签名
77 0
|
3天前
|
移动开发 jenkins 持续交付
jenkins编译H5做的android端编译卫士app记录
jenkins编译H5做的android端编译卫士app记录
|
5天前
|
Android开发
Android APP 隐藏系统软键盘的方法
Android APP 隐藏系统软键盘的方法
11 0
|
5天前
|
Android开发
Android修改默认system/bin/下可执行程序拥有者和权限,使用实例,只有root和系统app权限才能执行某个命令。
Android修改默认system/bin/下可执行程序拥有者和权限,使用实例,只有root和系统app权限才能执行某个命令。
12 0
|
20天前
|
XML Java Android开发
Android系统 添加动态控制屏幕方向、强制APP横竖屏方向
Android系统 添加动态控制屏幕方向、强制APP横竖屏方向
34 1
|
20天前
|
测试技术 Android开发
Android App获取不到pkgInfo信息问题原因
Android App获取不到pkgInfo信息问题原因
19 0
|
26天前
|
Android开发 UED 开发者
解释Android App Bundle是什么,它的优势是什么?
Android App Bundle是Google开发的优化应用分发技术,它打包应用及资源以减少下载大小,加快加载速度,节省用户流量。App Bundle支持离线使用,简化更新过程,提升用户体验。开发人员借此能更高效地构建和分发Android应用。
13 0
|
7月前
|
存储 缓存 安全
Android14 适配之——现有 App 安装到 Android14 手机上需要注意些什么?
Android14 适配之——现有 App 安装到 Android14 手机上需要注意些什么?
288 0
|
5月前
|
传感器 物联网 Android开发
【Android App】物联网中查看手机支持的传感器及实现摇一摇功能-加速度传感器(附源码和演示 超详细)
【Android App】物联网中查看手机支持的传感器及实现摇一摇功能-加速度传感器(附源码和演示 超详细)
74 1
|
5月前
|
Android开发 网络架构
【Android App】检查手机连接WiFi信息以及扫描周围WiFi的讲解及实战(附源码和演示 超详细必看)
【Android App】检查手机连接WiFi信息以及扫描周围WiFi的讲解及实战(附源码和演示 超详细必看)
248 1