一、首先,我们来看一下效果图,这是新浪微博的Tab滑动效果。我们可以手势滑动,也可以点击上面的头标进行切换。与此同方式,

白色横条会移动到相应的页卡头标下。这是一个动画效果,白条是缓慢滑动过去的。好了,接下来我们就来实现它。

二、在开始前,我们先要认识一个控件,ViewPager。它是google SDk中自带的一个附加包的一个类,可以用来实现屏幕间的切换。

这个附加包是android-support-v4.jar,在最后的源码中会提供给大家,在libs文件夹中。当然你也可以自己从网上搜索最新的版本。

找到它后,我们需要在项目中添加

三、我们先做界面,

界面设计很简单,第一行三个头标,第二行动画图片,第三行页卡内容展示。

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:umadsdk="http://schemas.android.com/apk/res/com.LoveBus"    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>

我们要展示三个页卡,所以还需要三个页卡内容的界面设计,这里我们只设置了背景颜色,能起到区别作用即可。

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical"    android:background="#158684" ></LinearLayout>

四、代码部分要进行初始化的工作

(1) 先来变量的定义

    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     private void InitTextView() { 5         t1 = (TextView) findViewById(R.id.text1); 6         t2 = (TextView) findViewById(R.id.text2); 7         t3 = (TextView) findViewById(R.id.text3); 8  9         t1.setOnClickListener(new MyOnClickListener(0));10         t2.setOnClickListener(new MyOnClickListener(1));11         t3.setOnClickListener(new MyOnClickListener(2));12     }
 1     /** 2      * 头标点击监听 3 */ 4     public class MyOnClickListener implements View.OnClickListener { 5         private int index = 0; 6  7         public MyOnClickListener(int i) { 8             index = i; 9         }10 11         @Override12         public void onClick(View v) {13             mPager.setCurrentItem(index);14         }15     };

相信大家看后都没什么问题,点击第几个,就展示第几个页卡内容。

(3) 初始化页卡内容区

 1     /** 2      * 初始化ViewPager 3 */ 4     private void InitViewPager() { 5         mPager = (ViewPager) findViewById(R.id.vPager); 6         listViews = new ArrayList<View>(); 7         LayoutInflater mInflater = getLayoutInflater(); 8         listViews.add(mInflater.inflate(R.layout.lay1, null)); 9         listViews.add(mInflater.inflate(R.layout.lay2, null));10         listViews.add(mInflater.inflate(R.layout.lay3, null));11         mPager.setAdapter(new MyPagerAdapter(listViews));12         mPager.setCurrentItem(0);13         mPager.setOnPageChangeListener(new MyOnPageChangeListener());14     }

我们将三个页卡界面装入其中,默认显示第一个页卡。这里我们还需要实现一个适配器。

 1 /** 2      * ViewPager适配器 3 */ 4     public class MyPagerAdapter extends PagerAdapter { 5         public List<View> mListViews; 6  7         public MyPagerAdapter(List<View> mListViews) { 8             this.mListViews = mListViews; 9         }10 11         @Override12         public void destroyItem(View arg0, int arg1, Object arg2) {13             ((ViewPager) arg0).removeView(mListViews.get(arg1));14         }15 16         @Override17         public void finishUpdate(View arg0) {18         }19 20         @Override21         public int getCount() {22             return mListViews.size();23         }24 25         @Override26         public Object instantiateItem(View arg0, int arg1) {27             ((ViewPager) arg0).addView(mListViews.get(arg1), 0);28             return mListViews.get(arg1);29         }30 31         @Override32         public boolean isViewFromObject(View arg0, Object arg1) {33             return arg0 == (arg1);34         }35 36         @Override37         public void restoreState(Parcelable arg0, ClassLoader arg1) {38         }39 40         @Override41         public Parcelable saveState() {42             return null;43         }44 45         @Override46         public void startUpdate(View arg0) {47         }48     }

这里我们实现了各页卡的装入和卸载

(3) 初始化动画

 1     /** 2      * 初始化动画 3 */ 4     private void InitImageView() { 5         cursor = (ImageView) findViewById(R.id.cursor); 6         bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.a) 7                 .getWidth();// 获取图片宽度 8         DisplayMetrics dm = new DisplayMetrics(); 9         getWindowManager().getDefaultDisplay().getMetrics(dm);10         int screenW = dm.widthPixels;// 获取分辨率宽度11         offset = (screenW / 3 - bmpW) / 2;// 计算偏移量12         Matrix matrix = new Matrix();13         matrix.postTranslate(offset, 0);14         cursor.setImageMatrix(matrix);// 设置动画初始位置15     }

根据屏幕的分辨率和图片的宽度计算动画移动的偏移量

