半个月前就有这样的想法,做出一个模仿launcher的效果。自己也曾从网上搜过很多资料,也思考过怎么实现,最终还是参考了别人的资料实现了此效果,也解决了我这半个多月的冥思苦想,再次感谢,今天把代码贴出来供大家学习,因为这方面做得比较好的资料缺失比较少(因为本人搜了很多资料都不能达到效果),如果大家觉得还不错,请顶起。
首先自定义一个 ViewGroup:
1. public class MyScrollLayout extends ViewGroup{
2.
3. private VelocityTracker mVelocityTracker; // 用于判断甩动手势
4. SNAP_VELOCITY = 600;
5. private Scroller mScroller; // 滑动控制器
6. private int mCurScreen;
7. mDefaultScreen = 0;
8. private float mLastMotionX;
9. // private int mTouchSlop;
10.
11. private OnViewChangeListener mOnViewChangeListener;
12.
13. public MyScrollLayout(Context context) {
14. super(context);
15. // TODO Auto-generated constructor stub
16. init(context);
17. }
18. public MyScrollLayout(Context context, AttributeSet attrs) {
19. super(context, attrs);
20. // TODO Auto-generated constructor stub
21. init(context);
22. }
23.
24. public MyScrollLayout(Context context, AttributeSet attrs, int defStyle) {
25. super(context, attrs, defStyle);
26. // TODO Auto-generated constructor stub
27. init(context);
28. }
29.
30. private void init(Context context)
31. {
32. mCurScreen = mDefaultScreen;
33. mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
34. mScroller = new Scroller(context);
35.
36. }
37.
38. @Override
39. protected void onLayout(boolean changed, int l, int t, int r, int b) {
40. // TODO Auto-generated method stub
41. if (changed) {
42. childLeft = 0;
43. childCount = getChildCount();
44. i=0; i<childCount; i++) {
45. childView = getChildAt(i);
46. if (childView.getVisibility() != View.GONE) {
47. childWidth = childView.getMeasuredWidth();
48. childView.layout(childLeft, 0,
49. childLeft+childWidth, childView.getMeasuredHeight());
50. childLeft += childWidth;
51. }
52. }
53. }
54. }
55.
56. @Override
57. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
58. // TODO Auto-generated method stub
59. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
60. width = MeasureSpec.getSize(widthMeasureSpec);
61. widthMode = MeasureSpec.getMode(widthMeasureSpec);
62.
63. count = getChildCount();
64. i = 0; i < count; i++) {
65. getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);
66. }
67. scrollTo(mCurScreen * width, 0);
68. }
69.
70. public void snapToDestination() {
71. screenWidth = getWidth();
72. destScreen = (getScrollX()+ screenWidth/2)/screenWidth;
73. snapToScreen(destScreen);
74. }
75.
76. public void snapToScreen(int whichScreen) {
77. // get the valid layout page
78. whichScreen = Math.max(0, Math.min(whichScreen, getChildCount()-1));
79. if (getScrollX() != (whichScreen*getWidth())) {
80. delta = whichScreen*getWidth()-getScrollX();
81. mScroller.startScroll(getScrollX(), 0,
82. delta, 0, Math.abs(delta)*2);
83.
84. mCurScreen = whichScreen;
85. invalidate(); // Redraw the layout
86. if (mOnViewChangeListener != null)
87. {
88. mOnViewChangeListener.OnViewChange(mCurScreen);
89. }
90. }
91. }
92.
93. @Override
94. public void computeScroll() {
95. // TODO Auto-generated method stub
96. if (mScroller.computeScrollOffset()) {
97. scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
98. postInvalidate();
99. }
100. }
101.
102. @Override
103. public boolean onTouchEvent(MotionEvent event) {
104. // TODO Auto-generated method stub
105. action = event.getAction();
106. x = event.getX();
107. y = event.getY();
108.
109. switch (action) {
110. case MotionEvent.ACTION_DOWN:
111. Log.i("", "onTouchEvent ACTION_DOWN");
112. mVelocityTracker == null) {
113. mVelocityTracker = VelocityTracker.obtain();
114. mVelocityTracker.addMovement(event);
115. }
116. if (!mScroller.isFinished()){
117. mScroller.abortAnimation();
118. }
119. mLastMotionX = x;
120. break;
121.
122. case MotionEvent.ACTION_MOVE:
123. deltaX = (int)(mLastMotionX - x);
124. if (IsCanMove(deltaX))
125. {
126. if (mVelocityTracker != null)
127. {
128. mVelocityTracker.addMovement(event);
129. }
130. mLastMotionX = x;
131. scrollBy(deltaX, 0);
132. }
133.
134. break;
135. case MotionEvent.ACTION_UP:
136. velocityX = 0;
137. if (mVelocityTracker != null)
138. {
139. mVelocityTracker.addMovement(event);
140. mVelocityTracker.computeCurrentVelocity(1000);
141. velocityX = (int) mVelocityTracker.getXVelocity();
142. }
143. > SNAP_VELOCITY && mCurScreen > 0) {
144. // Fling enough to move left
145. snapToScreen(mCurScreen - 1);
146. < -SNAP_VELOCITY
147. < getChildCount() - 1) {
148. // Fling enough to move right
149. snapToScreen(mCurScreen + 1);
150. } else {
151. snapToDestination();
152. }
153.
154. if (mVelocityTracker != null) {
155. mVelocityTracker.recycle();
156. mVelocityTracker = null;
157. }
158. mTouchState = TOUCH_STATE_REST;
159. break;
160. }
161. return true;
162. }
163.
164. private boolean IsCanMove(int deltaX)
165. {
166. <= 0 && deltaX < 0 ){
167. return false;
168. }
169. >= (getChildCount() - 1) * getWidth() && deltaX > 0){
170. return false;
171. }
172. return true;
173. }
174.
175. public void SetOnViewChangeListener(OnViewChangeListener listener)
176. {
177. mOnViewChangeListener = listener;
178. }
179. }
1. public class SwitchViewDemoActivity extends Activity implements OnViewChangeListener, OnClickListener{
2. /** Called when the activity is first created. */
3.
4. private MyScrollLayout mScrollLayout;
5. private ImageView[] mImageViews;
6. private int mViewCount;
7. private int mCurSel;
8.
9. @Override
10. public void onCreate(Bundle savedInstanceState) {
11. super.onCreate(savedInstanceState);
12. setContentView(R.layout.main);
13. init();
14.
15. }
16.
17. private void init()
18. {
19. mScrollLayout = (MyScrollLayout) findViewById(R.id.ScrollLayout);
20. linearLayout = (LinearLayout) findViewById(R.id.llayout);
21. //动态添加一个layout控件
22. layout=new LinearLayout(this);
23. layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
24. layout.setBackgroundResource(R.drawable.guide01);
25. mScrollLayout.addView(layout);
26. //动态添加一个imageView控件
27. imageView=new ImageView(this);
28. imageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
29. imageView.setPadding(15, 15, 15, 15);
30. imageView.setImageResource(R.drawable.guide_round);
31. linearLayout.addView(imageView);
32.
33. mViewCount = mScrollLayout.getChildCount();
34. mImageViews = new ImageView[mViewCount];
35. i = 0; i < mViewCount; i++) {
36. mImageViews[i] = (ImageView) linearLayout.getChildAt(i);
37. mImageViews[i].setEnabled(true);
38. mImageViews[i].setOnClickListener(this);
39. mImageViews[i].setTag(i);
40. }
41. mCurSel = 0;
42. mImageViews[mCurSel].setEnabled(false);
43. mScrollLayout.SetOnViewChangeListener(this);
44.
45. }
46.
47. private void setCurPoint(int index)
48. {
49. < 0 || index > mViewCount - 1 || mCurSel == index) {
50. return ;
51. }
52. mImageViews[mCurSel].setEnabled(true);
53. mImageViews[index].setEnabled(false);
54. mCurSel = index;
55. }
56.
57. @Override
58. public void OnViewChange(int view) {
59. // TODO Auto-generated method stub
60. setCurPoint(view);
61. }
62.
63. @Override
64. public void onClick(View v) {
65. // TODO Auto-generated method stub
66. pos = (Integer)(v.getTag());
67. setCurPoint(pos);
68. mScrollLayout.snapToScreen(pos);
69. }
70. }
最后是布局文件 :
1. <?xml version="1.0" encoding="utf-8"?>
2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. android:layout_width="fill_parent"
4. android:layout_height="fill_parent" >
5.
6. <cn.com.karl.scroll.MyScrollLayout
7. xmlns:android="http://schemas.android.com/apk/res/android"
8. android:id="@+id/ScrollLayout"
9. android:layout_width="fill_parent"
10. android:layout_height="fill_parent" >
11.
12. <LinearLayout
13. android:layout_width="fill_parent"
14. android:layout_height="fill_parent"
15. android:background="@drawable/guide01" >
16. </LinearLayout>
17.
18. <LinearLayout
19. android:layout_width="fill_parent"
20. android:layout_height="fill_parent"
21. android:background="@drawable/guide02" >
22. </LinearLayout>
23.
24. <LinearLayout
25. android:layout_width="fill_parent"
26. android:layout_height="fill_parent"
27. android:background="@drawable/guide03" >
28. </LinearLayout>
29.
30. <LinearLayout
31. android:layout_width="fill_parent"
32. android:layout_height="fill_parent"
33. android:background="@drawable/guide04" >
34. </LinearLayout>
35.
36. <LinearLayout
37. android:layout_width="fill_parent"
38. android:layout_height="fill_parent"
39. android:background="@drawable/guide05" >
40. </LinearLayout>
41. </cn.com.karl.scroll.MyScrollLayout>
42.
43. <LinearLayout
44. android:id="@+id/llayout"
45. android:layout_width="wrap_content"
46. android:layout_height="wrap_content"
47. android:layout_alignParentBottom="true"
48. android:layout_centerHorizontal="true"
49. android:layout_marginBottom="24.0dip"
50. android:orientation="horizontal" >
51.
52. <ImageView
53. android:layout_width="wrap_content"
54. android:layout_height="wrap_content"
55. android:layout_gravity="center_vertical"
56. android:clickable="true"
57. android:padding="15.0dip"
58. android:src="@drawable/guide_round" />
59.
60. <ImageView
61. android:layout_width="wrap_content"
62. android:layout_height="wrap_content"
63. android:layout_gravity="center_vertical"
64. android:clickable="true"
65. android:padding="15.0dip"
66. android:src="@drawable/guide_round" />
67.
68. <ImageView
69. android:layout_width="wrap_content"
70. android:layout_height="wrap_content"
71. android:layout_gravity="center_vertical"
72. android:clickable="true"
73. android:padding="15.0dip"
74. android:src="@drawable/guide_round" />
75.
76. <ImageView
77. android:layout_width="wrap_content"
78. android:layout_height="wrap_content"
79. android:layout_gravity="center_vertical"
80. android:clickable="true"
81. android:padding="15.0dip"
82. android:src="@drawable/guide_round" />
83.
84. <ImageView
85. android:layout_width="wrap_content"
86. android:layout_height="wrap_content"
87. android:layout_gravity="center_vertical"
88. android:clickable="true"
89. android:padding="15.0dip"
90. android:src="@drawable/guide_round" />
91. </LinearLayout>
92.
93. </RelativeLayout>
看看运行后的效果到底怎么样吧!
首先自定义一个 ViewGroup:
1. public class MyScrollLayout extends ViewGroup{
2.
3. private VelocityTracker mVelocityTracker; // 用于判断甩动手势
4. SNAP_VELOCITY = 600;
5. private Scroller mScroller; // 滑动控制器
6. private int mCurScreen;
7. mDefaultScreen = 0;
8. private float mLastMotionX;
9. // private int mTouchSlop;
10.
11. private OnViewChangeListener mOnViewChangeListener;
12.
13. public MyScrollLayout(Context context) {
14. super(context);
15. // TODO Auto-generated constructor stub
16. init(context);
17. }
18. public MyScrollLayout(Context context, AttributeSet attrs) {
19. super(context, attrs);
20. // TODO Auto-generated constructor stub
21. init(context);
22. }
23.
24. public MyScrollLayout(Context context, AttributeSet attrs, int defStyle) {
25. super(context, attrs, defStyle);
26. // TODO Auto-generated constructor stub
27. init(context);
28. }
29.
30. private void init(Context context)
31. {
32. mCurScreen = mDefaultScreen;
33. mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
34. mScroller = new Scroller(context);
35.
36. }
37.
38. @Override
39. protected void onLayout(boolean changed, int l, int t, int r, int b) {
40. // TODO Auto-generated method stub
41. if (changed) {
42. childLeft = 0;
43. childCount = getChildCount();
44. i=0; i<childCount; i++) {
45. childView = getChildAt(i);
46. if (childView.getVisibility() != View.GONE) {
47. childWidth = childView.getMeasuredWidth();
48. childView.layout(childLeft, 0,
49. childLeft+childWidth, childView.getMeasuredHeight());
50. childLeft += childWidth;
51. }
52. }
53. }
54. }
55.
56. @Override
57. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
58. // TODO Auto-generated method stub
59. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
60. width = MeasureSpec.getSize(widthMeasureSpec);
61. widthMode = MeasureSpec.getMode(widthMeasureSpec);
62.
63. count = getChildCount();
64. i = 0; i < count; i++) {
65. getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);
66. }
67. scrollTo(mCurScreen * width, 0);
68. }
69.
70. public void snapToDestination() {
71. screenWidth = getWidth();
72. destScreen = (getScrollX()+ screenWidth/2)/screenWidth;
73. snapToScreen(destScreen);
74. }
75.
76. public void snapToScreen(int whichScreen) {
77. // get the valid layout page
78. whichScreen = Math.max(0, Math.min(whichScreen, getChildCount()-1));
79. if (getScrollX() != (whichScreen*getWidth())) {
80. delta = whichScreen*getWidth()-getScrollX();
81. mScroller.startScroll(getScrollX(), 0,
82. delta, 0, Math.abs(delta)*2);
83.
84. mCurScreen = whichScreen;
85. invalidate(); // Redraw the layout
86. if (mOnViewChangeListener != null)
87. {
88. mOnViewChangeListener.OnViewChange(mCurScreen);
89. }
90. }
91. }
92.
93. @Override
94. public void computeScroll() {
95. // TODO Auto-generated method stub
96. if (mScroller.computeScrollOffset()) {
97. scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
98. postInvalidate();
99. }
100. }
101.
102. @Override
103. public boolean onTouchEvent(MotionEvent event) {
104. // TODO Auto-generated method stub
105. action = event.getAction();
106. x = event.getX();
107. y = event.getY();
108.
109. switch (action) {
110. case MotionEvent.ACTION_DOWN:
111. Log.i("", "onTouchEvent ACTION_DOWN");
112. mVelocityTracker == null) {
113. mVelocityTracker = VelocityTracker.obtain();
114. mVelocityTracker.addMovement(event);
115. }
116. if (!mScroller.isFinished()){
117. mScroller.abortAnimation();
118. }
119. mLastMotionX = x;
120. break;
121.
122. case MotionEvent.ACTION_MOVE:
123. deltaX = (int)(mLastMotionX - x);
124. if (IsCanMove(deltaX))
125. {
126. if (mVelocityTracker != null)
127. {
128. mVelocityTracker.addMovement(event);
129. }
130. mLastMotionX = x;
131. scrollBy(deltaX, 0);
132. }
133.
134. break;
135. case MotionEvent.ACTION_UP:
136. velocityX = 0;
137. if (mVelocityTracker != null)
138. {
139. mVelocityTracker.addMovement(event);
140. mVelocityTracker.computeCurrentVelocity(1000);
141. velocityX = (int) mVelocityTracker.getXVelocity();
142. }
143. > SNAP_VELOCITY && mCurScreen > 0) {
144. // Fling enough to move left
145. snapToScreen(mCurScreen - 1);
146. < -SNAP_VELOCITY
147. < getChildCount() - 1) {
148. // Fling enough to move right
149. snapToScreen(mCurScreen + 1);
150. } else {
151. snapToDestination();
152. }
153.
154. if (mVelocityTracker != null) {
155. mVelocityTracker.recycle();
156. mVelocityTracker = null;
157. }
158. mTouchState = TOUCH_STATE_REST;
159. break;
160. }
161. return true;
162. }
163.
164. private boolean IsCanMove(int deltaX)
165. {
166. <= 0 && deltaX < 0 ){
167. return false;
168. }
169. >= (getChildCount() - 1) * getWidth() && deltaX > 0){
170. return false;
171. }
172. return true;
173. }
174.
175. public void SetOnViewChangeListener(OnViewChangeListener listener)
176. {
177. mOnViewChangeListener = listener;
178. }
179. }
1. public class SwitchViewDemoActivity extends Activity implements OnViewChangeListener, OnClickListener{
2. /** Called when the activity is first created. */
3.
4. private MyScrollLayout mScrollLayout;
5. private ImageView[] mImageViews;
6. private int mViewCount;
7. private int mCurSel;
8.
9. @Override
10. public void onCreate(Bundle savedInstanceState) {
11. super.onCreate(savedInstanceState);
12. setContentView(R.layout.main);
13. init();
14.
15. }
16.
17. private void init()
18. {
19. mScrollLayout = (MyScrollLayout) findViewById(R.id.ScrollLayout);
20. linearLayout = (LinearLayout) findViewById(R.id.llayout);
21. //动态添加一个layout控件
22. layout=new LinearLayout(this);
23. layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
24. layout.setBackgroundResource(R.drawable.guide01);
25. mScrollLayout.addView(layout);
26. //动态添加一个imageView控件
27. imageView=new ImageView(this);
28. imageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
29. imageView.setPadding(15, 15, 15, 15);
30. imageView.setImageResource(R.drawable.guide_round);
31. linearLayout.addView(imageView);
32.
33. mViewCount = mScrollLayout.getChildCount();
34. mImageViews = new ImageView[mViewCount];
35. i = 0; i < mViewCount; i++) {
36. mImageViews[i] = (ImageView) linearLayout.getChildAt(i);
37. mImageViews[i].setEnabled(true);
38. mImageViews[i].setOnClickListener(this);
39. mImageViews[i].setTag(i);
40. }
41. mCurSel = 0;
42. mImageViews[mCurSel].setEnabled(false);
43. mScrollLayout.SetOnViewChangeListener(this);
44.
45. }
46.
47. private void setCurPoint(int index)
48. {
49. < 0 || index > mViewCount - 1 || mCurSel == index) {
50. return ;
51. }
52. mImageViews[mCurSel].setEnabled(true);
53. mImageViews[index].setEnabled(false);
54. mCurSel = index;
55. }
56.
57. @Override
58. public void OnViewChange(int view) {
59. // TODO Auto-generated method stub
60. setCurPoint(view);
61. }
62.
63. @Override
64. public void onClick(View v) {
65. // TODO Auto-generated method stub
66. pos = (Integer)(v.getTag());
67. setCurPoint(pos);
68. mScrollLayout.snapToScreen(pos);
69. }
70. }
最后是布局文件 :
1. <?xml version="1.0" encoding="utf-8"?>
2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. android:layout_width="fill_parent"
4. android:layout_height="fill_parent" >
5.
6. <cn.com.karl.scroll.MyScrollLayout
7. xmlns:android="http://schemas.android.com/apk/res/android"
8. android:id="@+id/ScrollLayout"
9. android:layout_width="fill_parent"
10. android:layout_height="fill_parent" >
11.
12. <LinearLayout
13. android:layout_width="fill_parent"
14. android:layout_height="fill_parent"
15. android:background="@drawable/guide01" >
16. </LinearLayout>
17.
18. <LinearLayout
19. android:layout_width="fill_parent"
20. android:layout_height="fill_parent"
21. android:background="@drawable/guide02" >
22. </LinearLayout>
23.
24. <LinearLayout
25. android:layout_width="fill_parent"
26. android:layout_height="fill_parent"
27. android:background="@drawable/guide03" >
28. </LinearLayout>
29.
30. <LinearLayout
31. android:layout_width="fill_parent"
32. android:layout_height="fill_parent"
33. android:background="@drawable/guide04" >
34. </LinearLayout>
35.
36. <LinearLayout
37. android:layout_width="fill_parent"
38. android:layout_height="fill_parent"
39. android:background="@drawable/guide05" >
40. </LinearLayout>
41. </cn.com.karl.scroll.MyScrollLayout>
42.
43. <LinearLayout
44. android:id="@+id/llayout"
45. android:layout_width="wrap_content"
46. android:layout_height="wrap_content"
47. android:layout_alignParentBottom="true"
48. android:layout_centerHorizontal="true"
49. android:layout_marginBottom="24.0dip"
50. android:orientation="horizontal" >
51.
52. <ImageView
53. android:layout_width="wrap_content"
54. android:layout_height="wrap_content"
55. android:layout_gravity="center_vertical"
56. android:clickable="true"
57. android:padding="15.0dip"
58. android:src="@drawable/guide_round" />
59.
60. <ImageView
61. android:layout_width="wrap_content"
62. android:layout_height="wrap_content"
63. android:layout_gravity="center_vertical"
64. android:clickable="true"
65. android:padding="15.0dip"
66. android:src="@drawable/guide_round" />
67.
68. <ImageView
69. android:layout_width="wrap_content"
70. android:layout_height="wrap_content"
71. android:layout_gravity="center_vertical"
72. android:clickable="true"
73. android:padding="15.0dip"
74. android:src="@drawable/guide_round" />
75.
76. <ImageView
77. android:layout_width="wrap_content"
78. android:layout_height="wrap_content"
79. android:layout_gravity="center_vertical"
80. android:clickable="true"
81. android:padding="15.0dip"
82. android:src="@drawable/guide_round" />
83.
84. <ImageView
85. android:layout_width="wrap_content"
86. android:layout_height="wrap_content"
87. android:layout_gravity="center_vertical"
88. android:clickable="true"
89. android:padding="15.0dip"
90. android:src="@drawable/guide_round" />
91. </LinearLayout>
92.
93. </RelativeLayout>
看看运行后的效果到底怎么样吧!