歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android中滑屏初探 ---- scrollTo 以及 scrollBy方法使用說明

Android中滑屏初探 ---- scrollTo 以及 scrollBy方法使用說明

日期:2017/3/1 10:32:56   编辑:Linux編程

今天給大家介紹下Android中滑屏功能的一個基本實現過程以及原理初探,最後給大家重點講解View視圖中scrollTo 與scrollBy這兩個函數的區別 。

首先 ,我們必須明白在Android View視圖是沒有邊界的,Canvas是沒有邊界的,只不過我們通過繪制特定的View時對Canvas對象進行了一定的操作,例如 : translate(平移)、clipRect(剪切)等,以便達到我們的對該Canvas對象繪制的要求 ,我們可以將這種無邊界的視圖稱為“視圖坐標”-----它不受物理屏幕限制。通常我們所理解的一個Layout布局文件只是該視圖的顯示區域,超過了這個顯示區域將不能顯示到父視圖的區域中 ,對應的,我們可以將這種無邊界的視圖稱為“布局坐標”------ 父視圖給子視圖分配的布局(layout)大小。而且, 一個視圖的在屏幕的起始坐標位於視圖坐標起始處,如下圖所示。

這麼來說吧 ,世界本是無邊無界的,可是我們的眼睛我們的心約束了我們所看到的“世界” 。

如下所示:

黑色框框表示該子視圖的布局坐標, 褐色框框表示該子視圖的視圖坐標--該坐標是無限的,超過了父視圖給子視圖規定的區域後,不再顯示該超出內容。

示例源代碼下載:

免費下載地址在 http://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /2012年資料/2月/12日/Android中滑屏初探 ---- scrollTo 以及 scrollBy方法使用說明/

那麼下面的問題就是:如何將我們的視圖的任意坐標能顯示到該視圖的中心坐標上呢? 由於該布局位置是只能顯示特定的一塊視圖內容 ,因此我們需要通過scrollTo()或者scrollBy()方法將我們期望的視圖“滾動”至布局坐標上。

在View.java中提供了了如下兩個變量以及相應的屬性方法去讀取滾動值 ,如下: View.java類中

  1. /**
  2. * The offset, in pixels, by which the content of this view is scrolled
  3. * horizontally.
  4. * {@hide}
  5. */
  6. protected int mScrollX; //該視圖內容相當於視圖起始坐標的偏移量 , X軸 方向
  7. /**
  8. * The offset, in pixels, by which the content of this view is scrolled
  9. * vertically.
  10. * {@hide}
  11. */
  12. protected int mScrollY; //該視圖內容相當於視圖起始坐標的偏移量 , Y軸方向
  13. /**
  14. * Return the scrolled left position of this view. This is the left edge of
  15. * the displayed part of your view. You do not need to draw any pixels
  16. * farther left, since those are outside of the frame of your view on
  17. * screen.
  18. *
  19. * @return The left edge of the displayed part of your view, in pixels.
  20. */
  21. public final int getScrollX() {
  22. return mScrollX;
  23. }
  24. /**
  25. * Return the scrolled top position of this view. This is the top edge of
  26. * the displayed part of your view. You do not need to draw any pixels above
  27. * it, since those are outside of the frame of your view on screen.
  28. *
  29. * @return The top edge of the displayed part of your view, in pixels.
  30. */
  31. public final int getScrollY() {
  32. return mScrollY;
  33. }
Copyright © Linux教程網 All Rights Reserved