1 import java.util.ArrayList;
2
3 import android.app.Activity;
4 import android.content.SharedPreferences;
5 import android.os.Bundle;
6 import android.support.v4.view.PagerAdapter;
7 import android.support.v4.view.ViewPager;
8 import android.view.LayoutInflater;
9 import android.view.View;
10 import android.view.ViewGroup;
11 import android.view.ViewGroup.LayoutParams;
12
13 //安卓应用欢迎界面标准
14 public class MainActivity extends Activity {
15 private ViewPager viewPager;
16 private ViewPagerAdapter pagerAdapter = new ViewPagerAdapter();
17 // 欢迎界面显示的视图ID
18 private int[] viewsId = { R.layout.wel1, R.layout.wel2, R.layout.wel3 };
19 // 缓冲界面显示的视图ID
20 private int cacheId = R.layout.wel2;
21
22 @Override
23 protected void onCreate(Bundle savedInstanceState) {
24 super.onCreate(savedInstanceState);
25 SharedPreferences shared = this.getSharedPreferences("app_global",
26 MODE_WORLD_WRITEABLE);
27 viewPager = new ViewPager(this);
28 viewPager.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
29 LayoutParams.MATCH_PARENT));
30 if (!shared.contains("used")) {
31 pagerAdapter.setViewsId(viewsId);
32 shared.edit().putBoolean("used", true).commit();
33 } else {
34 pagerAdapter.setViewsId(cacheId);
35 }
36 viewPager.setAdapter(pagerAdapter);
37
38 // 获取最后一个视图,以便绑定事件
39 View v = pagerAdapter.getLastView();
40
41 setContentView(viewPager);
42 }
43
44 // 在其中写入当显示缓冲图片时执行的操作
45 protected void onShowCachePicture() {
46
47 }
48
49 private class ViewPagerAdapter extends PagerAdapter {
50 private ArrayList<View> viewList = new ArrayList<View>();
51
52 @Override
53 public void destroyItem(ViewGroup container, int position, Object object) {
54 container.removeView(viewList.get(position));
55 }
56
57 @Override
58 public Object instantiateItem(ViewGroup container, int position) {
59 container.addView(viewList.get(position));
60 return viewList.get(position);
61 }
62
63 @Override
64 public int getCount() {
65 return viewList.size();
66 }
67
68 @Override
69 public boolean isViewFromObject(View arg0, Object arg1) {
70 return arg0 == arg1;
71 }
72
73 public void setViewsId(int... ids) {
74 LayoutInflater inflater = getLayoutInflater();
75 for (int id : ids) {
76 View v = inflater.inflate(id, null);
77 viewList.add(v);
78 }
79 }
80
81 public View getLastView() {
82 return viewList.get(viewList.size() - 1);
83 }
84 }
85
86 }