本文原創,轉載請注明出處。 歡迎關注我的 簡書 ,關注我的專題 Android Class 我會長期堅持為大家收錄簡書上高質量的 Android 相關博文。
寫在前面: 最近在學習研究總結 View 的知識體系,前天完成了系列的第一篇文章:
View體系之View的位置與事件
今天帶來 View 體系的第二篇文章,View 的滑動。
Android 手機因為設備面積有限,滑動就是一個展示更多內容的手段,并且平滑的滑動處理,還能給用戶比較好的體驗。
在 Android 中,常見的滑動方式有三種:
View 的 scrollTo / scrollBy 方法。補間動畫或屬性動畫。改變 View 的 LayoutParams 的 margin 屬性,來達到滑動的效果。首先關于補間動畫實現的效果,之前我寫過一個總結,可以看這里。
幀動畫和補間動畫看這篇足夠了
而屬性動畫本文不打算做講解,因為未來肯定我會寫一篇關于屬性動畫的學習,先挖一個坑,未來會填上。
所以本文會著重講解其他兩種方式,來實現 Android 中 View 的滑動。
首先不談他們兩個有什么區別,關于他們的效果一開始給我就弄得一頭霧水。首先我理解的 View 的平移是 View 自身位置的移動,而 ScrollTo / ScrollBy 移動的是 View 的內容。
View 的內容是什么意思呢,一開始我也不理解,但是看下寫了一個 Demo 就明白了。
首先我點擊下方 ScrollButton 按鈕,移動上方粉色背景的 TextView。
tvMove.scrollBy(30, 10);可以看到效果是:“看我移動” 這四個字向右上方向移動了。再來理解兩個概念,首先是 View 的繪制區域和 View 的布局區域。
首先來觀察被移動的 TextView 綠色框是 View 的 Canvas 繪制區域,它是沒有邊界并且無線延伸的,不受 TextView 自身大小和屏幕大小的控制。黃色框是 TextView 的布局區域,框出來的部分不是粉色邊界是因為這個 TextView 被我加了一個 padding 20dp,所以得出一個結論 Padding 屬性并不會增大布局區域的大小。布局區域內部盛裝的就是這個 View 的內容了。
當我調用 View.scrollTo/scrollBy 方法時,移動的僅僅是 View 的布局區域內的內容,也就是黃色框中的區域。移動是沒有界限的,可以隨意移動到任何地方,但是顯示是有界限的,內容只能顯示在布局區域中,這也就是為什么“看我移動”四個字被上部分被截掉了。
移動的原則是以圖中布局區域的左上角為原點,以上左為正移動,注意這里的移動方向與昨天說的 View 的坐標系恰好相反。
關于 scrollTo 和 scrollBy 移動的是什么、怎么移動的已經說明白了,那就來看看他們的區別吧,很好理解。
/** * Set the scrolled position of your view. This will cause a call to * {@link #onScrollChanged(int, int, int, int)} and the view will be * invalidated. * @param x the x position to scroll to * @param y the y position to scroll to */ public void scrollTo(int x, int y) { if (mScrollX != x || mScrollY != y) { int oldX = mScrollX; int oldY = mScrollY; mScrollX = x; mScrollY = y; //回調onScrollChanged方法 onScrollChanged(mScrollX, mScrollY, oldX, oldY); if (!awakenScrollBars()) { invalidate(); } } } /** * Move the scrolled position of your view. This will cause a call to * {@link #onScrollChanged(int, int, int, int)} and the view will be * invalidated. * @param x the amount of pixels to scroll by horizontally * @param y the amount of pixels to scroll by vertically */ public void scrollBy(int x, int y) { scrollTo(mScrollX + x, mScrollY + y); }代碼很好理解,scrollTo 是將 View 的布局內容移動到一個絕對的坐標位置,scrollBy 是根據現在的偏移量,繼續偏移多少。這也就是為什么如果用 scrollTo 的方式偏移,點一次就不會再動了。如果是 scrollBy 的方式,點擊按鈕可以一直偏移。
LayoutParams 這種方式還是很好理解的,比如我直接更改 View 的 margin 屬性,就可以達到我們的目的啦。
DemoRelativeLayout.LayoutParams layoutParams = (DemoRelativeLayout.LayoutParams) btViewDemo.getLayoutParams(); layoutParams.leftMargin += 100; layoutParams.topMargin += 100; btViewDemo.setLayoutParams(layoutParams);注意這里的 LayoutParams 要強轉成當前 View 的父布局類型。
未來會比較全面地研究屬性動畫。
接下來是本文的最后一個模塊的內容。首先看看上面的兩種滑動方式,差別還是很大的。我們已經分析出 scrollTo/scrollBy 這兩種方式是對 View 中的內容進行移動,而 LayoutParams 方式移動的是 View 本身。但是它們都有一個共同點,就是移動的過程就是瞬間完成的。也就是發生了“瞬移”。這種突兀的移動給用戶的效果就不是很好了,漸變平滑的移動才是我們想要的。所以這里來說說彈性移動。
Scroller
我們可以對 View 的內容,通過 Scroller 來進行彈性滑動。Scroller 的使用典型代碼如下:
public DemoButton(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } PRivate void init() { mScroller = new Scroller(getContext()); } @Override public void computeScroll() { super.computeScroll(); if (mScroller.computeScrollOffset()) { scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); postInvalidate(); } } public void smoothScrollTo(int destX, int destY) { int scrollX = getScrollX(); int delta = destX - scrollX; mScroller.startScroll(scrollX, 0, delta, 0, 5000); invalidate(); } btViewDemo.smoothScrollTo(500,500);Scroller 的使用典型代碼就是這些。它是如何進行彈性滑動的呢?來看看 Scroller 的源碼實現吧:
/** * Start scrolling by providing a starting point, the distance to travel, * and the duration of the scroll. * * @param startX Starting horizontal scroll offset in pixels. Positive * numbers will scroll the content to the left. * @param startY Starting vertical scroll offset in pixels. Positive numbers * will scroll the content up. * @param dx Horizontal distance to travel. Positive numbers will scroll the * content to the left. * @param dy Vertical distance to travel. Positive numbers will scroll the * content up. * @param duration Duration of the scroll in milliseconds. */ 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; }首先可以看到,startScroll
方法用來記錄了起始位置、終止位置、位移、以及移動的開始時間和移動的時長 duration。并沒有真正的觸發什么方法去滑動。真正滑動開始的起始點是 startScroll
下方的 invalidate
方法。
invalidate
方法可以讓 View 調用 onDraw 方法進行重繪,未來我們會研究 View 的繪制流程。在 View 調用 onDraw 時,會調用 computeScroll
方法,在 computeScroll
方法中我們再次調用 postInvalidate
再次重繪,這樣就形成了一個閉環,滑動就這樣開始了。
我們來看看computeScrollOffset
是怎么規定計算每次的滑動距離的:
關鍵代碼都已經給了注釋,可以看到這里設計的非常巧妙,沒用計時器甚至于 View 都不相關,就實現了平滑滑動。
當然實現平滑滑動還可以用屬性動畫,這也許是最好的方式,未來再說。還可以使用 Handler 的 sendDelay 的方式,達到一段時間內,移動一段距離達到彈性滑動的效果。不過我感覺這種方式很一般,不推薦。
本文到這里就結束了,下文會寫寫我對事件分發的理解,以及 View 的繪制流程。
新聞熱點
疑難解答