現在的大多數應用都會有一個歡迎引導頁面,

需求分析:
程序安裝后第一次啟動:
啟動頁-->功能引導頁-->應用主頁
以后啟動:
啟動頁-->應用主頁

需求分析:
程序安裝后第一次啟動:
啟動頁-->功能引導頁-->應用主頁
以后啟動:
啟動頁-->應用主頁
實現原理:
用SharedPreferences實現。
創建一個boolean的變量,默認值為true。
當判斷這個變量是true的時候,說明是第一次運行,就跳轉到另一個引導頁面。
引導頁面跳轉到最后一張圖片時,點擊某按鈕發生跳轉事件,回到MainActivity,此時把變量的值改成false。
引導圖效果用ViewPager可以很輕松的實現。
1.布局文件
splash.xml:
1
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2
xmlns:tools="http://schemas.android.com/tools"
3
android:layout_width="match_parent"
4
android:layout_height="match_parent"
5
tools:context=".SplashActivity" >
6
7
<ImageView
8
android:layout_width="match_parent"
9
android:layout_height="match_parent"
10
android:adjustViewBounds="true"
11
android:background="@drawable/welcome_android"
12
android:scaleType="centerCrop" />
13
14
</RelativeLayout>

2

3

4

5

6

7

8

9

10

11

12

13

14

guide.xml:
1
<?xml version="1.0" encoding="utf-8"?>
2
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3
android:layout_width="match_parent"
4
android:layout_height="match_parent"
5
android:orientation="vertical" >
6
7
<android.support.v4.view.ViewPager
8
android:id="@+id/viewpager"
9
android:layout_width="match_parent"
10
android:layout_height="match_parent" />
11
12
<LinearLayout
13
android:id="@+id/ll"
14
android:layout_width="wrap_content"
15
android:layout_height="wrap_content"
16
android:layout_alignParentBottom="true"
17
android:layout_centerHorizontal="true"
18
android:layout_marginBottom="24.0dp"
19
android:orientation="horizontal" >
20
21
<ImageView
22
android:layout_width="wrap_content"
23
android:layout_height="wrap_content"
24
android:layout_gravity="center_vertical"
25
android:clickable="true"
26
android:padding="15.0dip"
27
android:src="@drawable/dot" />
28
29
<ImageView
30
android:layout_width="wrap_content"
31
android:layout_height="wrap_content"
32
android:layout_gravity="center_vertical"
33
android:clickable="true"
34
android:padding="15.0dip"
35
android:src="@drawable/dot" />
36
37
<ImageView
38
android:layout_width="wrap_content"
39
android:layout_height="wrap_content"
40
android:layout_gravity="center_vertical"
41
android:clickable="true"
42
android:padding="15.0dip"
43
android:src="@drawable/dot" />
44
45
<ImageView
46
android:layout_width="wrap_content"
47
android:layout_height="wrap_content"
48
android:layout_gravity="center_vertical"
49
android:clickable="true"
50
android:padding="15.0dip"
51
android:src="@drawable/dot" />
52
</LinearLayout>
53
54
</RelativeLayout>

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

main_activity.xml默認
what_new_one.xml、what_new_two.xml、what_new_three.xml(將圖片名稱改下就行了):
1
<?xml version="1.0" encoding="utf-8"?>
2
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3
android:layout_width="match_parent"
4
android:layout_height="match_parent" >
5
6
<ImageView
7
android:layout_width="match_parent"
8
android:layout_height="match_parent"
9
android:layout_centerInParent="true"
10
android:adjustViewBounds="false"
11
android:focusable="true"
12
android:scaleType="centerCrop"
13
android:background="@drawable/guide_350_01" />
14
15
</RelativeLayout>

2

3

4

5

6

7

8

9

10

11

12

13

14

15

what_new_four.xml:
1
<?xml version="1.0" encoding="utf-8"?>
2
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3
android:layout_width="match_parent"
4
android:layout_height="match_parent" >
5
6
<ImageView
7
android:layout_width="match_parent"
8
android:layout_height="match_parent"
9
android:layout_centerInParent="true"
10
android:adjustViewBounds="false"
11
android:background="@drawable/guide_350_04"
12
android:focusable="true"
13
android:scaleType="centerCrop" />
14
15
<ImageView
16
android:id="@+id/iv_start_weibo"
17
android:layout_width="wrap_content"
18
android:layout_height="wrap_content"
19
android:layout_alignParentBottom="true"
20
android:layout_centerHorizontal="true"
21
android:layout_marginBottom="108dp"
22
android:background="@drawable/whats_new_start_btn"
23
android:focusable="true" />
24
25
</RelativeLayout>

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

