之前在网上看到一篇viewpager简单使用的例子程序,主要采用了上部标签button+中间指示作用的imageview+下部viewpager的结构,点击上部标签,或者滑动viewpager,均可以使中间的imageview产生滑动效果,但是由于程序不够完善,当改变imageview背景等时,不能够正确的显示我们期望的UI效果,因此我对此进行了优化,同时去掉原示例中使用的animation,转而采用valueanimator+imageview的matrix方法进行编码,解决了之前UI中体现的BUG,当然远远不能说满意,权当是对viewpager、valueanimator理解的入门示例。

注:以下编码对网上的示例程序多有借鉴,但是出处找不到了

  1. package com.example.testandroidviewpager;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import android.animation.ValueAnimator;
  7. import android.app.Activity;
  8. import android.graphics.BitmapFactory;
  9. import android.graphics.Matrix;
  10. import android.os.Bundle;
  11. import android.os.Parcelable;
  12. import android.support.v4.view.PagerAdapter;
  13. import android.support.v4.view.ViewPager;
  14. import android.support.v4.view.ViewPager.OnPageChangeListener;
  15. import android.util.DisplayMetrics;
  16. import android.util.Log;
  17. import android.view.LayoutInflater;
  18. import android.view.View;
  19. import android.view.animation.AccelerateInterpolator;
  20. import android.view.animation.Animation;
  21. import android.view.animation.BounceInterpolator;
  22. import android.view.animation.TranslateAnimation;
  23. import android.widget.ImageView;
  24. import android.widget.TextView;
  25.  
  26. public class MainActivity extends Activity {
  27.  
  28. private ViewPager mPager;//页卡内容
  29. private List<View> listViews; // Tab页面列表
  30. private ImageView cursor;// 动画图片
  31. private TextView t1, t2, t3;// 页卡头标
  32. private int offset = 0;// 动画图片偏移量
  33. private int currIndex = 0;// 当前页卡编号
  34. private int prevIndex = 0;// 移动前页卡编号
  35. private int bmpW;// 动画图片宽度
  36. private int screenW;//屏幕宽度,由于只是示例因此简单起见,直接使用屏幕宽度
  37. @Override
  38. protected void onCreate(Bundle savedInstanceState) {
  39. super.onCreate(savedInstanceState);
  40. setContentView(R.layout.activity_main);
  41. InitTextView();
  42. InitImageView();
  43. InitViewPager();
  44. }
  45.  
  46. /**
  47. * 初始化头标
  48. */
  49. private void InitTextView() {
  50. t1 = (TextView) findViewById(R.id.text1);
  51. t2 = (TextView) findViewById(R.id.text2);
  52. t3 = (TextView) findViewById(R.id.text3);
  53.  
  54. t1.setOnClickListener(new MyOnClickListener(0));
  55. t2.setOnClickListener(new MyOnClickListener(1));
  56. t3.setOnClickListener(new MyOnClickListener(2));
  57. }
  58.  
  59. /**
  60. * 头标点击监听
  61. */
  62. public class MyOnClickListener implements View.OnClickListener {
  63. private int index = 0;
  64.  
  65. public MyOnClickListener(int i) {
  66. index = i;
  67. }
  68.  
  69. @Override
  70. public void onClick(View v) {
  71. mPager.setCurrentItem(index);
  72. }
  73. };
  74.  
  75. /**
  76. * 初始化ViewPager
  77. */
  78. private void InitViewPager() {
  79. mPager = (ViewPager) findViewById(R.id.vPager);
  80. listViews = new ArrayList<View>();
  81. LayoutInflater mInflater = getLayoutInflater();
  82. listViews.add(mInflater.inflate(R.layout.page0, null));
  83. listViews.add(mInflater.inflate(R.layout.page1, null));
  84. listViews.add(mInflater.inflate(R.layout.page2, null));
  85. mPager.setAdapter(new MyPagerAdapter(listViews));
  86. mPager.setCurrentItem(0);
  87. mPager.setOnPageChangeListener(new MyOnPageChangeListener());
  88. }
  89.  
  90. /**
  91. * ViewPager适配器
  92. */
  93. public class MyPagerAdapter extends PagerAdapter {
  94. public List<View> mListViews;
  95.  
  96. public MyPagerAdapter(List<View> mListViews) {
  97. this.mListViews = mListViews;
  98. }
  99.  
  100. @Override
  101. public void destroyItem(View arg0, int arg1, Object arg2) {
  102. ((ViewPager) arg0).removeView(mListViews.get(arg1));
  103. }
  104.  
  105. @Override
  106. public void finishUpdate(View arg0) {
  107. }
  108.  
  109. @Override
  110. public int getCount() {
  111. return mListViews.size();
  112. }
  113.  
  114. @Override
  115. public Object instantiateItem(View arg0, int arg1) {
  116. ((ViewPager) arg0).addView(mListViews.get(arg1), 0);
  117. return mListViews.get(arg1);
  118. }
  119.  
  120. @Override
  121. public boolean isViewFromObject(View arg0, Object arg1) {
  122. Log.d("1202","view pager isViewFromObject --> return " + (arg0 == arg1));
  123. return arg0 == arg1;
  124. }
  125.  
  126. @Override
  127. public void restoreState(Parcelable arg0, ClassLoader arg1) {
  128. }
  129.  
  130. @Override
  131. public Parcelable saveState() {
  132. return null;
  133. }
  134.  
  135. @Override
  136. public void startUpdate(View arg0) {
  137. }
  138. }
  139.  
  140. /**
  141. * 初始化动画
  142. */
  143. private void InitImageView() {
  144. cursor = (ImageView) findViewById(R.id.cursor);
  145. bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)
  146. .getWidth();// 获取图片宽度
  147. DisplayMetrics dm = new DisplayMetrics();
  148. getWindowManager().getDefaultDisplay().getMetrics(dm);
  149. screenW = dm.widthPixels;// 获取分辨率宽度
  150. offset = (screenW / 3 - bmpW) / 2;// 计算偏移量
  151. Matrix matrix = new Matrix();
  152. matrix.postTranslate(offset, 0);
  153. cursor.setImageMatrix(matrix);// 设置动画初始位置
  154. }
  155.  
  156. /**
  157. * 页卡切换监听
  158. */
  159. public class MyOnPageChangeListener implements OnPageChangeListener {
  160.  
  161. int one = screenW / 3;// 页卡1 -> 页卡2 偏移量//==screenw/3
  162. int two = one * 2;// 页卡1 -> 页卡3 偏移量
  163.  
  164. @Override
  165. public void onPageSelected(int arg0) {
  166.  
  167. Animation animation = null;
  168. switch (arg0) {
  169. case 0:
  170. if (currIndex == 1) {
  171. animation = new TranslateAnimation(one, 0, 0, 0);
  172. } else if (currIndex == 2) {
  173. animation = new TranslateAnimation(two, 0, 0, 0);
  174. }
  175. break;
  176. case 1:
  177. if (currIndex == 0) {
  178. animation = new TranslateAnimation(0, one, 0, 0);
  179. } else if (currIndex == 2) {
  180. animation = new TranslateAnimation(two, one, 0, 0);
  181. }
  182. break;
  183. case 2:
  184. if (currIndex == 0) {
  185. animation = new TranslateAnimation(0, two, 0, 0);
  186. } else if (currIndex == 1) {
  187. animation = new TranslateAnimation(one, two, 0, 0);
  188. }
  189. break;
  190. }
  191. final Matrix matrix = new Matrix();
  192. prevIndex = currIndex;
  193. currIndex = arg0;
  194.  
  195. ValueAnimator animator0 = ValueAnimator.ofFloat(0f, 1f);
  196. animator0.setDuration(300);
  197. // animator0.setInterpolator(new BounceInterpolator());
  198. animator0.setInterpolator(new AccelerateInterpolator(2));
  199. animator0.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
  200.  
  201. @Override
  202. public void onAnimationUpdate(ValueAnimator animation) {
  203. Object o = animation.getAnimatedValue();
  204. if(o != null){
  205. float f = (Float)o;
  206. Log.d("1201", "onAnimationUpdate f is " + f);
  207. //prevIndex --> currIndex
  208. matrix.reset();
  209. int prevX = offset + one * prevIndex;
  210. int currX = offset + one * currIndex;
  211. int nowValue = (int) (prevX + f * (currX - prevX));
  212. matrix.postTranslate(nowValue, 0);
  213. cursor.setImageMatrix(matrix);
  214. }
  215. }
  216. });
  217. animator0.start();
  218.  
  219. cursor.setImageMatrix(matrix);// 设置动画初始位置
  220.  
  221. // animation.setFillAfter(true);// True:图片停在动画结束位置
  222. // animation.setDuration(300);
  223. // cursor.startAnimation(animation);
  224. }
  225.  
  226. @Override
  227. public void onPageScrolled(int arg0, float arg1, int arg2) {
  228. }
  229.  
  230. @Override
  231. public void onPageScrollStateChanged(int arg0) {
  232. }
  233. }
  234.  
  235. }

layout:activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:umadsdk="http://schemas.android.com/apk/res/com.LoveBus"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:orientation="vertical" >
  7.  
  8. <LinearLayout
  9. android:id="@+id/linearLayout1"
  10. android:layout_width="fill_parent"
  11. android:layout_height="100.0dip"
  12. android:background="#FFFFFF" >
  13.  
  14. <TextView
  15. android:id="@+id/text1"
  16. android:layout_width="fill_parent"
  17. android:layout_height="fill_parent"
  18. android:layout_weight="1.0"
  19. android:gravity="center"
  20. android:text="页卡1"
  21. android:textColor="#000000"
  22. android:textSize="22.0dip" />
  23.  
  24. <TextView
  25. android:id="@+id/text2"
  26. android:layout_width="fill_parent"
  27. android:layout_height="fill_parent"
  28. android:layout_weight="1.0"
  29. android:gravity="center"
  30. android:text="页卡2"
  31. android:textColor="#000000"
  32. android:textSize="22.0dip" />
  33.  
  34. <TextView
  35. android:id="@+id/text3"
  36. android:layout_width="fill_parent"
  37. android:layout_height="fill_parent"
  38. android:layout_weight="1.0"
  39. android:gravity="center"
  40. android:text="页卡3"
  41. android:textColor="#000000"
  42. android:textSize="22.0dip" />
  43. </LinearLayout>
  44.  
  45. <ImageView
  46. android:id="@+id/cursor"
  47. android:background="#333333"
  48. android:layout_width="fill_parent"
  49. android:layout_height="wrap_content"
  50. android:scaleType="matrix"
  51. android:src="@drawable/ic_launcher" />
  52.  
  53. <android.support.v4.view.ViewPager
  54. android:id="@+id/vPager"
  55. android:layout_width="wrap_content"
  56. android:layout_height="wrap_content"
  57. android:layout_gravity="center"
  58. android:layout_weight="1.0"
  59. android:background="#000000"
  60. android:flipInterval="30"
  61. android:persistentDrawingCache="animation" />
  62.  
  63. </LinearLayout>
  1. page0.xml
    1 <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="#330000"
  6. android:orientation="vertical" >
  7.  
  8. </LinearLayout>

