Android Scroller简单用法
来源:互联网 发布:石家庄花都学校 知乎 编辑:程序博客网 时间:2024/04/30 13:02
原文:http://ipjmc.iteye.com/blog/1615828
Android里Scroller类是为了实现View平滑滚动的一个Helper类。通常在自定义的View时使用,在View中定义一个私有成员mScroller = new Scroller(context)。设置mScroller滚动的位置时,并不会导致View的滚动,通常是用mScroller记录/计算View滚动的位置,再重写View的computeScroll(),完成实际的滚动。
mScroller.getCurrX() //获取mScroller当前水平滚动的位置mScroller.getCurrY() //获取mScroller当前竖直滚动的位置mScroller.getFinalX() //获取mScroller最终停止的水平位置mScroller.getFinalY() //获取mScroller最终停止的竖直位置mScroller.setFinalX(int newX) //设置mScroller最终停留的水平位置,没有动画效果,直接跳到目标位置mScroller.setFinalY(int newY) //设置mScroller最终停留的竖直位置,没有动画效果,直接跳到目标位置//滚动,startX, startY为开始滚动的位置,dx,dy为滚动的偏移量, duration为完成滚动的时间mScroller.startScroll(int startX, int startY, int dx, int dy) //使用默认完成时间250msmScroller.startScroll(int startX, int startY, int dx, int dy, int duration)mScroller.computeScrollOffset() //返回值为boolean,true说明滚动尚未完成,false说明滚动已经完成。这是一个很重要的方法,通常放在View.computeScroll()中,用来判断是否滚动是否结束。
举例说明,自定义一个CustomView,使用Scroller实现滚动:
import android.content.Context;import android.util.AttributeSet;import android.util.Log;import android.view.View;import android.widget.LinearLayout;import android.widget.Scroller;public class CustomView extends LinearLayout {private static final String TAG = "Scroller";private Scroller mScroller;public CustomView(Context context, AttributeSet attrs) {super(context, attrs);mScroller = new Scroller(context);}//调用此方法滚动到目标位置public void smoothScrollTo(int fx, int fy) {int dx = fx - mScroller.getFinalX();int dy = fy - mScroller.getFinalY();smoothScrollBy(dx, dy);}//调用此方法设置滚动的相对偏移public void smoothScrollBy(int dx, int dy) {//设置mScroller的滚动偏移量mScroller.startScroll(mScroller.getFinalX(), mScroller.getFinalY(), dx, dy);invalidate();//这里必须调用invalidate()才能保证computeScroll()会被调用,否则不一定会刷新界面,看不到滚动效果}@Overridepublic void computeScroll() {//先判断mScroller滚动是否完成if (mScroller.computeScrollOffset()) {//这里调用View的scrollTo()完成实际的滚动scrollTo(mScroller.getCurrX(), mScroller.getCurrY());//必须调用该方法,否则不一定能看到滚动效果postInvalidate();}super.computeScroll();}}
0 0
- Android Scroller简单用法
- Android Scroller简单用法
- Android Scroller简单用法
- Android Scroller简单用法
- Android Scroller简单用法
- Android Scroller简单用法
- Android Scroller简单用法
- Android Scroller简单用法
- Android Scroller简单用法
- Android Scroller简单用法
- Android Scroller简单用法
- Android Scroller简单用法
- Android Scroller简单用法
- Android Scroller简单用法
- Android Scroller简单用法
- Android Scroller简单用法
- Android Scroller简单用法
- Android Scroller简单用法
- Phoenix实现用SQL查询HBase
- c++设计模式(迭代器模式)
- IOS 集合视图指南2:集合视图基础
- 设计模式概论
- 一个挖掘机 Demo 用时一周【狗刨学习网】
- Android Scroller简单用法
- 对Physics.Raycast和Ray(射线)的理解 和 分层layer的使用
- 类别和类扩展的区别
- c++设计模式(单例模式)
- 系统托盘Shell_NotifyIcon 函数
- UISegmentedControl 分段控制器
- 循环-25. 求给定序列前N项和之三(15)
- android之Activity的生命周期
- 为什么页游公司喜欢用erlang