在android学习,行动互动是软件的重要组成部分,其中Scroller是提供了拖动效果的类,在网上。比方说一些Launcher实现滑屏都能够通过这个类去实现。。
样例相关博文:Android 仿 窗帘效果 和 登录界面拖动效果 (Scroller类的应用) 附 2个DEMO及源代码
在广泛使用的側边滑动导航开源库 --SlidingLayer事实上就是使用到了Scroller类进行的实现,下载地址:GITHUB  。以下要讲的不是这个库,而是这个库的实现过程中使用到的---Scroller类。懂了之后你看库的源代码就知道,原来它是这样实现的。
Scroller类使用过程中,懂得下面机制可能会对开发更有帮助:
1.视图的VIEW的自己定义以及其在屏幕中布局。
2.scrollTo()和scrollBy()方法的作用差别    
能够点击此处了解:android 布局之滑动探究 scrollTo 和 scrollBy 方法使用说明
3.屏幕中的触摸事件分发机制(这一块在涉及到触摸的不论什么情况下都十分重要)
首先看看发开文档里面说了些什么:

android发开文档

开发文档參考链接:http://developer.android.com/reference/android/widget/Scroller.html

Scroller

一.结构关系

extends Object

二.概述

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
Scroller(Context context)
Create a Scroller with the default duration and interpolator.
Scroller(Context context, Interpolator interpolator)
Create a Scroller with the specified interpolator.
Scroller(Context context, Interpolator interpolator, boolean flywheel)
Create a Scroller with the specified interpolator.

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();

四.公共方法

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);elsex = 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 <= mMaxXmCurrX = Math.min(mCurrX, mMaxX);mCurrX = Math.max(mCurrX, mMinX);mCurrY = mStartY + Math.round(distanceCoef * (mFinalY - mStartY));// Pin to mMinY <= mCurrY <= mMaxYmCurrY = 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都须要重载该方法 。

详细实现:

@Overridepublic 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类大体的使用和介绍已经完成。之后会放上自己调用类实现的几个美丽的效果。

版权声明:本文博客原创文章。博客,未经同意,不得转载。

转载于:https://www.cnblogs.com/mfrbuaa/p/4618611.html

Android 滑动界面实现---Scroller类别 从源代码和开发文档了解(让你的移动布局)...相关推荐

  1. Android官方开发文档Training系列课程中文版:布局性能优化之ListView的优化

    原文地址:http://android.xsoftlab.net/training/improving-layouts/smooth-scrolling.html 想要让ListView滑动流畅的关键 ...

  2. Android官方开发文档Training系列课程中文版:布局性能优化之按需加载View

    原文地址:http://android.xsoftlab.net/training/improving-layouts/loading-ondemand.html 有时应用程序中会有一些很少用到的复杂 ...

  3. Android官方开发文档Training系列课程中文版:布局性能优化之布局层级优化

    原文地址:http://android.xsoftlab.net/training/improving-layouts/index.html 引言 布局是直接影响用户体验的关键部分.如果实现的不好,那 ...

  4. Android官方开发文档Training系列课程中文版:布局性能优化之布局复用

    原文地址:http://android.xsoftlab.net/training/improving-layouts/reusing-layouts.html 尽管Android提供了种类繁多的常用 ...

  5. android怎么集成sdk,集成方式-Android开发集成-SDK开发集成-信令-网易云信开发文档...

    集成方式 网易云通信 SDK 支持两种方式集成. 1. 通过 Gradle 集成 SDK (推荐) 2. 通过类库配置集成 SDK 网易云通信 Android SDK 2.5.0 以上强烈推荐通过 G ...

  6. 橙狮AI图像识别绘本阅读方案(含完整源代码和开发文档)

    概述 本文描述一个基于人工智能2D图像识别算法实现的绘本阅读方案,应用于绘本阅读机器人和绘本阅读手机APP.主要内容包括:基础算法,方案架构及工程化,项目遇到的坑及解决方案.为了更容易理解,本文重点描 ...

  7. android调用js sdk,JSSDK使用步骤 - 微信 JS-SDK 开发文档 V1.6.0

    步骤一:绑定域名 先登录微信公众平台进入"公众号设置"的"功能设置"里填写"JS接口安全域名". 备注:登录后可在"开发者中心&q ...

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

    在android学习中,动作交互是软件中重要的一部分,其中的Scroller就是提供了拖动效果的类,在网上,比如说一些Launcher实现滑屏都可以通过这个类去实现..   例子相关博文:Androi ...

  9. Android的开发文档规范

    Android的开发文档规范 我们项目的代码时间时间很长,经过太多人手,代码的规范性堪忧,目前存在较多的比较自由的「代码规范」,这非常不利于项目的维护,代码可读性也不够高. 分析现有项目的代码的情况, ...

  10. Android官方开发文档Training系列课程中文版:目录

    原文地址 : http://android.xsoftlab.net/training/index.html 引言 在翻译了一篇安卓的官方文档之后,我觉得应该做一件事情,就是把安卓的整篇训练课程全部翻 ...

最新文章

  1. 生动的解释下什么是 MySQL 的“回表”?
  2. linux计划任务作业
  3. python【蓝桥杯vip练习题库】ADV-308递归输出
  4. redis占用内存过低_使用多种数据结构优化Redis 内存占用
  5. 电脑测速软件_联通你我【宽带提速】让网速飞!超实用的宽带测速提速攻略来啦!...
  6. 轻量级ORM框架 【Dapper】 的使用
  7. Visual studio 2013安装及单元测试
  8. 使用 Swift 在 iOS 10 中集成 Siri —— SiriKit 教程(Part 1)
  9. php源码下载 uctoo_uctoo
  10. MySql存储过程总结
  11. 二叉树的简单应用--表达式树
  12. 最佳软件开发实践指导
  13. 计算机鼠标双击怎么,电脑鼠标双击变成属性怎么办-解决电脑鼠标双击变成属性的方法 - 河东软件园...
  14. 西瓜播放器xgplayer的简单使用demo
  15. 技嘉显卡 RGBFusion 不能调光解决方法
  16. java怎么弹出页面_java怎么样实现弹出窗口
  17. 强大如斯的Bunch类
  18. PubWin不知道密码情况下卸载
  19. 【多模态】多模态特征融合策略——门控多模态融合方法
  20. OmegaT术语库介绍与分享

热门文章

  1. 领域搜索算法 是什么 和遗传算法、模拟退火算法、禁忌搜索算法、模糊优化 算法、微粒群算法关系
  2. Docker教程小白实操入门(16)--如何使用ONBUILD指令在构建下一级镜像时做些什么
  3. Docker教程小白实操入门(11)--如何构建私有Registry
  4. Java保存class文件,[转载]Class文件在JVM中如何存储
  5. 基于SSM的猫头鹰轻博客系统
  6. Leetcode之跳跃游戏Ⅱ
  7. mysql的驱动connect放在哪里_十年测试解析:ddt结合excel,csv,mysql实现自动化测试数据驱动...
  8. dcm4chee汉化_docker dcm4chee
  9. 我要发明计算机作文,我要发明机器人作文700字
  10. 小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_1-4.在线教育后台数据库设计...