Android 界面滑动实现---Scroller类 从源码和开发文档中学习(让你的布局动起来)

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坐标的偏移量。
三.构造函数
| Public Constructors | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
|
Create a Scroller with the default duration and interpolator.
|
|||||||||||
|
Create a Scroller with the specified interpolator.
|
|||||||||||
|
Create a Scroller with the specified interpolator.
|
|||||||||||
Interpolator interpolator 表示的是动画插入器,你可以设定相应的效果给它。
Interpolator
implements TimeInterpolator
|
android.view.animation.Interpolator |
AccelerateDecelerateInterpolator 动画效果:开始和结束都是缓慢的,通过中间时候加速
AccelerateInterpolator, 动画效果:开始缓慢,之后加速
AnticipateInterpolator, 动画效果:开始后退,然后前进
AnticipateOvershootInterpolator, 动画效果:开始后退,之后前进并超过终点位置,最终退回到终点
BounceInterpolator, 动画效果:慢慢反弹到,弹性衰减到结束
CycleInterpolator, 动画效果:重复循环动画,速度变化遵循正弦定律
DecelerateInterpolator, 动画效果:刚开始快速,之后减速
LinearInterpolator, 动画效果:不断的变化
OvershootInterpolator 动画效果:像前超越最终点然后回来
可以通过初始化构造方法Scroller(Context context, Interpolator interpolator)给它相应的动画效果。
Interpolator interpolator = new BounceInterpolator();
四.公共方法
| Public Methods | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
|
void
|
abortAnimation() 停止动画,滚到最终的x,y位置中止动画
|
||||||||||
|
boolean
|
computeScrollOffset() 当你想要知道新的位置时候,调用该方法。返回true:动画没结束
|
||||||||||
|
void
|
extendDuration(int extend) 延长滚动动画的时间。extend表示延迟时间(单位为毫秒)
|
||||||||||
|
void
|
fling(int startX, int startY, int velocityX, int velocityY, int minX, int maxX, int minY, int maxY)
在fling(快速滑动,触摸屏幕后快意移动松开)的手势基础上开始滚动,滚动距离取决于fling的初速度。
|
||||||||||
|
final void
|
forceFinished(boolean finished) 强制终止滚动。
|
||||||||||
|
float
|
getCurrVelocity() 返回当前的速度
|
||||||||||
|
final int
|
getCurrX() 返回当前滚动的X方向的偏移量(距离原点X轴方向)
|
||||||||||
|
final int
|
getCurrY() 返回当前滚动的Y方向的偏移量(距离原点Y轴方向)
|
||||||||||
|
final int
|
getDuration() 返回滚动事件的持续时间(毫秒)
|
||||||||||
|
final int
|
getFinalX() 返回滚动结束的X方向的偏移量(注:只针对fling 手势有效)(距离原点X轴方向)
|
||||||||||
|
final int
|
getFinalY() 返回滚动结束的Y方向的偏移量(注:只针对fling 手势有效)(距离原点Y轴方向)
|
||||||||||
|
final int
|
getStartX() 返回滚动起始点的X方向偏移量(距离原点X轴方向)
|
||||||||||
|
final int
|
getStartY() 返回滚动起始点的Y方向偏移量.(距离原点Y轴方向)
|
||||||||||
|
final boolean
|
isFinished() 返回scroller滚动是否结束,true:滚动结束 false:还在滚动
|
||||||||||
|
void
|
setFinalX(int newX) 设置scroller的终止时X方向偏移量
|
||||||||||
|
void
|
setFinalY(int newY) 设置scroller的终止时Y方向偏移量
|
||||||||||
|
final void
|
setFriction(float friction)
The amount of friction applied to flings.
|
||||||||||
|
void
|
startScroll(int startX, int startY, int dx, int dy)
提供起始点和滚动距离,调用该方法进行滚动。(此处默认时间为250ms)
|
||||||||||
|
void
|
startScroll(int startX, int startY, int dx, int dy, int duration)
提供起始点和滚动距离以及滚动时间,调用该方法进行滚动。
|
||||||||||
|
int
|
timePassed() 返回自滚动开始经过的时间(毫秒)
|
||||||||||
源码
下面看看以上方法的源码实现:
知识点1:computeScrollOffset()方法
- /**
- * Call this when you want to know the new location. If it returns true,
- * the animation is not yet finished. loc will be altered to provide the
- * new location.
- */
- public boolean computeScrollOffset() {
- if (mFinished) {
- return false; //已经完成了本次动画,直接返回为false
- }
- int timePassed = (int)(AnimationUtils.currentAnimationTimeMillis() - mStartTime);
- if (timePassed < mDuration) {
- switch (mMode) {
- case SCROLL_MODE:
- float x = timePassed * mDurationReciprocal;
- if (mInterpolator == null)
- x = viscousFluid(x);
- else
- x = mInterpolator.getInterpolation(x);
- mCurrX = mStartX + Math.round(x * mDeltaX);
- mCurrY = mStartY + Math.round(x * mDeltaY);
- break;
- case FLING_MODE:
- final float t = (float) timePassed / mDuration;
- final int index = (int) (NB_SAMPLES * t);
- float distanceCoef = 1.f;
- float velocityCoef = 0.f;
- if (index < NB_SAMPLES) {
- final float t_inf = (float) index / NB_SAMPLES;
- final float t_sup = (float) (index + 1) / NB_SAMPLES;
- final float d_inf = SPLINE_POSITION[index];
- final float d_sup = SPLINE_POSITION[index + 1];
- velocityCoef = (d_sup - d_inf) / (t_sup - t_inf);
- distanceCoef = d_inf + (t - t_inf) * velocityCoef;
- }
- mCurrVelocity = velocityCoef * mDistance / mDuration * 1000.0f;
- mCurrX = mStartX + Math.round(distanceCoef * (mFinalX - mStartX));
- // Pin to mMinX <= mCurrX <= mMaxX
- mCurrX = Math.min(mCurrX, mMaxX);
- mCurrX = Math.max(mCurrX, mMinX);
- mCurrY = mStartY + Math.round(distanceCoef * (mFinalY - mStartY));
- // Pin to mMinY <= mCurrY <= mMaxY
- mCurrY = Math.min(mCurrY, mMaxY);
- mCurrY = Math.max(mCurrY, mMinY);
- if (mCurrX == mFinalX && mCurrY == mFinalY) {
- mFinished = true;
- }
- break;
- }
- }
- else {
- mCurrX = mFinalX;
- mCurrY = mFinalY;
- mFinished = true;
- }
- return true;
- }
调用该方法判断滚动是否还在继续,mFinished属性判断是否滚动完成,如果滚动完成了,mFinished = true,computeScrollOffset() 返回false。
知识点2:computeScroll()方法
- /**
- * Called by a parent to request that a child update its values for mScrollX
- * and mScrollY if necessary. This will typically be done if the child is
- * animating a scroll using a {@link android.widget.Scroller Scroller}
- * object.
- */由父视图调用用来请求子视图根据偏移值 mScrollX,mScrollY重新绘制
- public void computeScroll() {
- }
知道了computeScrollOffset()这个判断是否滚动的方法,那我们必须要有监听滑屏控制,并且重绘,在Android框架中的VIEW类中就提供了computeScroll()这个方法去控制该流程。在绘制View时,会在draw()过程调用该方法。因此, 再配合使用Scroller实例,我们就可以获得当前应该的偏移坐标,手动使View/ViewGroup偏移至该处。
注:在使用Scroller这个类实现偏移控制,一般自定义View/ViewGroup都需要重载该方法 。
具体实现:
- @Override
- public void computeScroll() {
- if (mScroller.computeScrollOffset()) {
- scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
- // 更新界面
- postInvalidate();
- isMoving = true;
- } else {
- isMoving = false;
- }
- super.computeScroll();
- }
知识点3:startScroll()方法
- /**
- * Start scrolling by providing a starting point and the distance to travel.
- *
- * @param startX //水平方向滚动的偏移值,以像素为单位。正值表明滚动将向左滚动
- * @param startY //垂直方向滚动的偏移值,以像素为单位。正值表明滚动将向上滚动
- * @param dx //水平方向滑动的距离,正值会使滚动向左滚动
- * @param dy //垂直方向滑动的距离,正值会使滚动向上滚动
- * @param duration //滚动持续时间
- */
- public void startScroll(int startX, int startY, int dx, int dy, int duration) {
- mMode = SCROLL_MODE;
- mFinished = false;
- mDuration = duration;
- mStartTime = AnimationUtils.currentAnimationTimeMillis();
- mStartX = startX;
- mStartY = startY;
- mFinalX = startX + dx;
- mFinalY = startY + dy;
- mDeltaX = dx;
- mDeltaY = dy;
- mDurationReciprocal = 1.0f / (float) mDuration;
- }
该方法以提供的起始点和将要滑动的距离开始滚动,我们可以使用该方法达到自动滚动的效果。在滚动中,如果符合什么条件,可以调用该方法让它滚动到相对应的地方。
着重点:
在界面滚动中,你必须搞清楚和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类大体的使用和介绍已经完毕,之后会放上自己调用类实现的几个漂亮的效果。
版权声明:本文为博主原创文章,未经博主允许不得转载。
Android 界面滑动实现---Scroller类 从源码和开发文档中学习(让你的布局动起来)的更多相关文章
- Android 滑动界面实现---Scroller类别 从源代码和开发文档了解(让你的移动布局)
在android学习,行动互动是软件的重要组成部分,其中Scroller是提供了拖动效果的类,在网上.比方说一些Launcher实现滑屏都能够通过这个类去实现.. 样例相关博文:Android 仿 窗 ...
- Android 界面滑动卡顿分析与解决方案(入门)
Android 界面滑动卡顿分析与解决方案(入门) 导致Android界面滑动卡顿主要有两个原因: 1.UI线程(main)有耗时操作 2.视图渲染时间过长,导致卡顿 目前只讲第1点,第二点相对比较复 ...
- Android Studio2.x版本无法自动关联源码的解决方法
Android Studio2.x版本无法自动关联源码的解决方法 在学习android开发过程中,对于一个不熟悉的类,阅读源码是一个很好的学习方式,使用andorid studio开发工具的SDK M ...
- Android精品资源汇总,10个源码(持续更新)
最近一直在学习Android,在各大社区逛,总结下自己看到的一些不错的源码.希望可以给大家带来帮助. 1.Android精品源码:带动态效果的Button(按钮) 最喜欢各种效果的按钮了,没办法就是这 ...
- Android多线程之(一)View.post()源码分析——在子线程中更新UI
提起View.post(),相信不少童鞋一点都不陌生,它用得最多的有两个功能,使用简便而且实用: 1)在子线程中更新UI.从子线程中切换到主线程更新UI,不需要额外new一个Handler实例来实现. ...
- Android斗地主棋牌游戏牌桌实现源码下载
本次给大家分享下Android斗地主棋牌游戏牌桌实现源码下载如下: 为了节约内存资源,每张扑克牌都是剪切形成的,当然这也是当前编程的主流方法. 1.主Activity package com.biso ...
- android 在线升级借助开源中国App源码
android 在线升级借助开源中国App源码 http://www.cnblogs.com/luomingui/p/3949429.html android 在线升级借助开源中国App源码分析如下: ...
- Android Handler处理机制 ( 一 )(图+源码分析)——Handler,Message,Looper,MessageQueue
android的消息处理机制(图+源码分析)——Looper,Handler,Message 作为一个大三的预备程序员,我学习android的一大乐趣是可以通过源码学习 google大牛们的设计思想. ...
- 3D语音天气球(源码分享)——在Unity中使用Android语音服务
转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 开篇废话: 这个项目准备分四部分介绍: 一:创建可旋转的"3D球":3 ...
随机推荐
- winform控件记录
winform: --------------------------------------------------- Button控件: 属性: BackColor: 背景颜色 Backgroun ...
- emacs打开乱码解决办法
前言:有时候我们使用emacs打开文件的时候,因为emacs默认编码跟文档编码不同而出现了乱码如图: 对于新手的我们应该通过以下两种途径来解决: 方法一: 只需C-x <RET> r ( ...
- 再见了acm
2013年11月17日长沙区域赛我的最后一场区域赛. 忙碌了三年的acm要停下脚步,一时还无法接受. 这样一个结果有点无奈. 感谢队友,三年三支队伍五个队友,感谢你们.(每当写到这里时就总有点小忍不住 ...
- 使用 getopt() 进行命令行处理
引言 在早期的 UNIX® 中,其命令行环境(当时的唯一用户界面)包含着数十种小的文本处理工具.这些工具非常小,通常可很好地完成一项工作.这些工具通过较长的命令管道链接在一起,前面的程序将其输出传递给 ...
- win7下wordPress本地搭建博客详解(深度亲测整理---傻瓜式详细教程)
搭建一个wordPress作为一个个人博客本来是特别简单的事情,但是网上的各种转载让初学者举步维艰,我就本身条件而言,会java EE,懂mysql都花费了我好长时间才搭建好本地博客. 注意:这个是本 ...
- Eclipse使用技巧总结(三)
十六.快速关闭窗口 关闭当前打开窗口 Ctrl + W 关闭当前打开的所有窗口 Ctrl +Shift +F4 十九.重命名 F2 二十.快速回到上次编辑点 Ctrl + Q 二十 ...
- javascript每日一练(九)——运动一:匀速运动
一.js的运动 匀速运动 清除定时器 开启定时器 运动是否完成:a.运动完成,清除定时器:b.运动未完成继续 匀速运动停止条件:距离足够近 Math.abs(当然距离-目标距离) < 最小运动 ...
- 我的Python成长之路---第一天---Python基础(6)---2015年12月26日(雾霾)
七.列表——list Python的列表是一种内置的数据类型,是由Python的基本数据类型组成的有序的集合.有点类似C语言的数组,但与数组不同的是,Python在定义列表的时候不用指定列表的容积(长 ...
- Git 将本次修改追加在上一次修改上面
Git 将本次修改追加在上一次修改上面 git add . git commit --amend 之后就是进入日志提交页面 确保change-Id那条记录出现在最后一行,如: zh-->en 修 ...
- GDSOI2015 task4 ACU
题目大意 只要你有耐心看完题目,你就可以得到以下模型: 给出一个有向图,有若干询问,每次询问对于某条边\((v,u)\),求删掉这条边后,\(v\)到\(u\)的最短路. 算法1 暴力出奇迹,期望得分 ...
Known Indirect Subclasses