scrollTo vs scrollBy

轉載出處:http://blog.csdn.net/qinjuning

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

   scrollBy這兩個函數的區別 。

  

        首先 ,我們必須明白在Android View視圖是沒有邊界的,Canvas是沒有邊界的,只不過我們通過繪製特定的View時對

   Canvas對象進行了一定的操作,例如 : translate(平移)、clipRect(剪切)等,以便達到我們的對該Canvas對象繪製的要求 ,

   我們可以將這種無邊界的視圖稱爲“視圖座標”-----它不受物理屏幕限制。通常我們所理解的一個Layout佈局文件只是該視

   圖的顯示區域,超過了這個顯示區域將不能顯示到父視圖的區域中 ,對應的,我們可以將這種有邊界的視圖稱爲“佈局座標

    ------ 父視圖給子視圖分配的佈局(layout)大小。而且, 一個視圖的在屏幕的起始座標位於視圖座標起始處,如下圖所示。

 

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

 

       如下所示:

            

              

              黑色框框表示該子視圖的佈局座標, 褐色框框表示該子視圖的視圖座標--該座標是無限的,超過了父視圖給子視圖

       規定的區域後,不再顯示該超出內容。

 

          那麼下面的問題就是:如何將我們的視圖的任意座標能顯示到該視圖的中心座標上呢? 由於該佈局位置是隻能顯示特定的

  一塊視圖內容 ,因此我們需要通過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.     /** 
  15.      * Return the scrolled left position of this view. This is the left edge of 
  16.      * the displayed part of your view. You do not need to draw any pixels 
  17.      * farther left, since those are outside of the frame of your view on 
  18.      * screen. 
  19.      * 
  20.      * @return The left edge of the displayed part of your view, in pixels. 
  21.      */  
  22.     public final int getScrollX() {  
  23.         return mScrollX;  
  24.     }  
  25.   
  26.     /** 
  27.      * Return the scrolled top position of this view. This is the top edge of 
  28.      * the displayed part of your view. You do not need to draw any pixels above 
  29.      * it, since those are outside of the frame of your view on screen. 
  30.      * 
  31.      * @return The top edge of the displayed part of your view, in pixels. 
  32.      */  
  33.     public final int getScrollY() {  
  34.         return mScrollY;  
  35.     }  


 

      注意,所謂的“by which the content of this view is scrolled”表示該偏移量只針對於該View中onDraw()方法裏的

  具體內容實現,而不針對繪製背景圖片等 。具體原因可參考<Android中View繪製流程以及invalidate()等相關方法分析>

 

     提示:下文中提到的當前視圖內容是在繪製在佈局座標處的內容。

 

     public void scrollTo(int x, int y)

              說明:在當前視圖內容偏移至(x , y)座標處,即顯示(可視)區域位於(x , y)座標處。

        方法原型爲: View.java類中

  1. /** 
  2.  * Set the scrolled position of your view. This will cause a call to 
  3.  * {@link #onScrollChanged(int, int, int, int)} and the view will be 
  4.  * invalidated. 
  5.  * @param x the x position to scroll to 
  6.  * @param y the y position to scroll to 
  7.  */  
  8. public void scrollTo(int x, int y) {  
  9.     //偏移位置發生了改變  
  10.     if (mScrollX != x || mScrollY != y) {  
  11.         int oldX = mScrollX;  
  12.         int oldY = mScrollY;  
  13.         mScrollX = x;  //賦新值,保存當前便宜量  
  14.         mScrollY = y;  
  15.         //回調onScrollChanged方法  
  16.         onScrollChanged(mScrollX, mScrollY, oldX, oldY);  
  17.         if (!awakenScrollBars()) {  
  18.             invalidate();  //一般都引起重繪  
  19.         }  
  20.     }  
  21. }  

 

     public void scrollBy(int x, int y)    

            說明:在當前視圖內容繼續偏移(x , y)個單位,顯示(可視)區域也跟着偏移(x,y)個單位。

        方法原型爲: View.java類中

  1. /** 
  2.    * Move the scrolled position of your view. This will cause a call to 
  3.    * {@link #onScrollChanged(int, int, int, int)} and the view will be 
  4.    * invalidated. 
  5.    * @param x the amount of pixels to scroll by horizontally 
  6.    * @param y the amount of pixels to scroll by vertically 
  7.    */  
  8.   // 看出原因了吧 。。 mScrollX 與 mScrollY 代表我們當前偏移的位置 , 在當前位置繼續偏移(x ,y)個單位  
  9.   public void scrollBy(int x, int y) {  
  10.       scrollTo(mScrollX + x, mScrollY + y);  
  11.   }  


        

             

       第一個小Demo非常簡單 ,大家重點理解與掌握scrollTo() 與 scrollBy()函數的用法和區別。

 

       第二個小Demo則有了Launcher的模樣,能夠左右切換屏幕 。實現功能如下: 採用了一個自定義ViewGroup,該ViewGroup

   對象包含了3個LinearLayout子視圖,並且以一定的佈局座標(由layout()方法指定)顯示在ViewGroup上。 接下來,即可調用該

   ViewGroup對象的scrollTo或者scrollBy()方法切換指定視圖內容了,即切換屏幕。 呵呵 ,挺好玩的吧 。

 

       如果對View繪製流程不懂的,可以參考我的這篇博客<Android中View繪製流程以及invalidate()等相關方法分析> 。

       截圖如下:

                           

       自定義ViewGroup如下:

 

  1. //自定義ViewGroup , 包含了三個LinearLayout控件,存放在不同的佈局位置,通過scrollBy或者scrollTo方法切換  
  2. public class MultiViewGroup extends ViewGroup {  
  3.   
  4.     private Context mContext;  
  5.   
  6.     private static String TAG = "MultiViewGroup";  
  7.   
  8.     public MultiViewGroup(Context context) {  
  9.         super(context);  
  10.         mContext = context;  
  11.         init();  
  12.     }  
  13.   
  14.     public MultiViewGroup(Context context, AttributeSet attrs) {  
  15.         super(context, attrs);  
  16.         mContext = context;  
  17.         init();  
  18.     }  
  19.   
  20.     private void init() {  
  21.         // 初始化3個 LinearLayout控件  
  22.         LinearLayout oneLL = new LinearLayout(mContext);  
  23.         oneLL.setBackgroundColor(Color.RED);  
  24.         addView(oneLL);  
  25.           
  26.         LinearLayout twoLL = new LinearLayout(mContext);  
  27.         twoLL.setBackgroundColor(Color.YELLOW);  
  28.         addView(twoLL);  
  29.           
  30.         LinearLayout threeLL = new LinearLayout(mContext);  
  31.         threeLL.setBackgroundColor(Color.BLUE);  
  32.         addView(threeLL);  
  33.     }  
  34.   
  35.     // measure過程  
  36.     @Override  
  37.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  38.   
  39.         Log.i(TAG, "--- start onMeasure --");  
  40.   
  41.         // 設置該ViewGroup的大小  
  42.         int width = MeasureSpec.getSize(widthMeasureSpec);  
  43.         int height = MeasureSpec.getSize(heightMeasureSpec);  
  44.         setMeasuredDimension(width, height);  
  45.   
  46.         int childCount = getChildCount();  
  47.         Log.i(TAG, "--- onMeasure childCount is -->" + childCount);  
  48.         for (int i = 0; i < childCount; i++) {  
  49.             View child = getChildAt(i);  
  50.             // 設置每個子視圖的大小 , 即全屏  
  51.             child.measure(MultiScreenActivity.screenWidth, MultiScreenActivity.scrrenHeight);  
  52.         }  
  53.     }  
  54.   
  55.     // layout過程  
  56.     @Override  
  57.     protected void onLayout(boolean changed, int l, int t, int r, int b) {  
  58.         // TODO Auto-generated method stub  
  59.         Log.i(TAG, "--- start onLayout --");  
  60.         int startLeft = 0// 每個子視圖的起始佈局座標  
  61.         int startTop = 10// 間距設置爲10px 相當於 android:marginTop= "10px"  
  62.         int childCount = getChildCount();  
  63.         Log.i(TAG, "--- onLayout childCount is -->" + childCount);  
  64.         for (int i = 0; i < childCount; i++) {  
  65.             View child = getChildAt(i);  
  66.             child.layout(startLeft, startTop,   
  67.                     startLeft + MultiScreenActivity.screenWidth,   
  68.                     startTop + MultiScreenActivity.scrrenHeight);  
  69.             startLeft = startLeft + MultiScreenActivity.screenWidth ; //校準每個子View的起始佈局位置  
  70.             //三個子視圖的在屏幕中的分佈如下 [0 , 320] / [320,640] / [640,960]  
  71.         }  
  72.     }  
  73.   
  74. }  


 

          PS  :大家可以分別給這幾個LinearLayout試着添加幾個子View,例如TextView, Button等

 

        至於Launcher上滑屏功能的實現,我嘗試着去掌握,可能天資愚鈍吧,對Scoller類很是感冒,現今還沒有掌握好,不過在此

   給大家推薦幾個不錯的學習資源 。 以後有需要的話,還是採用拿來主義吧。 囧

 

        1、 Scoller類介紹:android 中文 api (64) —— Scroller

        2、相關資源彙總:http://blog.csdn.net/dellheng/article/details/7164275 

        3,launcher修改--左右滑動屏幕切換源碼追蹤

 

         示例源代碼位於: http://download.csdn.net/detail/qinjuning/4054840


發佈了34 篇原創文章 · 獲贊 115 · 訪問量 33萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章