前言:
过渡页面 的重要性,相当于你在KFC 排队买吃的, 在排队的过程中,你可能会想 "今天我要吃什么?" .
如果去掉这个过渡的环节, 可能会有些不友好. 二来 ,过渡页面我们可以做一些广告的植入 ..
项目结构
// 原始需求 一个页面加载后一段时间后 ,另外一个页面加载
// 所以首先考虑到的是 AsyncTask 异步线程
// 创建BackgroundLoadable 目的是为了将实例化的过程,延迟到它的实现类中去完成.
过渡页面layout/splash.xml
<?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff" android:gravity="center" android:orientation="vertical" > <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="0.0dip" android:layout_weight="1.0" android:visibility="gone" /> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" android:visibility="gone" /> <LinearLayout android:id="@+id/welcomelayout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:orientation="vertical" > <ImageView android:contentDescription="@string/contentDescription" android:id="@+id/MainActivity_Image" android:layout_width="match_parent" android:layout_height="match_parent" /> <TextView android:visibility="visible" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="@string/welcome" android:textSize="20sp" /> <TextView android:visibility="visible" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="@string/app_name" android:textSize="20sp" /> </LinearLayout> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|center_horizontal" android:layout_marginBottom="10dp" android:text="@string/copyright" android:textAppearance="?android:attr/textAppearanceSmall" > </TextView> </TabHost>
interface BackgroundLoadable
public interface BackgroundLoadable{ public void onPreLoad(); public View loadInBackground(); public void onPostLoad(View view); }
LoadMainInBackgroundTask
public class LoadMainInBackgroundTask extends AsyncTask<Void,Object,View>{ BackgroundLoadable activity; int minLoadTime; public LoadMainInBackgroundTask(BackgroundLoadable activity,int minLoadTime){ this.activity = activity; this.minLoadTime = minLoadTime; } protected void onPreExecute() { activity.onPreLoad(); } @Override protected View doInBackground(Void... params) { // 获取当前时间 .毫秒级 long time1 = System.currentTimeMillis(); View view = activity.loadInBackground(); long time2 = System.currentTimeMillis(); if ((time2 - time1) < minLoadTime) { try { Thread.sleep(minLoadTime - (time2 - time1)); } catch (InterruptedException e) { e.printStackTrace(); } } return view; } @Override protected void onPostExecute(View result) { activity.onPostLoad(result); } }
MainActivity
public class MainActivity extends Activity implements BackgroundLoadable{ protected final int SPLASH_TIME = 2000; private ImageView imageView; private LoadMainInBackgroundTask task; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.splash); setTransitionPage(); task = new LoadMainInBackgroundTask(this, SPLASH_TIME); task.execute(); } private void setTransitionPage(){ imageView = (ImageView) findViewById(R.id.MainActivity_Image); imageView.setImageResource(R.drawable.ic_launcher); } @Override public void onPreLoad() { } @Override public View loadInBackground() { // TODO Auto-generated method stub return getLayoutInflater().inflate(R.layout.activity_main, null); } @Override public void onPostLoad(View view) { // TODO Auto-generated method stub setContentView(view); } }