Android 界面滑动实现---Scroller类 从源码和开发文档中学习(让你的布局动起来)
来源:互联网 发布:举牌软件下载 编辑:程序博客网 时间:2024/04/28 08:42
android发开文档
Scroller
一.结构关系
二.概述
Class Overview
This class encapsulates scrolling. You can use scrollers (Scroller
or OverScroller
) to collect the data you need to produce a scrolling animation—for example, in response to a fling gesture. Scrollers track scroll offsets for you over time, but they don't automatically apply those positions to your view. It's your responsibility to get and apply new coordinates at a rate that will make the scrolling animation look smooth.
To track the changing positions of the x/y coordinates, use computeScrollOffset()
. The method returns a boolean to indicate whether the scroller is finished. If it isn't, it means that a fling or programmatic pan operation is still in progress. You can use this method to find the current offsets of the x and y coordinates, for example:
跟踪变化的x / y坐标的位置,通过computeScrollOffset()方法监听返回的布尔值来指示滚动动作是否完成。如果返回为false,说明滚动已经结束。返回true,它意味着操作仍在进行中。您可以使用
int currX = mScroller.getCurrX(); //滚动的X滚动距离
int currY = mScroller.getCurrY(); //滚动的y滚动距离
这个方法来找到当前的x和y坐标的偏移量。
三.构造函数
Interpolator interpolator 表示的是动画插入器,你可以设定相应的效果给它。
Interpolator
implements TimeInterpolator
Android.view.animation.Interpolator
Known Indirect Subclasses
AccelerateDecelerateInterpolator, AccelerateInterpolator, AnticipateInterpolator, AnticipateOvershootInterpolator, BounceInterpolator, CycleInterpolator,DecelerateInterpolator, LinearInterpolator, OvershootInterpolator
AccelerateDecelerateInterpolator 动画效果:开始和结束都是缓慢的,通过中间时候加速
AccelerateInterpolator, 动画效果:开始缓慢,之后加速
AnticipateInterpolator, 动画效果:开始后退,然后前进
AnticipateOvershootInterpolator, 动画效果:开始后退,之后前进并超过终点位置,最终退回到终点
BounceInterpolator, 动画效果:慢慢反弹到,弹性衰减到结束
CycleInterpolator, 动画效果:重复循环动画,速度变化遵循正弦定律
DecelerateInterpolator, 动画效果:刚开始快速,之后减速
LinearInterpolator, 动画效果:不断的变化
OvershootInterpolator 动画效果:像前超越最终点然后回来
可以通过初始化构造方法Scroller(Context context, Interpolator interpolator)给它相应的动画效果。
Interpolator interpolator = new BounceInterpolator();
四.公共方法
源码
下面看看以上方法的源码实现:
知识点1:computeScrollOffset()方法
调用该方法判断滚动是否还在继续,mFinished属性判断是否滚动完成,如果滚动完成了,mFinished = true,computeScrollOffset() 返回false。
知识点2:computeScroll()方法
知道了computeScrollOffset()这个判断是否滚动的方法,那我们必须要有监听滑屏控制,并且重绘,在android框架中的VIEW类中就提供了computeScroll()这个方法去控制该流程。在绘制View时,会在draw()过程调用该方法。因此, 再配合使用Scroller实例,我们就可以获得当前应该的偏移坐标,手动使View/ViewGroup偏移至该处。
注:在使用Scroller这个类实现偏移控制,一般自定义View/ViewGroup都需要重载该方法 。
具体实现:
知识点3:startScroll()方法
该方法以提供的起始点和将要滑动的距离开始滚动,我们可以使用该方法达到自动滚动的效果。在滚动中,如果符合什么条件,可以调用该方法让它滚动到相对应的地方。
着重点:
在界面滚动中,你必须搞清楚和scrollTo和scrollBy之间的区别所在:android 布局之滑动探究 scrollTo 和 scrollBy 方法使用说明
使用思路流程:
如果你使用Scroller,流程如下:
1.可以在自定义的布局中,按照需求初始化Scroller构造函数。
2.重写onInterceptTouchEvent(MotionEvent ev)方法,看看是否要拦截相关的点击时间。
3.重写onTouchEvent(MotionEvent event)方法,根据触摸屏上的动作使用computeScroll()以及scrollTo 和 scrollBy 方法进行根据手指对布局进行滑动效果。
4.在触摸操作结束(MotionEvent.ACTION_UP)的时候,调用startScroll(int startX, int startY, int dx, int dy, int duration)方法,进行动画自动操作,来完成整个滚动流程。
对于Scroller类大体的使用和介绍已经完毕,之后会放上自己调用类实现的几个漂亮的效果。
原文链接:http://blog.csdn.net/vipzjyno1/article/details/24592591
- Android 界面滑动实现---Scroller类 从源码和开发文档中学习(让你的布局动起来)
- Android 界面滑动实现---Scroller类 从源码和开发文档中学习(让你的布局动起来)
- Android 界面滑动实现---Scroller类 从源码和开发文档中学习(让你的布局动起来)
- Android 界面滑动实现---Scroller类 从源码和开发文档中学习(让你的布局动起来)
- Android 界面滑动实现---Scroller类 从源码和开发文档中学习(让你的布局动起来)
- Android 界面滑动实现---Scroller类 从源码和开发文档中学习(让你的布局动起来)
- Android 界面滑动实现---Scroller类 从源码和开发文档中学习(让你的布局动起来)
- Android 界面滑动实现---Scroller类 从源码和开发文档中学习(让你的布局动起来)
- [置顶] Android 界面滑动实现---Scroller类 从源码和开发文档中学习(让你的布局动起来)
- android让你的TabHost滑动起来
- android让你的TabHost滑动起来
- android让你的TabHost滑动起来
- android让你的TabHost滑动起来
- android让你的TabHost滑动起来
- android让你的TabHost滑动起来
- iOS开发之让你的应用“动”起来(动画)
- iOS开发之让你的应用“动”起来
- iOS开发之让你的应用“动”起来
- Use UMDH to identify memory leak problem
- ViewPager 指示器
- POJ 3690 Constellations + Gym
- Maven构建忽略测试失败
- 自定义AlertDialog
- Android 界面滑动实现---Scroller类 从源码和开发文档中学习(让你的布局动起来)
- Retrofit2.0的使用及原理解析
- securecrt 上传文件出现 *** was skipped 问题
- composer 的安装和使用
- css 垂直居中
- Linq切勿使用count()>0来判断集合是否为空
- 基于Lock对象的Condition接口实现的有界队列
- 14. Longest Common Prefix(最长公共前缀) —— Java
- butterknife注解框架源码解析