MainActivity.java不變
SplashActivity.java:
1
package cn.eoe.leigo.splash;
2
3
import android.app.Activity;
4
import android.content.Intent;
5
import android.content.SharedPreferences;
6
import android.os.Bundle;
7
import android.os.Handler;
8
import android.os.Message;
9
10
/**
11
*
12
* @{# SplashActivity.java Create on 2013-5-2 下午9:10:01
13
*
14
* class desc: 啟動畫面 (1)判斷是否是首次加載應用--采取讀取SharedPreferences的方法
15
* (2)是,則進入GuideActivity;否,則進入MainActivity (3)3s后執行(2)操作
16
*
17
* <p>
18
* Copyright: Copyright(c) 2013
19
* </p>
20
* @Version 1.0
21
* @Author <a href="mailto:gaolei_xj@163.com">Leo</a>
22
*
23
*
24
*/
25
public class SplashActivity extends Activity {
26
boolean isFirstIn = false;
27
28
private static final int GO_HOME = 1000;
29
private static final int GO_GUIDE = 1001;
30
// 延遲3秒
31
private static final long SPLASH_DELAY_MILLIS = 3000;
32
33
private static final String SHAREDPREFERENCES_NAME = "first_pref";
34
35
/**
36
* Handler:跳轉到不同界面
37
*/
38
private Handler mHandler = new Handler() {
39
40
@Override
41
public void handleMessage(Message msg) {
42
switch (msg.what) {
43
case GO_HOME:
44
goHome();
45
break;
46
case GO_GUIDE:
47
goGuide();
48
break;
49
}
50
super.handleMessage(msg);
51
}
52
};
53
54
@Override
55
protected void onCreate(Bundle savedInstanceState) {
56
super.onCreate(savedInstanceState);
57
setContentView(R.layout.splash);
58
59
init();
60
}
61
62
private void init() {
63
// 讀取SharedPreferences中需要的數據
64
// 使用SharedPreferences來記錄程序的使用次數
65
SharedPreferences preferences = getSharedPreferences(
66
SHAREDPREFERENCES_NAME, MODE_PRIVATE);
67
68
// 取得相應的值,如果沒有該值,說明還未寫入,用true作為默認值
69
isFirstIn = preferences.getBoolean("isFirstIn", true);
70
71
// 判斷程序與第幾次運行,如果是第一次運行則跳轉到引導界面,否則跳轉到主界面
72
if (!isFirstIn) {
73
// 使用Handler的postDelayed方法,3秒后執行跳轉到MainActivity
74
mHandler.sendEmptyMessageDelayed(GO_HOME, SPLASH_DELAY_MILLIS);
75
} else {
76
mHandler.sendEmptyMessageDelayed(GO_GUIDE, SPLASH_DELAY_MILLIS);
77
}
78
79
}
80
81
private void goHome() {
82
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
83
SplashActivity.this.startActivity(intent);
84
SplashActivity.this.finish();
85
}
86
87
private void goGuide() {
88
Intent intent = new Intent(SplashActivity.this, GuideActivity.class);
89
SplashActivity.this.startActivity(intent);
90
SplashActivity.this.finish();
91
}
92
}
93

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

GuideActivity.java:
1
package cn.eoe.leigo.splash;
2
3
import java.util.ArrayList;
4
import java.util.List;
5
6
import android.app.Activity;
7
import android.os.Bundle;
8
import android.support.v4.view.ViewPager;
9
import android.support.v4.view.ViewPager.OnPageChangeListener;
10
import android.view.LayoutInflater;
11
import android.view.View;
12
import android.widget.ImageView;
13
import android.widget.LinearLayout;
14
import cn.eoe.leigo.splash.adapter.ViewPagerAdapter;
15
16
/**
17
*
18
* @{# GuideActivity.java Create on 2013-5-2 下午10:59:08
19
*
20
* class desc: 引導界面
21
*
22
* <p>
23
* Copyright: Copyright(c) 2013
24
* </p>
25
* @Version 1.0
26
* @Author <a href="mailto:gaolei_xj@163.com">Leo</a>
27
*
28
*
29
*/
30
public class GuideActivity extends Activity implements OnPageChangeListener {
31
32
private ViewPager vp;
33
private ViewPagerAdapter vpAdapter;
34
private List<View> views;
35
36
// 底部小點圖片
37
private ImageView[] dots;
38
39
// 記錄當前選中位置
40
private int currentIndex;
41
42
@Override
43
protected void onCreate(Bundle savedInstanceState) {
44
super.onCreate(savedInstanceState);
45
setContentView(R.layout.guide);
46
47
// 初始化頁面
48
initViews();
49
50
// 初始化底部小點
51
initDots();
52
}
53
54
private void initViews() {
55
LayoutInflater inflater = LayoutInflater.from(this);
56
57
views = new ArrayList<View>();
58
// 初始化引導圖片列表
59
views.add(inflater.inflate(R.layout.what_new_one, null));
60
views.add(inflater.inflate(R.layout.what_new_two, null));
61
views.add(inflater.inflate(R.layout.what_new_three, null));
62
views.add(inflater.inflate(R.layout.what_new_four, null));
63
64
// 初始化Adapter
65
vpAdapter = new ViewPagerAdapter(views, this);
66
67
vp = (ViewPager) findViewById(R.id.viewpager);
68
vp.setAdapter(vpAdapter);
69
// 綁定回調
70
vp.setOnPageChangeListener(this);
71
}
72
73
private void initDots() {
74
LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
75
76
dots = new ImageView[views.size()];
77
78
// 循環取得小點圖片
79
for (int i = 0; i < views.size(); i++) {
80
dots[i] = (ImageView) ll.getChildAt(i);
81
dots[i].setEnabled(true);// 都設為灰色
82
}
83
84
currentIndex = 0;
85
dots[currentIndex].setEnabled(false);// 設置為白色,即選中狀態
86
}
87
88
private void setCurrentDot(int position) {
89
if (position < 0 || position > views.size() - 1
90
|| currentIndex == position) {
91
return;
92
}
93
94
dots[position].setEnabled(false);
95
dots[currentIndex].setEnabled(true);
96
97
currentIndex = position;
98
}
99
100
// 當滑動狀態改變時調用
101
@Override
102
public void onPageScrollStateChanged(int arg0) {
103
}
104
105
// 當當前頁面被滑動時調用
106
@Override
107
public void onPageScrolled(int arg0, float arg1, int arg2) {
108
}
109
110
// 當新的頁面被選中時調用
111
@Override
112
public void onPageSelected(int arg0) {
113
// 設置底部小點選中狀態
114
setCurrentDot(arg0);
115
}
116
117
}
118

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

