1. 程式人生 > >Android Gallery3d原始碼學習總結(二)——繪製流程drawThumbnails

Android Gallery3d原始碼學習總結(二)——繪製流程drawThumbnails

此函式控制相簿表格頁、相片表格頁、時間分類表格頁的展示,非常重要。以下以相簿表格頁為例進行講解,其他的就舉一反三吧。
準備輸入引數

  1. final GridDrawables drawables = mDrawables;
  2.         final DisplayList displayList = mDisplayList;
  3.         final DisplayItem[] displayItems = mDisplayItems;
  4.         final int firstBufferedVisibleSlot = mBufferedVisibleRange.begin;
  5.         final int lastBufferedVisibleSlot = mBufferedVisibleRange.end;
  6.         final int firstVisibleSlot = mVisibleRange.begin;
  7.         final int lastVisibleSlot = mVisibleRange.end;
  8.         final int selectedSlotIndex = mSelectedSlot;
  9.         final int currentFocusSlot = mCurrentFocusSlot;
  10.         final int currentScaleSlot = mCurrentScaleSlot;
  11.         final DisplayItem[] itemsDrawn = mItemsDrawn;
  12.         itemsDrawn[0] = null; // No items drawn yet.
  13.         int drawnCounter = 0;
  14.         final GridQuad grid = GridDrawables.sGrid;
  15.         grid.bindArrays(gl);
  16.         int numTexturesQueued = 0;
  17.         Context context = view.getContext();
複製程式碼

通 過之前的computeVisibleItems(),已得到顯示元素列表,顯示元素緩衝陣列,快取可見槽位的範圍,可見槽位的範圍;得到當前選中的槽位 索引selectedSlotIndex、當前焦點槽位索引currentFocusSlot和縮放槽位索引currentScaleSlot。
已畫元素的快取陣列,繫結表格類材質。selectedSlotIndex、currentFocusSlot、currentScaleSlot在不進行任何操作時都是-1.

遍歷快取可見槽位範圍中的每一個槽位

  1. for (int itrSlotIndex = firstBufferedVisibleSlot; itrSlotIndex <= lastBufferedVisibleSlot; ++itrSlotIndex) {
  2.         int index = itrSlotIndex;
  3.         ...        
  4.         }
複製程式碼

即遍歷每個相簿,index是相簿序號。

為每個相簿分別計算“需要繪製“的最開頭那張照片的序號

  1.             boolean priority = !(index < firstVisibleSlot || index > lastVisibleSlot);
  2.             int startSlotIndex = 0;
  3.             final int maxDisplayedItemsPerSlot = (index == mCurrentScaleSlot) ? GridLayer.MAX_DISPLAYED_ITEMS_PER_FOCUSED_SLOT
  4.                     : GridLayer.MAX_DISPLAYED_ITEMS_PER_SLOT;
  5.             if (index != mCurrentScaleSlot) {
  6.                 for (int j = maxDisplayedItemsPerSlot - 1; j >= 0; --j) {
  7.                     DisplayItem displayItem = displayItems[(index - firstBufferedVisibleSlot) * GridLayer.MAX_ITEMS_PER_SLOT + j];
  8.                     if (displayItem == null) {
  9.                         continue;
  10.                     } else {
  11.                         Texture texture = displayItem.getThumbnailImage(context, sThumbnailConfig);
  12.                         if (texture != null && texture.isLoaded() == false) {
  13.                             startSlotIndex = j;
  14.                             break;
  15.                         }
  16.                     }
  17.                 }
  18.             }
複製程式碼

priority表示當前槽位在[firstVisibleSlot,lastVisibleSlot]範圍中,則為高優先順序,可見槽位上的自然是最高優先順序嘍;
maxDisplayedItemsPerSlot表示每個槽位中最多的顯示專案,即每個相簿封面的最大縮圖數,此處為4;
startSlotIndex是當前幀 每個相簿中“需要繪製“的最開頭那張照片的序號(取值在0-3之間),注意“需要繪製“的最開頭那張照片往往不是相簿中的第一張照片,幀動畫開始時它通常 是第四張照片(第四張被依次壓在321張照片下面嘛,因此繪製載入材質都要按照第四張向首張的次序),隨著動畫和材質逐步載入,它慢慢變為第三張、第二 張、第一張。