page1.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="#003300"
  6. android:orientation="vertical" >
  7.  
  8. </LinearLayout>

page2.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="#000033"
  6. android:orientation="vertical" >
  7.  
  8. </LinearLayout>

注意:位于界面中间的imageview,其scaletype一定要设置成为matrix。

原理:主要是使用valueanimator对matrix的

  1. postTranslate()函数的参数进行了设置,然后不断更新imageview,使其具有动画效果。

viewpager的简单使用,以及ValueAnimator的用法示例的更多相关文章

  1. ViewPager的简单用法+适配器+监听器的介绍

    之前的actionbar+fragment文章中写过viewpager的简单用法,但因为是融合的文章,所以今天把viewpager提取出来写了.方便查询浏览~ 思路: 1.在布局文件中设置viewpa ...

  2. Python网络编程之TCP套接字简单用法示例

    Python网络编程之TCP套接字简单用法示例 本文实例讲述了Python网络编程之TCP套接字简单用法.分享给大家供大家参考,具体如下: 上学期学的计算机网络,因为之前还未学习python,而jav ...

  3. python简单的函数定义和用法实例

    python简单的函数定义和用法实例 这篇文章主要介绍了python简单的函数定义和用法,实例分析了Python自定义函数及其使用方法,具有一定参考借鉴价值,需要的朋友可以参考下 具体分析如下: 这里 ...

  4. 腾讯云上Selenium用法示例

    欢迎大家关注腾讯云技术社区-博客园官方主页,我们将持续在博客园为大家推荐技术精品文章哦~ 作者:崔庆才 前言 在上一节我们学习了PhantomJS 的基本用法,归根结底它是一个没有界面的浏览器,而且运 ...

  5. BinaryOperator<T>接口的用法示例+BiFunction

    转自http://www.tpyyes.com/a/java/2017/1015/285.html 转自https://blog.csdn.net/u014331288/article/details ...

  6. wxpython布局管理部件wx.gridbagsizer用法示例

    text = ("This is text box")         panel = wx.Panel(self, -1)         chkAll1 = wx.CheckB ...

  7. Python排序算法之选择排序定义与用法示例

    Python排序算法之选择排序定义与用法示例 这篇文章主要介绍了Python排序算法之选择排序定义与用法,简单描述了选择排序的功能.原理,并结合实例形式分析了Python定义与使用选择排序的相关操作技 ...

  8. python中hashlib模块用法示例

    python中hashlib模块用法示例 我们以前介绍过一篇Python加密的文章:Python 加密的实例详解.今天我们看看python中hashlib模块用法示例,具体如下. hashlib ha ...

  9. linux日志中查找关键字、前几行、结尾几行,Linux的find用法示例

    linux在日志中查找关键字.前几行.结尾几行,Linux的find用法示例 1.linux在日志中查找关键字.前几行.结尾几行 1.1查看日志 前 n行: 1.2查看日志 尾 n行: 1.3根据 关 ...

