Android系统自带有一个多页面管理的控件:ViewFlipper.
它可以简单实现子页面的切换,,,
它只需使用addView方法添加几个View,每个View对应的是一个页面,即可完成对于多页面的管理,,,
在android上实现手势的识别也比较简单,可以实现OnTouchListener和OnGuestureListener接口,
然后在OnTouch函数中注册GestureDetector来判别手势动作,
参考一位大牛的文章:
http://wang-peng1.iteye.com/blog/572886
GestureDetector.OnGestureListener:用来通知普通的手势事件,该接口有如下六个回调函数:
1. onDown(MotionEvent e):down事件;
2. onSingleTapUp(MotionEvent e):一次点击up事件;
3. onShowPress(MotionEvent e):down事件发生而move或则up还没发生前触发该事件;
4. onLongPress(MotionEvent e):长按事件;
5. onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY):滑动手势事件;
6. onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY):在屏幕上拖动事件。
主要判断是在onFling()函数里面,e1表示开始按下去的位置信息,e2表示抬起时的位置信息,因此可以通过它们在x轴上面 的距离差来是左滑还是右滑。。。
view plaincopy to clipboardprint?
01.public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
02. float velocityY) {
03. // TODO Auto-generated method stub
04. if (e2.getX()-e1.getX() > 100) {
05. // fling right
06. showNextView();
07. } else if (e1.getX() - e2.getX() > 100) {
08. // fling left
09. showPreviousView();
10. }
11. return false;
12.}
资源文件:
view plaincopy to clipboardprint?
01.<?xml version="1.0" encoding="utf-8"?>
02.<LinearLayout
03. xmlns:android="http://schemas.android.com/apk/res/android"
04. android:orientation="vertical"
05. android:layout_width="fill_parent"
06. android:layout_height="fill_parent">
07.<LinearLayout
08. xmlns:android="http://schemas.android.com/apk/res/android"
09. android:orientation="horizontal"
10. android:layout_width="fill_parent"
11. android:layout_height="wrap_content">
12. <Button
13. android:id="@+id/btnPrev"
14. android:text="Previous"
15. android:layout_width="wrap_content"
16. android:layout_height="wrap_content" />
17. <Button
18. android:id="@+id/btnNext"
19. android:text="Next"
20. android:layout_width="wrap_content"
21. android:layout_height="wrap_content" />
22.</LinearLayout>
23.<ViewFlipper
24. android:id="@+id/vfFlingTest"
25. android:layout_width="fill_parent"
26. android:layout_height="fill_parent"></ViewFlipper>
27.</LinearLayout>
代码:
view plaincopy to clipboardprint?
01.package com.sf.learn;
02.import android.app.Activity;
03.import android.os.Bundle;
04.import android.view.GestureDetector;
05.import android.view.KeyEvent;
06.import android.view.MotionEvent;
07.import android.view.View;
08.import android.view.GestureDetector.OnGestureListener;
09.import android.view.View.OnClickListener;
10.import android.view.View.OnTouchListener;
11.import android.view.ViewGroup.LayoutParams;
12.import android.view.animation.AnimationUtils;
13.import android.widget.Button;
14.import android.widget.EditText;
15.import android.widget.ImageView;
16.import android.widget.TextView;
17.import android.widget.ViewFlipper;
18.public class FlingTest extends Activity implements
19. OnTouchListener, OnGestureListener{
20. private Button btnPrev;
21. private Button btnNext;
22. private ViewFlipper vfFlingTest;
23.
24. private TextView tvFlipper;
25. private EditText etFlipper;
26. private ImageView ivFlipper;
27.
28. private GestureDetector mGestureDetector;
29.
30. @Override
31. protected void onCreate(Bundle savedInstanceState) {
32. // TODO Auto-generated method stub
33. super.onCreate(savedInstanceState);
34. setContentView(R.layout.fling_test);
35.
36. btnPrev = (Button)findViewById(R.id.btnPrev);
37. btnNext = (Button)findViewById(R.id.btnNext);
38. vfFlingTest = (ViewFlipper)findViewById(R.id.vfFlingTest);
39.
40. initViews();
41.
42. vfFlingTest.addView(tvFlipper);
43. vfFlingTest.addView(etFlipper);
44. vfFlingTest.addView(ivFlipper);
45.
46. vfFlingTest.setOnTouchListener(this);
47. vfFlingTest.setLongClickable(true);
48. mGestureDetector = new GestureDetector(this);
49.
50. btnPrev.setOnClickListener(new OnClickListener() {
51.
52. public void onClick(View v) {
53. // TODO Auto-generated method stub
54. showPreviousView();
55. }
56. });
57.
58. btnNext.setOnClickListener(new OnClickListener() {
59.
60. public void onClick(View v) {
61. // TODO Auto-generated method stub
62. showNextView();
63. }
64. });
65. }
66.
67. public void showPreviousView() {
68. vfFlingTest.setInAnimation(AnimationUtils.loadAnimation(
69. this, R.anim.slide_right_in));
70. vfFlingTest.setOutAnimation(AnimationUtils.loadAnimation(
71. this, R.anim.slide_left_out));
72. vfFlingTest.showPrevious();
73. }
74.
75. public void showNextView() {
76. vfFlingTest.setInAnimation(AnimationUtils.loadAnimation(
77. this, R.anim.slide_left_in));
78. vfFlingTest.setOutAnimation(AnimationUtils.loadAnimation(
79. this, R.anim.slide_right_out));
80. vfFlingTest.showNext();
81. }
82.
83. private void initViews() {
84. tvFlipper = new TextView(this);
85. tvFlipper.setText("this is a text view!");
86.
87. etFlipper = new EditText(this);
88. etFlipper.setText("this is a text view!");
89.
90. ivFlipper = new ImageView(this);
91. ivFlipper.setLayoutParams(new LayoutParams(
92. LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
93. ivFlipper.setImageResource(R.drawable.pic1);
94. }
95. @Override
96. protected void onDestroy() {
97. // TODO Auto-generated method stub
98. android.os.Process.killProcess(android.os.Process.myPid());
99. super.onDestroy();
100. }
101. @Override
102. public boolean onKeyDown(int keyCode, KeyEvent event) {
103. // TODO Auto-generated method stub
104. if (keyCode == KeyEvent.KEYCODE_BACK) {
105. finish();
106. return true;
107. }
108. return super.onKeyDown(keyCode, event);
109. }
110. public boolean onTouch(View view, MotionEvent event) {
111. // TODO Auto-generated method stub
112. return mGestureDetector.onTouchEvent(event);
113. }
114.
115. public boolean onDown(MotionEvent arg0) {
116. // TODO Auto-generated method stub
117. return false;
118. }
119. public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
120. float velocityY) {
121. // TODO Auto-generated method stub
122. if (e2.getX()-e1.getX() > 100) {
123. // fling right
124. showNextView();
125. } else if (e1.getX() - e2.getX() > 100) {
126. // fling left
127. showPreviousView();
128. }
129. return false;
130. }
131. public void onLongPress(MotionEvent e) {
132. // TODO Auto-generated method stub
133.
134. }
135. public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
136. float distanceY) {
137. // TODO Auto-generated method stub
138. return false;
139. }
140. public void onShowPress(MotionEvent e) {
141. // TODO Auto-generated method stub
142.
143. }
144. public boolean onSingleTapUp(MotionEvent e) {
145. // TODO Auto-generated method stub
146. return false;
147. }
148.}
还有就是一个页面切换的简单动画效果(slide_right_out.xml),
其它的自己扩展,,,
view plaincopy to clipboardprint?
01.<?xml version="1.0" encoding="utf-8"?>
02.<set xmlns:android="http://schemas.android.com/apk/res/android">
03.<translate
04. android:fromXDelta="0%p"
05. android:toXDelta="100%p"
06. android:duration="1000" />
07.</set>
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/Yao_GUET/archive/2011/04/18/6331299.aspx