從第四張向第一張載入材質

  1. // Prime the textures in the reverse order.
  2.             for (int j = 0; j < maxDisplayedItemsPerSlot; ++j) {
  3.                 int stackIndex = (index == mCurrentScaleSlot) ? maxDisplayedItemsPerSlot - j - 1 : j;
  4.                 DisplayItem displayItem = displayItems[(index - firstBufferedVisibleSlot) * GridLayer.MAX_ITEMS_PER_SLOT
  5.                         + stackIndex];
  6.                 if (displayItem == null) {
  7.                     continue;
  8.                 } else {
  9.                     displayItem.mCurrentSlotIndex = index;
  10.                     if (selectedSlotIndex != Shared.INVALID && (index <= selectedSlotIndex - 2 || index >= selectedSlotIndex + 2)) {
  11.                         displayItem.clearScreennailImage();
  12.                     }
  13.                     Texture texture = displayItem.getThumbnailImage(context, sThumbnailConfig);
  14.                     if (index == mCurrentScaleSlot && texture != null && !texture.isLoaded()) {
  15.                         view.prime(texture, true);
  16.                         view.bind(texture);
  17.                     } else if (texture != null && !texture.isLoaded() /*&& numTexturesQueued <= 6*/) {
  18.                         /*boolean isCached = texture.isCached();*/
  19.                         view.prime(texture, priority);
  20.                         view.bind(texture);
  21.                         /*if (priority && isCached && texture.mState != Texture.STATE_ERROR)
  22.                             ++numTexturesQueued;*/
  23.                     }
  24.                 }
  25.             }
複製程式碼

因為是正序的處理4個元素,插入隊頭,因此是按照從第四張向第一張的次序載入材質。無用的程式碼已經註釋掉了。

準備一些變數

  1. if (itrSlotIndex == selectedSlotIndex) {
  2.                 continue;
  3.             }
  4.             view.prime(drawables.mTexturePlaceholder, true);
  5.             Texture placeholder = (state == GridLayer.STATE_GRID_VIEW) ? drawables.mTexturePlaceholder : null;
  6.             final boolean pushDown = (state == GridLayer.STATE_GRID_VIEW || state == GridLayer.STATE_FULL_SCREEN) ? false : true;
複製程式碼

載入佔位材質,自己找找看是那個圖示;如果是相片表格檢視,則佔位材質使用這個圖示,否則佔位材質為空;如果是相片表格檢視,則長按選中後向上彈起,否則向下陷下去。

