[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來顯示圖片, 都是指向同一張圖片,佈局如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#000000"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/textView"
- android:textColor="#FFFFFF"
- android:textSize="16sp"
- android:layout_height="wrap_content"
- android:layout_width="match_parent"/>
- <ImageView
- android:id="@+id/scrollImageView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/scroll_testing"
- android:contentDescription="Testing Scrolling"/>
- <TextView
- android:id="@+id/textViewToRight"
- android:textColor="#FFFFFF"
- android:textSize="16sp"
- android:layout_height="wrap_content"
- android:layout_width="match_parent"/>
- <ImageView
- android:id="@+id/scrollImageViewToRight"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/scroll_testing"
- android:contentDescription="Testing Scrolling"/>
- <TextView
- android:id="@+id/textViewToLeft"
- android:textColor="#FFFFFF"
- android:textSize="16sp"
- android:layout_height="wrap_content"
- android:layout_width="match_parent"/>
- <ImageView
- android:id="@+id/scrollImageViewToLeft"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/scroll_testing"
- android:contentDescription="Testing Scrolling"/>
- </LinearLayout>
然後我們在Activity 中,分別對下面兩張ImageView 進行 scrollTo 操作,然後獲取其getScrollX() 的值,放到對應的TextView 上面,其代碼如下:
- public class ScrollActivity extends Activity{
- protected void onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.scroll_layout);
- ImageView imageView = (ImageView) findViewById(R.id.scrollImageView);
- TextView textView = (TextView) findViewById(R.id.textView);
- textView.setText("getScrollX() = " + imageView.getScrollX());
- ImageView imageViewToRight = (ImageView) findViewById(R.id.scrollImageViewToRight);
- imageViewToRight.scrollTo(-100, 0);
- TextView textViewToRight = (TextView) findViewById(R.id.textViewToRight);
- textViewToRight.setText("getScrollX() = " + imageViewToRight.getScrollX());
- ImageView imageViewToLeft = (ImageView) findViewById(R.id.scrollImageViewToLeft);
- imageViewToLeft.scrollTo(100, 0);
- TextView textViewToLeft = (TextView) findViewById(R.id.textViewToLeft);
- textViewToLeft.setText("getScrollX() = " + imageViewToLeft.getScrollX());
- }
- }
下面是運行後的效果
可以看到,正如上面所說的,向右滾動的時候,等於是把背後的幕布向右推動,使得沒有內容的幕布(X < 0)顯示出來, 而向左滾動,則是把幕左向左推動,讓右邊的內容(X > 0)移到窗口的左邊緣上。
在View上面還一個叫ScrollBy的函數,跟ScrollTo的區別在於,ScrollTo 是 到那個位置,ScrollBy 是經過這段位置,這個從英文的To 跟 By 來理解就很簡單了。
- 頂