随机推荐

  1. ORA-30004 错误处理

    一.问题情景: 原SQL: select c.CATEGORY_ID,       c.CATEGORY_NAME,       SYS_CONNECT_BY_PATH(c.CATEGORY_NAME ...

  2. C++混合编程之idlcpp教程Python篇(4)

    上一篇在这 C++混合编程之idlcpp教程Python篇(3) 第一篇在这 C++混合编程之idlcpp教程(一) 与前面的工程相似,工程PythonTutorial2中,同样加入了三个文件 Pyt ...

  3. javascript改变样式(cssFloat,styleFloat)

    昨天遇到一用js改变元素浮动的,当时直接写了 obj.style.float="left";结果没起作用:查了资料后才发现不能这样写,现在整理下几种样式写法 1,直接写css属性的 ...

  4. MySQL prepare 原理

    Prepare的好处  Prepare SQL产生的原因.首先从mysql服务器执行sql的过程开始讲起,SQL执行过程包括以下阶段 词法分析->语法分析->语义分析->执行计划优化 ...

  5. 蛙蛙推荐:如何实时监控MySql状态

    大多网站的性能瓶颈都会出在数据库上,所以想把Mysql监控起来,就搜索了下相关资料. 后来和同事讨论了下cacti和nagios有些老套和过时,graphite比较时尚,然后就搜了下相关的资料,最后搞 ...

  6. Swift 笔记

    苹果官方文档 https://developer.apple.com CocoaChina帮助文档 http://www.cocoachina.com/special/swift/ 74个Swift标 ...

  7. C语言实现二叉树-04版

    二叉树,通常应当是研究其他一些复杂的数据结构的基础.因此,通常我们应该精通它,而不是了解:当然,可能并不是每个人都认同这种观点,甚至有些人认为理解数据结构就行了!根本没有必要去研究如何实现,因为大多数 ...

  8. C语言实现二叉树-03版

    我们亲爱的项目经理真是有创意,他说你给我写得二叉树挺好的: 功能还算可以:插入节点,能够删除节点: 可是有时候我们只是需要查找树的某个节点是否存在: 所以我希望你能够给我一个find功能: 还有就是, ...

  9. iOS开发-xCode代码格式化xAlign

    xCode默认是可以进行代码格式化的,能满足基础开发需求,如果想要个性一些代码对齐方式宏对齐,等号对齐,属性对齐,xAlign就提供了以上三种功能,参考文中效果~ 基础效果 等号对齐: 属性对齐: 宏 ...

  10. Leetcode 326 Power of Three 数论

    判断一个数是否是3的n次幂 这里我用了一点巧,所有的int范围的3的n次幂是int范围最大的3的n次幂数(即3^((int)log3(MAXINT)) =  1162261467)的约数 这种方法是我 ...