ViewPagerAdapter.java:
1
package cn.eoe.leigo.splash.adapter;
2
3
import java.util.List;
4
5
import android.app.Activity;
6
import android.content.Context;
7
import android.content.Intent;
8
import android.content.SharedPreferences;
9
import android.content.SharedPreferences.Editor;
10
import android.os.Parcelable;
11
import android.support.v4.view.PagerAdapter;
12
import android.support.v4.view.ViewPager;
13
import android.view.View;
14
import android.view.View.OnClickListener;
15
import android.widget.ImageView;
16
import cn.eoe.leigo.splash.MainActivity;
17
import cn.eoe.leigo.splash.R;
18
19
/**
20
*
21
* @{# ViewPagerAdapter.java Create on 2013-5-2 下午11:03:39
22
*
23
* class desc: 引導頁面適配器
24
*
25
* <p>
26
* Copyright: Copyright(c) 2013
27
* </p>
28
* @Version 1.0
29
* @Author <a href="mailto:gaolei_xj@163.com">Leo</a>
30
*
31
*
32
*/
33
public class ViewPagerAdapter extends PagerAdapter {
34
35
// 界面列表
36
private List<View> views;
37
private Activity activity;
38
39
private static final String SHAREDPREFERENCES_NAME = "first_pref";
40
41
public ViewPagerAdapter(List<View> views, Activity activity) {
42
this.views = views;
43
this.activity = activity;
44
}
45
46
// 銷毀arg1位置的界面
47
@Override
48
public void destroyItem(View arg0, int arg1, Object arg2) {
49
((ViewPager) arg0).removeView(views.get(arg1));
50
}
51
52
@Override
53
public void finishUpdate(View arg0) {
54
}
55
56
// 獲得當前界面數
57
@Override
58
public int getCount() {
59
if (views != null) {
60
return views.size();
61
}
62
return 0;
63
}
64
65
// 初始化arg1位置的界面
66
@Override
67
public Object instantiateItem(View arg0, int arg1) {
68
((ViewPager) arg0).addView(views.get(arg1), 0);
69
if (arg1 == views.size() - 1) {
70
ImageView mStartWeiboImageButton = (ImageView) arg0
71
.findViewById(R.id.iv_start_weibo);
72
mStartWeiboImageButton.setOnClickListener(new OnClickListener() {
73
74
@Override
75
public void onClick(View v) {
76
// 設置已經引導
77
setGuided();
78
goHome();
79
80
}
81
82
});
83
}
84
return views.get(arg1);
85
}
86
87
private void goHome() {
88
// 跳轉
89
Intent intent = new Intent(activity, MainActivity.class);
90
activity.startActivity(intent);
91
activity.finish();
92
}
93
94
/**
95
*
96
* method desc:設置已經引導過了,下次啟動不用再次引導
97
*/
98
private void setGuided() {
99
SharedPreferences preferences = activity.getSharedPreferences(
100
SHAREDPREFERENCES_NAME, Context.MODE_PRIVATE);
101
Editor editor = preferences.edit();
102
// 存入數據
103
editor.putBoolean("isFirstIn", false);
104
// 提交修改
105
editor.commit();
106
}
107
108
// 判斷是否由對象生成界面
109
@Override
110
public boolean isViewFromObject(View arg0, Object arg1) {
111
return (arg0 == arg1);
112
}
113
114
@Override
115
public void restoreState(Parcelable arg0, ClassLoader arg1) {
116
}
117
118
@Override
119
public Parcelable saveState() {
120
return null;
121
}
122
123
@Override
124
public void startUpdate(View arg0) {
125
}
126
127
}
128

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128
