导读:一、首先,我们来看一下效果图,这是新浪微博的Tab滑动效果。我们可以手势滑动,也可以点击上面的头标进行切换。与此同方式,白色横条会移动到相应的页卡头标下。这是一个动画效果,
一、首先,我们来看一下效果图,这是新浪微博的Tab滑动效果。我们可以手势滑动,也可以点击上面的头标进行切换。与此同方式,白色横条会移动到相应的页卡头标下。这是一个动画效果,白条是缓慢滑动过去的。好了,接下来我们就来实现它。

二、在开始前,我们先要认识一个控件,ViewPager。它是google SDk中自带的一个附加包的一个类,可以用来实现屏幕间的切换。这个附加包是android-support-v4。jar,在最后的源码中会提供给大家,在libs文件夹中。当然你也可以自己从网上搜索最新的版本。找到它后,我们需要在项目中添加

三、我们先做界面,
界面设计很简单,第一行三个头标,第二行动画图片,第三行页卡内容展示。
01 | <?xml version="1.0"encoding="utf-8"?> |
02 | <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
03 | xmlns:umadsdk="http://schemas.android.com/apk/res/com.LoveBus" |
04 | android:layout_width="fill_parent" |
05 | android:layout_height="fill_parent" |
06 | android:orientation="vertical"> |
08 | android:id="@+id/linearLayout1" |
09 | android:layout_width="fill_parent" |
10 | android:layout_height="100.0dip" |
11 | android:background="#FFFFFF"> |
13 | android:id="@+id/text1" |
14 | android:layout_width="fill_parent" |
15 | android:layout_height="fill_parent" |
16 | android:layout_weight="1.0" |
17 | android:gravity="center" |
19 | android:textColor="#000000" |
20 | android:textSize="22.0dip"/> |
22 | android:id="@+id/text2" |
23 | android:layout_width="fill_parent" |
24 | android:layout_height="fill_parent" |
25 | android:layout_weight="1.0" |
26 | android:gravity="center" |
28 | android:textColor="#000000" |
29 | android:textSize="22.0dip"/> |
31 | android:id="@+id/text3" |
32 | android:layout_width="fill_parent" |
33 | android:layout_height="fill_parent" |
34 | android:layout_weight="1.0" |
35 | android:gravity="center" |
37 | android:textColor="#000000" |
38 | android:textSize="22.0dip"/> |
41 | android:id="@+id/cursor" |
42 | android:layout_width="fill_parent" |
43 | android:layout_height="wrap_content" |
44 | android:scaleType="matrix" |
45 | android:src="@drawable/a"/> |
46 | <android.support.v4.view.ViewPager |
47 | android:id="@+id/vPager" |
48 | android:layout_width="wrap_content" |
49 | android:layout_height="wrap_content" |
50 | android:layout_gravity="center" |
51 | android:layout_weight="1.0" |
52 | android:background="#000000" |
53 | android:flipInterval="30" |
54 | android:persistentDrawingCache="animation"/> |
我们要展示三个页卡,所以还需要三个页卡内容的界面设计,这里我们只设置了背景颜色,能起到区别作用即可。
1 | <?xml version="1.0"encoding="utf-8"?> |
2 | <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
3 | android:layout_width="fill_parent" |
4 | android:layout_height="fill_parent" |
5 | android:orientation="vertical" |
6 | android:background="#158684"> |
四、代码部分要进行初始化的工作 (1) 先来变量的定义
1 | privateViewPager mPager; |
2 | privateList<View> listViews; |
3 | privateImageView cursor; |
4 | privateTextView t1, t2, t3; |
6 | privateint currIndex = 0; |
(2) 初始化头标
04 | privatevoid InitTextView() { |
05 | t1 = (TextView) findViewById(R.id.text1); |
06 | t2 = (TextView) findViewById(R.id.text2); |
07 | t3 = (TextView) findViewById(R.id.text3); |
08 | t1.setOnClickListener(newMyOnClickListener(0)); |
09 | t2.setOnClickListener(newMyOnClickListener(1)); |
10 | t3.setOnClickListener(newMyOnClickListener(2)); |
04 | publicclass MyOnClickListenerimplements View.OnClickListener { |
06 | publicMyOnClickListener(inti) { |
10 | publicvoid onClick(View v) { |
11 | mPager.setCurrentItem(index); |
相信大家看后都没什么问题,点击第几个,就展示第几个页卡内容。
(3) 初始化页卡内容区
04 | publicclass MyPagerAdapter extends PagerAdapter { |
05 | publicList<View> mListViews; |
06 | publicMyPagerAdapter(List<View> mListViews) { |
07 | this.mListViews = mListViews; |
10 | publicvoid destroyItem(View arg0,int arg1, Object arg2) { |
11 | ((ViewPager) arg0).removeView(mListViews.get(arg1)); |
14 | publicvoid finishUpdate(View arg0) { |
17 | publicint getCount() { |
18 | returnmListViews.size(); |
21 | publicObject instantiateItem(View arg0, intarg1) { |
22 | ((ViewPager) arg0).addView(mListViews.get(arg1),0); |
23 | returnmListViews.get(arg1); |
26 | publicboolean isViewFromObject(View arg0, Object arg1) { |
30 | publicvoid restoreState(Parcelable arg0, ClassLoader arg1) { |
33 | publicParcelable saveState() { |
37 | publicvoid startUpdate(View arg0) { |
这里我们实现了各页卡的装入和卸载 (4) 初始化动画
04 | privatevoid InitImageView() { |
05 | cursor = (ImageView) findViewById(R.id.cursor); |
06 | bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.a) |
08 | DisplayMetrics dm = newDisplayMetrics(); |
09 | getWindowManager().getDefaultDisplay().getMetrics(dm); |
10 | intscreenW = dm.widthPixels; |
11 | offset = (screenW / 3- bmpW) / 2; |
12 | Matrix matrix = newMatrix(); |
13 | matrix.postTranslate(offset, 0); |
14 | cursor.setImageMatrix(matrix); |
根据屏幕的分辨率和图片的宽度计算动画移动的偏移量 /** * 页卡切换监听 */ 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) { } }
五、打完收工,快来看看自己的劳动成果吧