關於View的ScrollTo, getScrollX 和 getScrollY

[FROM] http://blog.csdn.net/linmiansheng/article/details/17767795


當利用 Scroller 去滑動屏幕或者擴展 ScrollView 的時候,總是會用到 getScrollX 和 getScrollY 去獲取當前View 滑動到的位置,那麼getScrollX() 和 getScrollY() 獲取的到底是什麼呢? 

由於getScrollX 和 getScrollY 本質 上是一樣的東西,下面只說明一下getScrollX, 一般是在屏幕上面左右划動的時候會去獲取這個值。

請看下圖:


圖上面,褐色的框,其實就是我們眼睛看到的手機界面,就是一個窗口。

而綠色的長方體呢,就是一塊可以左右拉動的幕布啦,其實也就是我們要顯示在窗口上面的內容,它其實是可以很大的,大到無限大,只是沒在窗口中間的,所以我們就看不到。

而getScrollX 其實獲取的值,就是這塊 幕布在窗口左邊界時候的值了,而幕布上面哪個點是原點(0,0)呢?就是初始化時內容顯示的位置。

所以當我們將幕布往右推動的時候,幕布在窗口左邊界的值就會在0的左邊(-100),而向左推動,則其值會是在0的右邊(100)。


下面以一個實際例子來看一下。

隨便在CSDN上面截了一下圖,


我們在一個LinearLayout 裏面定義了三個TextView 來顯示 getScrollX() 的值,三個ImageView來顯示圖片, 都是指向同一張圖片,佈局如下:

[html] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="#000000"  
  6.     android:orientation="vertical" >  
  7.     <TextView  
  8.         android:id="@+id/textView"  
  9.         android:textColor="#FFFFFF"  
  10.         android:textSize="16sp"  
  11.         android:layout_height="wrap_content"   
  12.         android:layout_width="match_parent"/>  
  13.     <ImageView   
  14.         android:id="@+id/scrollImageView"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:src="@drawable/scroll_testing"  
  18.         android:contentDescription="Testing Scrolling"/>  
  19.     <TextView  
  20.         android:id="@+id/textViewToRight"  
  21.         android:textColor="#FFFFFF"  
  22.         android:textSize="16sp"  
  23.         android:layout_height="wrap_content"   
  24.         android:layout_width="match_parent"/>  
  25.     <ImageView   
  26.         android:id="@+id/scrollImageViewToRight"  
  27.         android:layout_width="wrap_content"  
  28.         android:layout_height="wrap_content"  
  29.         android:src="@drawable/scroll_testing"  
  30.         android:contentDescription="Testing Scrolling"/>  
  31.     <TextView  
  32.         android:id="@+id/textViewToLeft"  
  33.         android:textColor="#FFFFFF"  
  34.         android:textSize="16sp"  
  35.         android:layout_height="wrap_content"   
  36.         android:layout_width="match_parent"/>  
  37.     <ImageView   
  38.         android:id="@+id/scrollImageViewToLeft"  
  39.         android:layout_width="wrap_content"  
  40.         android:layout_height="wrap_content"  
  41.         android:src="@drawable/scroll_testing"  
  42.         android:contentDescription="Testing Scrolling"/>  
  43.   
  44. </LinearLayout>  

然後我們在Activity 中,分別對下面兩張ImageView 進行 scrollTo 操作,然後獲取其getScrollX()  的值,放到對應的TextView 上面,其代碼如下:

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. public class ScrollActivity extends Activity{  
  2.   
  3.     protected void onCreate(Bundle savedInstanceState){  
  4.         super.onCreate(savedInstanceState);  
  5.         setContentView(R.layout.scroll_layout);  
  6.           
  7.         ImageView imageView = (ImageView) findViewById(R.id.scrollImageView);  
  8.         TextView textView = (TextView) findViewById(R.id.textView);  
  9.         textView.setText("getScrollX() = " + imageView.getScrollX());  
  10.           
  11.         ImageView imageViewToRight = (ImageView) findViewById(R.id.scrollImageViewToRight);  
  12.         imageViewToRight.scrollTo(-1000);  
  13.         TextView textViewToRight = (TextView) findViewById(R.id.textViewToRight);  
  14.         textViewToRight.setText("getScrollX() = " + imageViewToRight.getScrollX());  
  15.           
  16.         ImageView imageViewToLeft = (ImageView) findViewById(R.id.scrollImageViewToLeft);  
  17.         imageViewToLeft.scrollTo(1000);  
  18.         TextView textViewToLeft = (TextView) findViewById(R.id.textViewToLeft);  
  19.         textViewToLeft.setText("getScrollX() = " + imageViewToLeft.getScrollX());  
  20.     }  
  21. }  

下面是運行後的效果


可以看到,正如上面所說的,向右滾動的時候,等於是把背後的幕布向右推動,使得沒有內容的幕布(X < 0)顯示出來, 而向左滾動,則是把幕左向左推動,讓右邊的內容(X > 0)移到窗口的左邊緣上。


在View上面還一個叫ScrollBy的函數,跟ScrollTo的區別在於,ScrollTo 是 到那個位置,ScrollBy 是經過這段位置,這個從英文的To 跟 By 來理解就很簡單了。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章