文章轉自: http://www.itstrike.cn/question/8a6e55b3-9dcf-4cb1-a867-49e1611f804b.html
界面
界面設計很簡單,第一行三個頭標,第二行動畫圖片,第三行頁卡內容展示。
1
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
|
<? xml version = "1.0" encoding = "utf-8" ?> android:layout_width = "fill_parent" android:layout_height = "fill_parent" android:orientation = "vertical" > < LinearLayout android:id = "@+id/linearLayout1" android:layout_width = "fill_parent" android:layout_height = "100.0dip" android:background = "#FFFFFF" > < TextView android:id = "@+id/text1" android:layout_width = "fill_parent" android:layout_height = "fill_parent" android:layout_weight = "1.0" android:gravity = "center" android:text = "頁卡1" android:textColor = "#000000" android:textSize = "22.0dip" /> < TextView android:id = "@+id/text2" android:layout_width = "fill_parent" android:layout_height = "fill_parent" android:layout_weight = "1.0" android:gravity = "center" android:text = "頁卡2" android:textColor = "#000000" android:textSize = "22.0dip" /> < TextView android:id = "@+id/text3" android:layout_width = "fill_parent" android:layout_height = "fill_parent" android:layout_weight = "1.0" android:gravity = "center" android:text = "頁卡3" android:textColor = "#000000" android:textSize = "22.0dip" /> </ LinearLayout > < ImageView android:id = "@+id/cursor" android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:scaleType = "matrix" android:src = "@drawable/a" /> < android.support.v4.view.ViewPager android:id = "@+id/vPager" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_gravity = "center" android:layout_weight = "1.0" android:background = "#000000" android:flipInterval = "30" android:persistentDrawingCache = "animation" /> </ LinearLayout > |
我們要展示三個頁卡,所以還需要三個頁卡內容的界面設計,這裏我們只設置了背景顏色,能起到區別作用即可。
1
2
3
4
5
6
7
|
<? xml version = "1.0" encoding = "utf-8" ?> android:layout_width = "fill_parent" android:layout_height = "fill_parent" android:orientation = "vertical" android:background = "#158684" > </ LinearLayout > |
代碼
(1) 變量的定義
1
2
3
4
5
6
7
|
private ViewPager mPager; //頁卡內容 private List<View> listViews; // Tab頁面列表 private ImageView cursor; // 動畫圖片 private TextView t1, t2, t3; // 頁卡頭標 private int offset = 0 ; // 動畫圖片偏移量 private int currIndex = 0 ; // 當前頁卡編號 private int bmpW; // 動畫圖片寬度 |
(2) 初始化頭標
1
2
3
4
5
6
7
8
9
10
11
12
|
/** * 初始化頭標 */ private void InitTextView() { t1 = (TextView) findViewById(R.id.text1); t2 = (TextView) findViewById(R.id.text2); t3 = (TextView) findViewById(R.id.text3); t1.setOnClickListener( new MyOnClickListener( 0 )); t2.setOnClickListener( new MyOnClickListener( 1 )); t3.setOnClickListener( new MyOnClickListener( 2 )); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
/** * 頭標點擊監聽 */ public class MyOnClickListener implements View.OnClickListener { private int index = 0 ; public MyOnClickListener( int i) { index = i; } @Override public void onClick(View v) { mPager.setCurrentItem(index); } }; |
(3) 初始化頁卡內容區
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
/** * 初始化ViewPager */ private void InitViewPager() { mPager = (ViewPager) findViewById(R.id.vPager); listViews = new ArrayList<View>(); LayoutInflater mInflater = getLayoutInflater(); listViews.add(mInflater.inflate(R.layout.lay1, null )); listViews.add(mInflater.inflate(R.layout.lay2, null )); listViews.add(mInflater.inflate(R.layout.lay3, null )); mPager.setAdapter( new MyPagerAdapter(listViews)); mPager.setCurrentItem( 0 ); mPager.setOnPageChangeListener( new MyOnPageChangeListener()); } |
我們將三個頁卡界面裝入其中,默認顯示第一個頁卡。這裏我們還需要實現一個適配器。
1
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
|
/** * ViewPager適配器 */ public class MyPagerAdapter extends PagerAdapter { public List<View> mListViews; public MyPagerAdapter(List<View> mListViews) { this .mListViews = mListViews; } @Override public void destroyItem(View arg0, int arg1, Object arg2) { ((ViewPager) arg0).removeView(mListViews.get(arg1)); } @Override public void finishUpdate(View arg0) { } @Override public int getCount() { return mListViews.size(); } @Override public Object instantiateItem(View arg0, int arg1) { ((ViewPager) arg0).addView(mListViews.get(arg1), 0 ); return mListViews.get(arg1); } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == (arg1); } @Override public void restoreState(Parcelable arg0, ClassLoader arg1) { } @Override public Parcelable saveState() { return null ; } @Override public void startUpdate(View arg0) { } } |
(4) 初始化動畫
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
/** * 初始化動畫 */ private void InitImageView() { cursor = (ImageView) findViewById(R.id.cursor); bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.a) .getWidth(); // 獲取圖片寬度 DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); int screenW = dm.widthPixels; // 獲取分辨率寬度 offset = (screenW / 3 - bmpW) / 2 ; // 計算偏移量 Matrix matrix = new Matrix(); matrix.postTranslate(offset, 0 ); cursor.setImageMatrix(matrix); // 設置動畫初始位置 } |
根據屏幕的分辨率和圖片的寬度計算動畫移動的偏移量
(5) 實現頁卡切換監聽
1
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
|
/** * 頁卡切換監聽 */ public class MyOnPageChangeListener implements OnPageChangeListener { int one = offset * 2 + bmpW; // 頁卡1 -> 頁卡2 偏移量 int two = one * 2 ; // 頁卡1 -> 頁卡3 偏移量 @Override public void onPageSelected( int arg0) { Animation animation = null ; switch (arg0) { case 0 : if (currIndex == 1 ) { animation = new TranslateAnimation(one, 0 , 0 , 0 ); } else if (currIndex == 2 ) { animation = new TranslateAnimation(two, 0 , 0 , 0 ); } break ; case 1 : if (currIndex == 0 ) { animation = new TranslateAnimation(offset, one, 0 , 0 ); } else if (currIndex == 2 ) { animation = new TranslateAnimation(two, one, 0 , 0 ); } break ; case 2 : if (currIndex == 0 ) { animation = new TranslateAnimation(offset, two, 0 , 0 ); } else if (currIndex == 1 ) { animation = new TranslateAnimation(one, two, 0 , 0 ); } break ; } currIndex = arg0; animation.setFillAfter( true ); // True:圖片停在動畫結束位置 animation.setDuration( 300 ); cursor.startAnimation(animation); } @Override public void onPageScrolled( int arg0, float arg1, int arg2) { } @Override public void onPageScrollStateChanged( int arg0) { } } |
演示