Android开发艺术探索学习-View的滑动
来源:互联网 发布:2016当红网络主播 编辑:程序博客网 时间:2024/06/05 00:08
滑动是View的一个很重要的特性,Android也提供了很多支持滑动的控件,可以说滑动无处不在。因此掌握View的滑动是相当重要的。常见的滑动三种方法有:使用scrollTo和scrollBy、使用动画、改变布局参数。
- 使用scrollTo和scrollBy
在View中提供了scrollTo和scrollBy方法来实现View的滑动,按惯例看下这两个方法的实现:
/** * Set the scrolled position of your view. This will cause a call to * {@link #onScrollChanged(int, int, int, int)} and the view will be * invalidated. * @param x the x position to scroll to * @param y the y position to scroll to */ public void scrollTo(int x, int y) { if (mScrollX != x || mScrollY != y) { int oldX = mScrollX; int oldY = mScrollY; mScrollX = x; mScrollY = y; invalidateParentCaches(); onScrollChanged(mScrollX, mScrollY, oldX, oldY); if (!awakenScrollBars()) { postInvalidateOnAnimation(); } } }和
/** * Move the scrolled position of your view. This will cause a call to * {@link #onScrollChanged(int, int, int, int)} and the view will be * invalidated. * @param x the amount of pixels to scroll by horizontally * @param y the amount of pixels to scroll by vertically */ public void scrollBy(int x, int y) { scrollTo(mScrollX + x, mScrollY + y); }通过源码可以看到scrollBy方法其实是调用的scrollTo方法,它是基于当前位置的相对滑动,而scrollTo是基于所传参数的绝对滑动。这里有两个非常重要的属性:mScrollX和mScrollY。
/** * The offset, in pixels, by which the content of this view is scrolled * horizontally. * {@hide} */ @ViewDebug.ExportedProperty(category = "scrolling") protected int mScrollX; /** * The offset, in pixels, by which the content of this view is scrolled * vertically. * {@hide} */ @ViewDebug.ExportedProperty(category = "scrolling") protected int mScrollY;通过mScrollX和mScrollY的注释可以知道,它们分别是指的View的横向和纵向位移。使用scrollTo和scrollBy滑动只能改变View内容的位置而不能改变View在布局中的位置。通过这两方法实现的滑动,其实并没有体现“滑”,我认为仅仅体现了“动”。因为View调用scrollTo和scrollBy后是突然闪现到终点位置,有时候在交互上并不是很友好。不要担心,Android提供了Scroller类来实现View的弹性滑动。关于Scroller的使用请看官方文档http://developer.android.com/reference/android/widget/Scroller.html(请翻墙浏览)
- 使用动画
使用动画可以移动View,这里动画主要操作的就是View的translationX和translationY属性,既可以采用传统的View动画,也可以采用属性动画,如果采用属性动画的话,为了能兼容3.0以下的版本,需要采用开源动画库nineoldandroids。
实现View滑动的动画有两种方式,一种是利用xml文件编写动画,另外一种是在代码中调用动画相关API实现。这里就不介绍具体用法了。
View的动画师对View的影像做操作,并没有改变View本身的位置参数,包括高和宽,如果希望动画完成后的状态得以保留必须fillAfter属性设置为true,否则动画完成后其动画结果会消失。如果fillAfter设置为false,那么动画完成后View将会回到初始位置。
普通的动画并不能改变View真正的位置,因此,当View通过动画向右平移了100个像素,这时候我们会发现,点击新位置的View并不能响应点击事件,但是当我们点击View原始位置时,仍可以响应点击事件。因为View在新位置上仅仅是一个影像而已,并不能响应点击事件。从Android3.0开始,使用属性动画可以解决该问题。那么Android3.0以下的版本如何解决?思路我觉得可以有很多,比如在动画重点位置预埋一个一模一样的View,动画完成后隐藏动画View,显示预埋View。
- 改变布局参数
这种方法大家应该都有见过,那就是ListView的下拉刷新就是采用这种方式实现的,通过改变headView的LayoutParams的布局参数达到下拉滑动效果,具体的实现方式,大家可以参考XListView的实现。https://github.com/Maxwin-z/XListView-Android
- 三种滑动方式的区别
1⃣️scrollTo、scrollBy:操作简单,适合View内容的滑动。
2⃣️动画:操作简单,主要适用于没有交互的View和实现复杂的动画效。
3⃣️改变布局参数:操作较复杂,通常重写onTouchEvent方法配合手势滑动,适用于有交互的View。
0 0
- Android开发艺术探索学习-View的滑动
- View的滑动冲突解决-android开发艺术探索笔记
- Android开发艺术探索笔记(9)- View的滑动
- Android开发艺术探索学习-View的基本知识
- Android开发艺术探索(连载)之View的事件体系(二)View的滑动
- 读《Android开发艺术探索》---View滑动冲突
- Android中View的弹性滑动——Android开发艺术探索笔记
- Android中View的滑动冲突——Android开发艺术探索笔记
- AnAndroid开发艺术探索读后感—View的滑动
- Android开发艺术探索笔记(10)- View的弹性滑动
- Android开发艺术探索笔记(12)- View的滑动冲突
- Android开发艺术View滑动
- Android——View的事件分发机制(学习Android开发艺术探索)
- Android开发艺术探索学习-View的事件分发机制(一)
- Android开发艺术探索学习-View的事件分发机制(二)
- 《Android开发艺术探索》之View的事件体系和工作原理学习笔记
- 《Android开发艺术探索》之学习笔记(四)View的工作原理
- 《Android开发艺术探索》之学习笔记(三)View的基础知识
- 关于ALAssetsLibrary的学习
- long start time
- Adreno Profiler分析任意安卓游戏特效+抓取资源
- HashSet,TreeSet和LinkedHashSet的区别
- 深入理解Java:内省(Introspector)
- Android开发艺术探索学习-View的滑动
- android sdk 安装及解决翻墙获取包列表、提升下载速度
- poj 2409 polya原理
- 模块化管理(一)
- 如何去掉windows10 windows8 我的电脑 中的几个烦人文件夹
- rman 定时增量全备
- 正则表达式在iOS中的运用
- Hadoop本地库与系统版本不一致引起的错误解决方法
- 深度理解Jquery 中 scrollTop() 方法