調整顯示專案和相機的偏移量

  1. for (int j = 0; j < GridLayer.MAX_ITEMS_PER_SLOT; ++j) {
  2.                 DisplayItem displayItem = displayItems[(index - firstBufferedVisibleSlot) * GridLayer.MAX_ITEMS_PER_SLOT + j];
  3.                 if (displayItem == null)
  4.                     continue;
  5.                 Texture texture = displayItem.getThumbnailImage(context, sThumbnailConfig);
  6.                 if (texture == null || !texture.isLoaded()) {
  7.                     if (currentScaleSlot != index) {
  8.                         if (j == 0) {
  9.                             final MediaSet parentSet = displayItem.mItemRef.mParentMediaSet;
  10.                             if (parentSet != null && parentSet.getNumItems() <= 1) {
  11.                                 displayList.setAlive(displayItem, false);
  12.                             }
  13.                         } else {
  14.                             displayList.setAlive(displayItem, false);
  15.                         }
  16.                     }
  17.                 }
  18.                 final float dx1 = mScaleGestureDetector.getTopFingerDeltaX();
  19.                 final float dy1 = mScaleGestureDetector.getTopFingerDeltaY();
  20.                 final float dx2 = mScaleGestureDetector.getBottomFingerDeltaX();
  21.                 final float dy2 = mScaleGestureDetector.getBottomFingerDeltaY();
  22.                 final float span = mScaleGestureDetector.getCurrentSpan();
  23.                 if (state == GridLayer.STATE_FULL_SCREEN) {
  24.                     displayList.setOffset(displayItem, false, true, span, dx1, dy1, dx2, dy2);
  25.                 } else {
  26.                     if (!mHoldPosition) {
  27.                         if (state != GridLayer.STATE_GRID_VIEW) {
  28.                             if (currentScaleSlot == index) {
  29.                                 displayList.setOffset(displayItem, true, false, span, dx1, dy1, dx2, dy2);
  30.                             } else if (currentScaleSlot != Shared.INVALID) {
  31.                                 displayList.setOffset(displayItem, true, true, span, dx1, dy1, dx2, dy2);
  32.                             } else {
  33.                                 displayList.setOffset(displayItem, false, false, span, dx1, dy1, dx2, dy2);
  34.                             }
  35.                         } else {
  36.                             float minVal = -1.0f;
  37.                             float maxVal = GridCamera.EYE_Z * 0.5f;
  38.                             float zVal = minVal + mSpreadValue;
  39.                             zVal = FloatUtils.clamp(zVal, minVal, maxVal);
  40.                             if (Float.isInfinite(zVal) || Float.isNaN(zVal)) {
  41.                                 mCamera.moveZTo(0);
  42.                             } else {
  43.                                 mCamera.moveZTo(-zVal);
  44.                             }
  45.                         }
  46.                     }
  47.                 }
  48.             }
複製程式碼

第一部分每太看懂,第二部分是得到多點觸碰的輸入資訊,第三部分根據檢視調整了照片偏移量和相機距離。

繪製縮圖顯示專案

  1. for (int j = startSlotIndex; j < GridLayer.MAX_ITEMS_PER_SLOT; ++j) {
  2.                 DisplayItem displayItem = displayItems[(index - firstBufferedVisibleSlot) * GridLayer.MAX_ITEMS_PER_SLOT + j];
  3.                 if (displayItem == null) {
  4.                     break;
  5.                 } else {
  6.                     if (currentFocusSlot == index) {
  7.                         displayList.setHasFocus(displayItem, true, pushDown);
  8.                         mTargetFocusMixRatio = 1.0f;
  9.                     } else {
  10.                         displayList.setHasFocus(displayItem, false, pushDown);
  11.                     }
  12.                     if (j >= maxDisplayedItemsPerSlot)
  13.                         continue;
  14.                     Texture texture = displayItem.getThumbnailImage(view.getContext(), sThumbnailConfig);
  15.                     if (texture != null) {
  16.                         if (index == mCurrentScaleSlot)
  17.                             displayItem.mAlive = true;
  18.                         if ((!displayItem.isAnimating() || !texture.isLoaded())
  19.                                 && displayItem.getStackIndex() > GridLayer.MAX_ITEMS_PER_SLOT) {
  20.                             displayList.setAlive(displayItem, true);
  21.                             continue;
  22.                         }
  23.                         if (index < firstVisibleSlot || index > lastVisibleSlot) {
  24.                             if (view.bind(texture)) {
  25.                                 displayList.setAlive(displayItem, true);
  26.                             }
  27.                             continue;
  28.                         }
  29.                         drawDisplayItem(view, gl, displayItem, texture, PASS_THUMBNAIL_CONTENT, placeholder,
  30.                                 displayItem.mAnimatedPlaceholderFade);
  31.                     } else {
  32.                         // Move on to the next stack.
  33.                         break;
  34.                     }
  35.                     if (drawnCounter >= GridLayer.MAX_ITEMS_DRAWABLE - 1 || drawnCounter < 0) {
  36.                         break;
  37.                     }
  38.                     // Insert in order of z.
  39.                     itemsDrawn[drawnCounter++] = displayItem;
  40.                     itemsDrawn[drawnCounter] = null;
  41.                 }
  42.             }
複製程式碼

如果是當前選中,則根據當前檢視選擇下陷還是彈起;繪製縮圖(其中的displayList.setAlive還沒有看懂有什麼用);記錄已繪製的items。