Launcher源碼學習 PageView中的ScrollIndicator分頁指示器

           看Launcher2源碼忽然看到分頁指示器,想看看源碼,於是記錄下看代碼的過程

           直接看代碼了,Launcher2中的PageView.java可以說是Launcher2中非常核心的代碼了,因爲可以說Launcher2的頁面顯示和滑動可以說是支撐了整個Launcher風格的一個核心,因此很多人在應用中都會參考PageView的很多地方做滑動或者頁面顯示等操作。

           今天看的是其中一個功能,分頁指示器的代碼,先看Launcher.xml中的佈局

           

    <include
        android:id="@+id/paged_view_indicator"
        layout="@layout/scroll_indicator"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom" />
          可以看到是在整個佈局中添加了一個指示器的佈局,其實這個佈局很簡單就是一個ImageView,接着看scroll_indicator.xml

       

<ImageView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:visibility="gone"
    android:alpha="0"
    android:scaleType="fitXY"
    android:src="@drawable/hotseat_scrubber_holo" />
        看到這裏就可以直接看下原圖了,可以發現是一張.9的圖片,此刻才發現原來這個指示器的圖片是由一張.9圖用fitXY的樣式橫向拉寬並分爲5個指示器圖片用來表示當前頁。

        說道fitXY這個屬性有必要看下這個地方,下面的是論壇博友總結的,如果不是很清楚可以自己動手設置下這些屬性來看效果。

         

     android:scaleType是控制圖片如何resized/moved來匹對ImageView的size。

     ImageView.ScaleType / android:scaleType值的意義區別:

     CENTER /center  按圖片的原來size居中顯示,當圖片長/寬超過View的長/寬,則截取圖片的居中部分顯示

     CENTER_CROP / centerCrop  按比例擴大圖片的size居中顯示,使得圖片長(寬)等於或大於View的長(寬)

     CENTER_INSIDE / centerInside  將圖片的內容完整居中顯示,通過按比例縮小或原來的size使得圖片長/寬等於或小於View的長/寬

     FIT_CENTER / fitCenter  把圖片按比例擴大/縮小到View的寬度,居中顯示

     FIT_END / fitEnd   把圖片按比例擴大/縮小到View的寬度,顯示在View的下部分位置

     FIT_START / fitStart  把圖片按比例擴大/縮小到View的寬度,顯示在View的上部分位置

     FIT_XY / fitXY  把圖片不按比例擴大/縮小到View的大小顯示

     MATRIX / matrix 用矩陣來繪製,動態縮小放大圖片來顯示。

        接着看代碼,這回看到的就是分頁指示器View的初始狀態了,因爲launcher2中默認的分頁指示器是不顯示的,只有在滑動的時候指示器纔會顯示,因此並不會在最開始的時候就給予初始化和設置可見,而是在後面滑動的時候動態更新指示器位置和狀態,等滑動停止則繼續隱藏。

        

    protected View getScrollingIndicator() {
        // We use mHasScrollIndicator to prevent future lookups if there is no sibling indicator
        // found
        if (mHasScrollIndicator && mScrollIndicator == null) {
            ViewGroup parent = (ViewGroup) getParent();
            if (parent != null) {
                mScrollIndicator = (View) (parent.findViewById(R.id.paged_view_indicator));
                mHasScrollIndicator = mScrollIndicator != null;
                if (mHasScrollIndicator) {
                    mScrollIndicator.setVisibility(View.VISIBLE);
                }
            }
        }
        return mScrollIndicator;
    }
       接下來是更新指示器的代碼

       

    private void updateScrollingIndicator() {
        if (getChildCount() <= 1) return;
        if (!isScrollingIndicatorEnabled()) return;

        getScrollingIndicator();
        if (mScrollIndicator != null) {
            updateScrollingIndicatorPosition();
        }
        if (mShouldShowScrollIndicator) {
            showScrollingIndicator(mShouldShowScrollIndicatorImmediately);
        }
    }
       其中updateScrollingIndicatorPosition這個函數是設置和更新指示器詳細參數的函數

       

    private void updateScrollingIndicatorPosition() {
        if (!isScrollingIndicatorEnabled()) return;
        if (mScrollIndicator == null) return;
        int numPages = getChildCount();
        int pageWidth = getMeasuredWidth();
        int lastChildIndex = Math.max(0, getChildCount() - 1);
        int maxScrollX = getChildOffset(lastChildIndex) - getRelativeChildOffset(lastChildIndex);
        int trackWidth = pageWidth - mScrollIndicatorPaddingLeft - mScrollIndicatorPaddingRight;
        int indicatorWidth = mScrollIndicator.getMeasuredWidth() -
                mScrollIndicator.getPaddingLeft() - mScrollIndicator.getPaddingRight();

        float offset = Math.max(0f, Math.min(1f, (float) getScrollX() / maxScrollX));
        int indicatorSpace = trackWidth / numPages;
        int indicatorPos = (int) (offset * (trackWidth - indicatorSpace)) + mScrollIndicatorPaddingLeft;
        if (hasElasticScrollIndicator()) {
            if (mScrollIndicator.getMeasuredWidth() != indicatorSpace) {
                mScrollIndicator.getLayoutParams().width = indicatorSpace;
                mScrollIndicator.requestLayout();
            }
        } else {
            int indicatorCenterOffset = indicatorSpace / 2 - indicatorWidth / 2;
            indicatorPos += indicatorCenterOffset;
        }
        mScrollIndicator.setTranslationX(indicatorPos);
    }
         可以大概看出其中的重要參數的意義,

        pageWidth  是當前顯示的頁的寬度,即我們launcher中workspace顯示的那個區域寬度

        trackWidth  是實際指示器需要顯示的範圍寬度

        indicatorSpace 是每個單獨的指示器圖片的實際顯示寬度

        indicatorPos 是實際的指示器圖片顯示位置偏移量

       以上就是指示器的顯示和更新的詳細設置了,另外就是每次翻頁的時候是如何調用的問題了

       這裏可以參考下代碼,無非就是設置當前頁數和指定某一頁的時候會用到

       

    void setCurrentPage(int currentPage) {
        if (!mScroller.isFinished()) {
            mScroller.abortAnimation();
        }
        // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
        // the default
        if (getChildCount() == 0) {
            return;
        }

        mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
        updateCurrentPageScroll();
        updateScrollingIndicator();
        notifyPageSwitchListener();
        invalidate();
    }
         可以看出就是當前頁變化的時候會更新分頁指示器的狀態並有一個動畫的過程,如果需要修改分頁指示器應該要從這裏入手了。

         後面的工作就是想想怎麼修改了,代碼暫時就到這裏了,感覺寫的好亂估計後面自己都看不懂了,不過起碼能看的懂大概流程,這就沒算浪費時間了。


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