Android 界面滑动实现---Scroller类 从源码和开发文档中学习(让你的布局动起来)...
android發開文檔
? 開發文檔參考鏈接:http://developer.android.com/reference/android/widget/Scroller.htmlScroller
一.結構關系
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坐標的偏移量。
?
三.構造函數
| ? | 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();
?
?
四.公共方法
?
| 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()方法
?
[java]?view plaincopy ??
調用該方法判斷滾動是否還在繼續,mFinished屬性判斷是否滾動完成,如果滾動完成了,mFinished = true,computeScrollOffset()?返回false。
?
知識點2:computeScroll()方法
?
[java]?view plaincopy ??
知道了computeScrollOffset()這個判斷是否滾動的方法,那我們必須要有監聽滑屏控制,并且重繪,在Android框架中的VIEW類中就提供了computeScroll()這個方法去控制該流程。在繪制View時,會在draw()過程調用該方法。因此,?再配合使用Scroller實例,我們就可以獲得當前應該的偏移坐標,手動使View/ViewGroup偏移至該處。
注:在使用Scroller這個類實現偏移控制,一般自定義View/ViewGroup都需要重載該方法 。
具體實現:
?
[java]?view plaincopy ??
?
知識點3:startScroll()方法
[java]?view plaincopy ?該方法以提供的起始點和將要滑動的距離開始滾動,我們可以使用該方法達到自動滾動的效果。在滾動中,如果符合什么條件,可以調用該方法讓它滾動到相對應的地方。
?
著重點:
在界面滾動中,你必須搞清楚和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/Free-Thinker/p/4785014.html
總結
以上是生活随笔為你收集整理的Android 界面滑动实现---Scroller类 从源码和开发文档中学习(让你的布局动起来)...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: BZOJ 3668: [Noi2014]
- 下一篇: PHPCMS v9 二次开发_验证码结合