实现页卡切换监听

 1 /** 2      * 页卡切换监听 3 */ 4     public class MyOnPageChangeListener implements OnPageChangeListener { 5  6         int one = offset * 2 + bmpW;// 页卡1 -> 页卡2 偏移量 7         int two = one * 2;// 页卡1 -> 页卡3 偏移量 8  9         @Override10         public void onPageSelected(int arg0) {11             Animation animation = null;12             switch (arg0) {13             case 0:14                 if (currIndex == 1) {15                     animation = new TranslateAnimation(one, 0, 0, 0);16                 } else if (currIndex == 2) {17                     animation = new TranslateAnimation(two, 0, 0, 0);18                 }19                 break;20             case 1:21                 if (currIndex == 0) {22                     animation = new TranslateAnimation(offset, one, 0, 0);23                 } else if (currIndex == 2) {24                     animation = new TranslateAnimation(two, one, 0, 0);25                 }26                 break;27             case 2:28                 if (currIndex == 0) {29                     animation = new TranslateAnimation(offset, two, 0, 0);30                 } else if (currIndex == 1) {31                     animation = new TranslateAnimation(one, two, 0, 0);32                 }33                 break;34             }35             currIndex = arg0;36             animation.setFillAfter(true);// True:图片停在动画结束位置37             animation.setDuration(300);38             cursor.startAnimation(animation);39         }40 41         @Override42         public void onPageScrolled(int arg0, float arg1, int arg2) {43         }44 45         @Override46         public void onPageScrollStateChanged(int arg0) {47         }48     }

五、打完收工,快来看看自己的劳动成果吧

源码分享:http://115.com/file/dpi0unyg

Android ViewPager多页面滑动切换以及动画效果---换view相关推荐

  1. android切换页面上滑动动画,Android ViewPager多页面滑动切换以及动画效果

    评论 #28楼[楼主] 2012-06-01 14:27D.Winter @孤寒江雪 我猜 要么在头尾各再加入一个页卡 在页卡切换监听中判断,如果选中了头尾的页卡,就返回到相邻的那个页卡.头尾页卡的界 ...

  2. [Android实例] ViewPager多页面滑动切换以及动画效果(精)

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

  3. ViewPager多页面滑动切换以及动画效果

    2019独角兽企业重金招聘Python工程师标准>>> 一.首先,我们来看一下效果图,这是新浪微博的Tab滑动效果.我们可以手势滑动,也可以点击上面的头标进行切换.与此同方式,白色横 ...

  4. Tab页面手势滑动切换以及动画效果

    . 3张页卡之间的切换.带动画效果. 工程结构. 主要应用到android-support-v4.jar这个jar包. 布局文件. 1.main.xml中的代码 [html] <?xml ver ...

  5. react滑动切换tab动画效果_[React Native]react-native-scrollable-tab-view(入门篇)

    官方为我们提供的Tab控制器有两种: TabBarIOS,仅适用于IOS平台 ViewPagerAndroid,仅适用于Android平台(严格来讲并不算,因为我们还需要自己实现Tab) 如果我们需要 ...

  6. Flutter AnimatedSwitcher 实现的滑动切换数字动画效果

    优美的应用体验 来自于细节的处理,更源自于码农的自我要求与努力,当然也需要码农年轻灵活的思维,不局限于思维,不局限语言限制,才是编程的最高境界. 本文章实现的效果如下图所示: 在这里定义一个Timer ...

  7. react滑动切换tab动画效果_Swiper - 免费开源、功能强大的触摸滑动js特效插件

    简单配置就能实现手机.PC 网页中滑动.焦点轮播图.tab 切换和触摸导航等大部分功能. js 滑动特效插件 Swiper 是一款纯 javascript 打造的滑动特效插件,主要用对移动端 web ...

  8. react滑动切换tab动画效果_后端设计中,如何用axure实现table切换动效?

    前段时间做了一个微信小程序的寄卖项目,前端页面这里不过多总结,今天先来说说后端里涉及到的内容.在做项目评审时候,不想局限于静态页面,所以会加些点击效果让产品demo看起来更加有实操性,先看一下Tabl ...

  9. react滑动切换tab动画效果_使用React实现的水平标签(Tab)栏

    JavaScript 语言: JaveScriptBabelCoffeeScript 确定 var data = [ {name: 'Red', value: 'red'}, {name: 'Blue ...

最新文章

  1. java线性表_java实现线性表
  2. android热更新插件,与Android热更新方案Amigo的再次接触
  3. 阿里云容器服务入选云原生边缘「领导力企业TOP3」,推动「原生云边」基础设施标准建立
  4. SAP CS模块拓展项目实施建议书
  5. 网络之DNS协议图解
  6. spring AbstractBeanDefinition创建bean类型是动态代理类的方式
  7. 无聊时分析了下目前国内和国外汽车消费市场的区域性分布
  8. Elasticsearch报错:NodeDisconnectedException[[][IP:9300][cluster:monitor/nodes/liveness] disc
  9. BZOJ2243[SDOI2011] 染色
  10. win7 thinkpad 屏幕旋转 快捷键 与 eclipse冲突
  11. SDUT 第十届校赛H menhera酱那惨不忍睹的数学 【二分图 || 网络流】
  12. 曲线拟合(多项式函数+MATLAB实例)
  13. 蝴蝶效应、青蛙现象、鳄鱼法则、鲇鱼效应…… 好多新名词 :)
  14. 区块链技术及其在信息安全领域的研究进展 简单概括
  15. 在Solaris下自动启动oracle|Sybase
  16. linux lsiutil raid创建,使用Lsiutil在線管理Dell(R410,R610等)內置陣列卡SAS 6i/R
  17. Nexus 7 二代 挂在U盘成功
  18. 微博为何做绿洲?社交化、生活化和垂直化
  19. 树莓派下DS18B20获取实时温度
  20. struts2+spring3+hibernate4

热门文章

  1. 【C 语言】动态库封装与设计 ( Windows 动态库简介 | Visual Studio 调用动态库 )
  2. 【Windows 逆向】OD 调试器工具 ( OD 工具简介 | OD 工具与 CE 工具对比 )
  3. 【Kotlin】属性 与 幕后字段 ( 属性声明 | 属性初始化器 | 属性访问器 | field 属性幕后字段 | lateinit 延迟初始化属性 )
  4. shell脚本之nginx的安装
  5. C提高_day03_const小专题
  6. android:ellipsize实现跑马灯效果总结
  7. 解决fstream不能打开带有中文路径文件的问题
  8. 使用ASP.NET Abstractions增强ASP.NET应用程序的可测试性
  9. 如何在Ubuntu中安装java jdk
  10. runc容器逃逸漏洞最强后续:应对之策汇总与热点疑问解答