一、首先,我们来看一下效果图,这是新浪微博的Tab滑动效果。我们可以手势滑动,也可以点击上面的头标进行切换。与此同方式,
白色横条会移动到相应的页卡头标下。这是一个动画效果,白条是缓慢滑动过去的。好了,接下来我们就来实现它。
二、在开始前,我们先要认识一个控件,ViewPager。它是google SDk中自带的一个附加包的一个类,可以用来实现屏幕间的切换。
这个附加包是android-support-v4.jar,在最后的源码中会提供给大家,在libs文件夹中。当然你也可以自己从网上搜索最新的版本。
找到它后,我们需要在项目中添加
三、我们先做界面,
界面设计很简单,第一行三个头标,第二行动画图片,第三行页卡内容展示。
<span style="color: #0000ff;"><?</span><span style="color: #ff00ff;">xml version="1.0" encoding="utf-8"</span><span style="color: #0000ff;">?></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">LinearLayout </span><span style="color: #ff0000;">xmlns:android</span><span style="color: #0000ff;">="http://schemas.android.com/apk/res/android"</span><span style="color: #ff0000;"> xmlns:umadsdk</span><span style="color: #0000ff;">="http://schemas.android.com/apk/res/com.LoveBus"</span><span style="color: #ff0000;"> android:layout_width</span><span style="color: #0000ff;">="fill_parent"</span><span style="color: #ff0000;"> android:layout_height</span><span style="color: #0000ff;">="fill_parent"</span><span style="color: #ff0000;"> android:orientation</span><span style="color: #0000ff;">="vertical"</span> <span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">LinearLayout </span><span style="color: #ff0000;">android:id</span><span style="color: #0000ff;">="@+id/linearLayout1"</span><span style="color: #ff0000;"> android:layout_width</span><span style="color: #0000ff;">="fill_parent"</span><span style="color: #ff0000;"> android:layout_height</span><span style="color: #0000ff;">="100.0dip"</span><span style="color: #ff0000;"> android:background</span><span style="color: #0000ff;">="#FFFFFF"</span> <span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">TextView </span><span style="color: #ff0000;">android:id</span><span style="color: #0000ff;">="@+id/text1"</span><span style="color: #ff0000;"> android:layout_width</span><span style="color: #0000ff;">="fill_parent"</span><span style="color: #ff0000;"> android:layout_height</span><span style="color: #0000ff;">="fill_parent"</span><span style="color: #ff0000;"> android:layout_weight</span><span style="color: #0000ff;">="1.0"</span><span style="color: #ff0000;"> android:gravity</span><span style="color: #0000ff;">="center"</span><span style="color: #ff0000;"> android:text</span><span style="color: #0000ff;">="页卡1"</span><span style="color: #ff0000;"> android:textColor</span><span style="color: #0000ff;">="#000000"</span><span style="color: #ff0000;"> android:textSize</span><span style="color: #0000ff;">="22.0dip"</span> <span style="color: #0000ff;">/></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">TextView </span><span style="color: #ff0000;">android:id</span><span style="color: #0000ff;">="@+id/text2"</span><span style="color: #ff0000;"> android:layout_width</span><span style="color: #0000ff;">="fill_parent"</span><span style="color: #ff0000;"> android:layout_height</span><span style="color: #0000ff;">="fill_parent"</span><span style="color: #ff0000;"> android:layout_weight</span><span style="color: #0000ff;">="1.0"</span><span style="color: #ff0000;"> android:gravity</span><span style="color: #0000ff;">="center"</span><span style="color: #ff0000;"> android:text</span><span style="color: #0000ff;">="页卡2"</span><span style="color: #ff0000;"> android:textColor</span><span style="color: #0000ff;">="#000000"</span><span style="color: #ff0000;"> android:textSize</span><span style="color: #0000ff;">="22.0dip"</span> <span style="color: #0000ff;">/></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">TextView </span><span style="color: #ff0000;">android:id</span><span style="color: #0000ff;">="@+id/text3"</span><span style="color: #ff0000;"> android:layout_width</span><span style="color: #0000ff;">="fill_parent"</span><span style="color: #ff0000;"> android:layout_height</span><span style="color: #0000ff;">="fill_parent"</span><span style="color: #ff0000;"> android:layout_weight</span><span style="color: #0000ff;">="1.0"</span><span style="color: #ff0000;"> android:gravity</span><span style="color: #0000ff;">="center"</span><span style="color: #ff0000;"> android:text</span><span style="color: #0000ff;">="页卡3"</span><span style="color: #ff0000;"> android:textColor</span><span style="color: #0000ff;">="#000000"</span><span style="color: #ff0000;"> android:textSize</span><span style="color: #0000ff;">="22.0dip"</span> <span style="color: #0000ff;">/></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">LinearLayout</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ImageView </span><span style="color: #ff0000;">android:id</span><span style="color: #0000ff;">="@+id/cursor"</span><span style="color: #ff0000;"> android:layout_width</span><span style="color: #0000ff;">="fill_parent"</span><span style="color: #ff0000;"> android:layout_height</span><span style="color: #0000ff;">="wrap_content"</span><span style="color: #ff0000;"> android:scaleType</span><span style="color: #0000ff;">="matrix"</span><span style="color: #ff0000;"> android:src</span><span style="color: #0000ff;">="@drawable/a"</span> <span style="color: #0000ff;">/></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">android.support.v4.view.ViewPager </span><span style="color: #ff0000;">android:id</span><span style="color: #0000ff;">="@+id/vPager"</span><span style="color: #ff0000;"> android:layout_width</span><span style="color: #0000ff;">="wrap_content"</span><span style="color: #ff0000;"> android:layout_height</span><span style="color: #0000ff;">="wrap_content"</span><span style="color: #ff0000;"> android:layout_gravity</span><span style="color: #0000ff;">="center"</span><span style="color: #ff0000;"> android:layout_weight</span><span style="color: #0000ff;">="1.0"</span><span style="color: #ff0000;"> android:background</span><span style="color: #0000ff;">="#000000"</span><span style="color: #ff0000;"> android:flipInterval</span><span style="color: #0000ff;">="30"</span><span style="color: #ff0000;"> android:persistentDrawingCache</span><span style="color: #0000ff;">="animation"</span> <span style="color: #0000ff;">/></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">LinearLayout</span><span style="color: #0000ff;">></span>
我们要展示三个页卡,所以还需要三个页卡内容的界面设计,这里我们只设置了背景颜色,能起到区别作用即可。
<span style="color: #0000ff;"><?</span><span style="color: #ff00ff;">xml version="1.0" encoding="utf-8"</span><span style="color: #0000ff;">?></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">LinearLayout </span><span style="color: #ff0000;">xmlns:android</span><span style="color: #0000ff;">="http://schemas.android.com/apk/res/android"</span><span style="color: #ff0000;"> android:layout_width</span><span style="color: #0000ff;">="fill_parent"</span><span style="color: #ff0000;"> android:layout_height</span><span style="color: #0000ff;">="fill_parent"</span><span style="color: #ff0000;"> android:orientation</span><span style="color: #0000ff;">="vertical"</span><span style="color: #ff0000;"> android:background</span><span style="color: #0000ff;">="#158684"</span> <span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">LinearLayout</span><span style="color: #0000ff;">></span>
四、代码部分要进行初始化的工作
(1) 先来变量的定义
<span style="color: #0000ff;">private</span> ViewPager mPager;<span style="color: #008000;">//</span><span style="color: #008000;">页卡内容</span><span style="color: #008000;"> </span> <span style="color: #0000ff;">private</span> List<View> listViews; <span style="color: #008000;">//</span><span style="color: #008000;"> Tab页面列表</span><span style="color: #008000;"> </span> <span style="color: #0000ff;">private</span> ImageView cursor;<span style="color: #008000;">//</span><span style="color: #008000;"> 动画图片</span><span style="color: #008000;"> </span> <span style="color: #0000ff;">private</span> TextView t1, t2, t3;<span style="color: #008000;">//</span><span style="color: #008000;"> 页卡头标</span><span style="color: #008000;"> </span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">int</span> offset = 0;<span style="color: #008000;">//</span><span style="color: #008000;"> 动画图片偏移量</span><span style="color: #008000;"> </span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">int</span> currIndex = 0;<span style="color: #008000;">//</span><span style="color: #008000;"> 当前页卡编号</span><span style="color: #008000;"> </span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">int</span> bmpW;<span style="color: #008000;">//</span><span style="color: #008000;"> 动画图片宽度</span>
(2) 初始化头标
<span style="color: #008080;"> 1</span> <span style="color: #008000;">/**</span><span style="color: #008000;"> </span><span style="color: #008080;"> 2</span> <span style="color: #008000;"> * 初始化头标 </span><span style="color: #008080;"> 3</span> <span style="color: #008000;">*/</span> <span style="color: #008080;"> 4</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span> InitTextView() { <span style="color: #008080;"> 5</span> t1 = (TextView) findViewById(R.id.text1); <span style="color: #008080;"> 6</span> t2 = (TextView) findViewById(R.id.text2); <span style="color: #008080;"> 7</span> t3 = (TextView) findViewById(R.id.text3); <span style="color: #008080;"> 8</span> <span style="color: #008080;"> 9</span> t1.setOnClickListener(<span style="color: #0000ff;">new</span> MyOnClickListener(0)); <span style="color: #008080;">10</span> t2.setOnClickListener(<span style="color: #0000ff;">new</span> MyOnClickListener(1)); <span style="color: #008080;">11</span> t3.setOnClickListener(<span style="color: #0000ff;">new</span> MyOnClickListener(2)); <span style="color: #008080;">12</span> }
<span style="color: #008080;"> 1</span> <span style="color: #008000;">/**</span><span style="color: #008000;"> </span><span style="color: #008080;"> 2</span> <span style="color: #008000;"> * 头标点击监听 </span><span style="color: #008080;"> 3</span> <span style="color: #008000;">*/</span> <span style="color: #008080;"> 4</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> MyOnClickListener <span style="color: #0000ff;">implements</span> View.OnClickListener { <span style="color: #008080;"> 5</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">int</span> index = 0; <span style="color: #008080;"> 6</span> <span style="color: #008080;"> 7</span> <span style="color: #0000ff;">public</span> MyOnClickListener(<span style="color: #0000ff;">int</span> i) { <span style="color: #008080;"> 8</span> index = i; <span style="color: #008080;"> 9</span> } <span style="color: #008080;">10</span> <span style="color: #008080;">11</span> @Override <span style="color: #008080;">12</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> onClick(View v) { <span style="color: #008080;">13</span> mPager.setCurrentItem(index); <span style="color: #008080;">14</span> } <span style="color: #008080;">15</span> };
相信大家看后都没什么问题,点击第几个,就展示第几个页卡内容。
(3) 初始化页卡内容区
<span style="color: #008080;"> 1</span> <span style="color: #008000;">/**</span><span style="color: #008000;"> </span><span style="color: #008080;"> 2</span> <span style="color: #008000;"> * 初始化ViewPager </span><span style="color: #008080;"> 3</span> <span style="color: #008000;">*/</span> <span style="color: #008080;"> 4</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span> InitViewPager() { <span style="color: #008080;"> 5</span> mPager = (ViewPager) findViewById(R.id.vPager); <span style="color: #008080;"> 6</span> listViews = <span style="color: #0000ff;">new</span> ArrayList<View>(); <span style="color: #008080;"> 7</span> LayoutInflater mInflater = getLayoutInflater(); <span style="color: #008080;"> 8</span> listViews.add(mInflater.inflate(R.layout.lay1, <span style="color: #0000ff;">null</span>)); <span style="color: #008080;"> 9</span> listViews.add(mInflater.inflate(R.layout.lay2, <span style="color: #0000ff;">null</span>)); <span style="color: #008080;">10</span> listViews.add(mInflater.inflate(R.layout.lay3, <span style="color: #0000ff;">null</span>)); <span style="color: #008080;">11</span> mPager.setAdapter(<span style="color: #0000ff;">new</span> MyPagerAdapter(listViews)); <span style="color: #008080;">12</span> mPager.setCurrentItem(0); <span style="color: #008080;">13</span> mPager.setOnPageChangeListener(<span style="color: #0000ff;">new</span> MyOnPageChangeListener()); <span style="color: #008080;">14</span> }
我们将三个页卡界面装入其中,默认显示第一个页卡。这里我们还需要实现一个适配器。
<span style="color: #008080;"> 1</span> <span style="color: #008000;">/**</span><span style="color: #008000;"> </span><span style="color: #008080;"> 2</span> <span style="color: #008000;"> * ViewPager适配器 </span><span style="color: #008080;"> 3</span> <span style="color: #008000;">*/</span> <span style="color: #008080;"> 4</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> MyPagerAdapter <span style="color: #0000ff;">extends</span> PagerAdapter { <span style="color: #008080;"> 5</span> <span style="color: #0000ff;">public</span> List<View> mListViews; <span style="color: #008080;"> 6</span> <span style="color: #008080;"> 7</span> <span style="color: #0000ff;">public</span> MyPagerAdapter(List<View> mListViews) { <span style="color: #008080;"> 8</span> <span style="color: #0000ff;">this</span>.mListViews = mListViews; <span style="color: #008080;"> 9</span> } <span style="color: #008080;">10</span> <span style="color: #008080;">11</span> @Override <span style="color: #008080;">12</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> destroyItem(View arg0, <span style="color: #0000ff;">int</span> arg1, Object arg2) { <span style="color: #008080;">13</span> ((ViewPager) arg0).removeView(mListViews.get(arg1)); <span style="color: #008080;">14</span> } <span style="color: #008080;">15</span> <span style="color: #008080;">16</span> @Override <span style="color: #008080;">17</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> finishUpdate(View arg0) { <span style="color: #008080;">18</span> } <span style="color: #008080;">19</span> <span style="color: #008080;">20</span> @Override <span style="color: #008080;">21</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> getCount() { <span style="color: #008080;">22</span> <span style="color: #0000ff;">return</span> mListViews.size(); <span style="color: #008080;">23</span> } <span style="color: #008080;">24</span> <span style="color: #008080;">25</span> @Override <span style="color: #008080;">26</span> <span style="color: #0000ff;">public</span> Object instantiateItem(View arg0, <span style="color: #0000ff;">int</span> arg1) { <span style="color: #008080;">27</span> ((ViewPager) arg0).addView(mListViews.get(arg1), 0); <span style="color: #008080;">28</span> <span style="color: #0000ff;">return</span> mListViews.get(arg1); <span style="color: #008080;">29</span> } <span style="color: #008080;">30</span> <span style="color: #008080;">31</span> @Override <span style="color: #008080;">32</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">boolean</span> isViewFromObject(View arg0, Object arg1) { <span style="color: #008080;">33</span> <span style="color: #0000ff;">return</span> arg0 == (arg1); <span style="color: #008080;">34</span> } <span style="color: #008080;">35</span> <span style="color: #008080;">36</span> @Override <span style="color: #008080;">37</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> restoreState(Parcelable arg0, ClassLoader arg1) { <span style="color: #008080;">38</span> } <span style="color: #008080;">39</span> <span style="color: #008080;">40</span> @Override <span style="color: #008080;">41</span> <span style="color: #0000ff;">public</span> Parcelable saveState() { <span style="color: #008080;">42</span> <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">null</span>; <span style="color: #008080;">43</span> } <span style="color: #008080;">44</span> <span style="color: #008080;">45</span> @Override <span style="color: #008080;">46</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> startUpdate(View arg0) { <span style="color: #008080;">47</span> } <span style="color: #008080;">48</span> }
这里我们实现了各页卡的装入和卸载
(3) 初始化动画
<span style="color: #008080;"> 1</span> <span style="color: #008000;">/**</span><span style="color: #008000;"> </span><span style="color: #008080;"> 2</span> <span style="color: #008000;"> * 初始化动画 </span><span style="color: #008080;"> 3</span> <span style="color: #008000;">*/</span> <span style="color: #008080;"> 4</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span> InitImageView() { <span style="color: #008080;"> 5</span> cursor = (ImageView) findViewById(R.id.cursor); <span style="color: #008080;"> 6</span> bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.a) <span style="color: #008080;"> 7</span> .getWidth();<span style="color: #008000;">//</span><span style="color: #008000;"> 获取图片宽度</span><span style="color: #008000;"> </span><span style="color: #008080;"> 8</span> DisplayMetrics dm = <span style="color: #0000ff;">new</span> DisplayMetrics(); <span style="color: #008080;"> 9</span> getWindowManager().getDefaultDisplay().getMetrics(dm); <span style="color: #008080;">10</span> <span style="color: #0000ff;">int</span> screenW = dm.widthPixels;<span style="color: #008000;">//</span><span style="color: #008000;"> 获取分辨率宽度</span><span style="color: #008000;"> </span><span style="color: #008080;">11</span> offset = (screenW / 3 - bmpW) / 2;<span style="color: #008000;">//</span><span style="color: #008000;"> 计算偏移量</span><span style="color: #008000;"> </span><span style="color: #008080;">12</span> Matrix matrix = <span style="color: #0000ff;">new</span> Matrix(); <span style="color: #008080;">13</span> matrix.postTranslate(offset, 0); <span style="color: #008080;">14</span> cursor.setImageMatrix(matrix);<span style="color: #008000;">//</span><span style="color: #008000;"> 设置动画初始位置</span><span style="color: #008000;"> </span><span style="color: #008080;">15</span> }
根据屏幕的分辨率和图片的宽度计算动画移动的偏移量
实现页卡切换监听
<span style="color: #008080;"> 1</span> <span style="color: #008000;">/**</span><span style="color: #008000;"> </span><span style="color: #008080;"> 2</span> <span style="color: #008000;"> * 页卡切换监听 </span><span style="color: #008080;"> 3</span> <span style="color: #008000;">*/</span> <span style="color: #008080;"> 4</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> MyOnPageChangeListener <span style="color: #0000ff;">implements</span> OnPageChangeListener { <span style="color: #008080;"> 5</span> <span style="color: #008080;"> 6</span> <span style="color: #0000ff;">int</span> one = offset * 2 + bmpW;<span style="color: #008000;">//</span><span style="color: #008000;"> 页卡1 -> 页卡2 偏移量</span><span style="color: #008000;"> </span><span style="color: #008080;"> 7</span> <span style="color: #0000ff;">int</span> two = one * 2;<span style="color: #008000;">//</span><span style="color: #008000;"> 页卡1 -> 页卡3 偏移量</span><span style="color: #008000;"> </span><span style="color: #008080;"> 8</span> <span style="color: #008080;"> 9</span> @Override <span style="color: #008080;">10</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> onPageSelected(<span style="color: #0000ff;">int</span> arg0) { <span style="color: #008080;">11</span> Animation animation = <span style="color: #0000ff;">null</span>; <span style="color: #008080;">12</span> <span style="color: #0000ff;">switch</span> (arg0) { <span style="color: #008080;">13</span> <span style="color: #0000ff;">case</span> 0: <span style="color: #008080;">14</span> <span style="color: #0000ff;">if</span> (currIndex == 1) { <span style="color: #008080;">15</span> animation = <span style="color: #0000ff;">new</span> TranslateAnimation(one, 0, 0, 0); <span style="color: #008080;">16</span> } <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> (currIndex == 2) { <span style="color: #008080;">17</span> animation = <span style="color: #0000ff;">new</span> TranslateAnimation(two, 0, 0, 0); <span style="color: #008080;">18</span> } <span style="color: #008080;">19</span> <span style="color: #0000ff;">break</span>; <span style="color: #008080;">20</span> <span style="color: #0000ff;">case</span> 1: <span style="color: #008080;">21</span> <span style="color: #0000ff;">if</span> (currIndex == 0) { <span style="color: #008080;">22</span> animation = <span style="color: #0000ff;">new</span> TranslateAnimation(offset, one, 0, 0); <span style="color: #008080;">23</span> } <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> (currIndex == 2) { <span style="color: #008080;">24</span> animation = <span style="color: #0000ff;">new</span> TranslateAnimation(two, one, 0, 0); <span style="color: #008080;">25</span> } <span style="color: #008080;">26</span> <span style="color: #0000ff;">break</span>; <span style="color: #008080;">27</span> <span style="color: #0000ff;">case</span> 2: <span style="color: #008080;">28</span> <span style="color: #0000ff;">if</span> (currIndex == 0) { <span style="color: #008080;">29</span> animation = <span style="color: #0000ff;">new</span> TranslateAnimation(offset, two, 0, 0); <span style="color: #008080;">30</span> } <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> (currIndex == 1) { <span style="color: #008080;">31</span> animation = <span style="color: #0000ff;">new</span> TranslateAnimation(one, two, 0, 0); <span style="color: #008080;">32</span> } <span style="color: #008080;">33</span> <span style="color: #0000ff;">break</span>; <span style="color: #008080;">34</span> } <span style="color: #008080;">35</span> currIndex = arg0; <span style="color: #008080;">36</span> animation.setFillAfter(<span style="color: #0000ff;">true</span>);<span style="color: #008000;">//</span><span style="color: #008000;"> True:图片停在动画结束位置</span><span style="color: #008000;"> </span><span style="color: #008080;">37</span> animation.setDuration(300); <span style="color: #008080;">38</span> cursor.startAnimation(animation); <span style="color: #008080;">39</span> } <span style="color: #008080;">40</span> <span style="color: #008080;">41</span> @Override <span style="color: #008080;">42</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> onPageScrolled(<span style="color: #0000ff;">int</span> arg0, <span style="color: #0000ff;">float</span> arg1, <span style="color: #0000ff;">int</span> arg2) { <span style="color: #008080;">43</span> } <span style="color: #008080;">44</span> <span style="color: #008080;">45</span> @Override <span style="color: #008080;">46</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> onPageScrollStateChanged(<span style="color: #0000ff;">int</span> arg0) { <span style="color: #008080;">47</span> } <span style="color: #008080;">48</span> }
五、打完收工,快来看看自己的劳动成果吧
源码分享:http://115.com/file/dpi0unyg
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
各大安卓市场专业刷量,QQ346981595