概念
当前应用程序在启动的时候都会有一个展示自己公司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的切换时加入动画。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>
结论:
效果: