Android Gallery3d原始碼學習總結(二)——繪製流程drawThumbnails
此函式控制相簿表格頁、相片表格頁、時間分類表格頁的展示,非常重要。以下以相簿表格頁為例進行講解,其他的就舉一反三吧。
準備輸入引數
- final GridDrawables drawables = mDrawables;
- final DisplayList displayList = mDisplayList;
- final DisplayItem[] displayItems = mDisplayItems;
- final int firstBufferedVisibleSlot = mBufferedVisibleRange.begin;
- final int lastBufferedVisibleSlot = mBufferedVisibleRange.end;
- final int firstVisibleSlot = mVisibleRange.begin;
- final int lastVisibleSlot = mVisibleRange.end;
- final int selectedSlotIndex = mSelectedSlot;
- final int currentFocusSlot = mCurrentFocusSlot;
- final int currentScaleSlot = mCurrentScaleSlot;
- final DisplayItem[] itemsDrawn = mItemsDrawn;
- itemsDrawn[0] = null; // No items drawn yet.
- int drawnCounter = 0;
- final GridQuad grid = GridDrawables.sGrid;
- grid.bindArrays(gl);
- int numTexturesQueued = 0;
- Context context = view.getContext();
通
過之前的computeVisibleItems(),已得到顯示元素列表,顯示元素緩衝陣列,快取可見槽位的範圍,可見槽位的範圍;得到當前選中的槽位
索引selectedSlotIndex、當前焦點槽位索引currentFocusSlot和縮放槽位索引currentScaleSlot。
已畫元素的快取陣列,繫結表格類材質。selectedSlotIndex、currentFocusSlot、currentScaleSlot在不進行任何操作時都是-1.
遍歷快取可見槽位範圍中的每一個槽位
- for (int itrSlotIndex = firstBufferedVisibleSlot; itrSlotIndex <= lastBufferedVisibleSlot; ++itrSlotIndex) {
- int index = itrSlotIndex;
- ...
- }
即遍歷每個相簿,index是相簿序號。
為每個相簿分別計算“需要繪製“的最開頭那張照片的序號
- boolean priority = !(index < firstVisibleSlot || index > lastVisibleSlot);
- int startSlotIndex = 0;
- final int maxDisplayedItemsPerSlot = (index == mCurrentScaleSlot) ? GridLayer.MAX_DISPLAYED_ITEMS_PER_FOCUSED_SLOT
- : GridLayer.MAX_DISPLAYED_ITEMS_PER_SLOT;
- if (index != mCurrentScaleSlot) {
- for (int j = maxDisplayedItemsPerSlot - 1; j >= 0; --j) {
- DisplayItem displayItem =
displayItems[(index - firstBufferedVisibleSlot) *
GridLayer.MAX_ITEMS_PER_SLOT + j];
- if (displayItem == null) {
- continue;
- } else {
- Texture texture = displayItem.getThumbnailImage(context, sThumbnailConfig);
- if (texture != null && texture.isLoaded() == false) {
- startSlotIndex = j;
- break;
- }
- }
- }
- }
priority表示當前槽位在[firstVisibleSlot,lastVisibleSlot]範圍中,則為高優先順序,可見槽位上的自然是最高優先順序嘍;
maxDisplayedItemsPerSlot表示每個槽位中最多的顯示專案,即每個相簿封面的最大縮圖數,此處為4;
startSlotIndex是當前幀
每個相簿中“需要繪製“的最開頭那張照片的序號(取值在0-3之間),注意“需要繪製“的最開頭那張照片往往不是相簿中的第一張照片,幀動畫開始時它通常
是第四張照片(第四張被依次壓在321張照片下面嘛,因此繪製載入材質都要按照第四張向首張的次序),隨著動畫和材質逐步載入,它慢慢變為第三張、第二
張、第一張。
從第四張向第一張載入材質
- // Prime the textures in the reverse order.
- for (int j = 0; j < maxDisplayedItemsPerSlot; ++j) {
- int stackIndex = (index == mCurrentScaleSlot) ? maxDisplayedItemsPerSlot - j - 1 : j;
- DisplayItem displayItem = displayItems[(index - firstBufferedVisibleSlot) * GridLayer.MAX_ITEMS_PER_SLOT
- + stackIndex];
- if (displayItem == null) {
- continue;
- } else {
- displayItem.mCurrentSlotIndex = index;
- if (selectedSlotIndex != Shared.INVALID
&& (index <= selectedSlotIndex - 2 || index >=
selectedSlotIndex + 2)) {
- displayItem.clearScreennailImage();
- }
- Texture texture = displayItem.getThumbnailImage(context, sThumbnailConfig);
- if (index == mCurrentScaleSlot && texture != null && !texture.isLoaded()) {
- view.prime(texture, true);
- view.bind(texture);
- } else if (texture != null && !texture.isLoaded() /*&& numTexturesQueued <= 6*/) {
- /*boolean isCached = texture.isCached();*/
- view.prime(texture, priority);
- view.bind(texture);
- /*if (priority && isCached && texture.mState != Texture.STATE_ERROR)
- ++numTexturesQueued;*/
- }
- }
- }
因為是正序的處理4個元素,插入隊頭,因此是按照從第四張向第一張的次序載入材質。無用的程式碼已經註釋掉了。
準備一些變數
- if (itrSlotIndex == selectedSlotIndex) {
- continue;
- }
- view.prime(drawables.mTexturePlaceholder, true);
- Texture placeholder = (state == GridLayer.STATE_GRID_VIEW) ? drawables.mTexturePlaceholder : null;
- final boolean pushDown = (state == GridLayer.STATE_GRID_VIEW || state == GridLayer.STATE_FULL_SCREEN) ? false : true;
載入佔位材質,自己找找看是那個圖示;如果是相片表格檢視,則佔位材質使用這個圖示,否則佔位材質為空;如果是相片表格檢視,則長按選中後向上彈起,否則向下陷下去。
調整顯示專案和相機的偏移量
- for (int j = 0; j < GridLayer.MAX_ITEMS_PER_SLOT; ++j) {
- DisplayItem displayItem = displayItems[(index - firstBufferedVisibleSlot) * GridLayer.MAX_ITEMS_PER_SLOT + j];
- if (displayItem == null)
- continue;
- Texture texture = displayItem.getThumbnailImage(context, sThumbnailConfig);
- if (texture == null || !texture.isLoaded()) {
- if (currentScaleSlot != index) {
- if (j == 0) {
- final MediaSet parentSet = displayItem.mItemRef.mParentMediaSet;
- if (parentSet != null && parentSet.getNumItems() <= 1) {
- displayList.setAlive(displayItem, false);
- }
- } else {
- displayList.setAlive(displayItem, false);
- }
- }
- }
- final float dx1 = mScaleGestureDetector.getTopFingerDeltaX();
- final float dy1 = mScaleGestureDetector.getTopFingerDeltaY();
- final float dx2 = mScaleGestureDetector.getBottomFingerDeltaX();
- final float dy2 = mScaleGestureDetector.getBottomFingerDeltaY();
- final float span = mScaleGestureDetector.getCurrentSpan();
- if (state == GridLayer.STATE_FULL_SCREEN) {
- displayList.setOffset(displayItem, false, true, span, dx1, dy1, dx2, dy2);
- } else {
- if (!mHoldPosition) {
- if (state != GridLayer.STATE_GRID_VIEW) {
- if (currentScaleSlot == index) {
- displayList.setOffset(displayItem, true, false, span, dx1, dy1, dx2, dy2);
- } else if (currentScaleSlot != Shared.INVALID) {
- displayList.setOffset(displayItem, true, true, span, dx1, dy1, dx2, dy2);
- } else {
- displayList.setOffset(displayItem, false, false, span, dx1, dy1, dx2, dy2);
- }
- } else {
- float minVal = -1.0f;
- float maxVal = GridCamera.EYE_Z * 0.5f;
- float zVal = minVal + mSpreadValue;
- zVal = FloatUtils.clamp(zVal, minVal, maxVal);
- if (Float.isInfinite(zVal) || Float.isNaN(zVal)) {
- mCamera.moveZTo(0);
- } else {
- mCamera.moveZTo(-zVal);
- }
- }
- }
- }
- }
第一部分每太看懂,第二部分是得到多點觸碰的輸入資訊,第三部分根據檢視調整了照片偏移量和相機距離。
繪製縮圖顯示專案
- for (int j = startSlotIndex; j < GridLayer.MAX_ITEMS_PER_SLOT; ++j) {
- DisplayItem displayItem = displayItems[(index - firstBufferedVisibleSlot) * GridLayer.MAX_ITEMS_PER_SLOT + j];
- if (displayItem == null) {
- break;
- } else {
- if (currentFocusSlot == index) {
- displayList.setHasFocus(displayItem, true, pushDown);
- mTargetFocusMixRatio = 1.0f;
- } else {
- displayList.setHasFocus(displayItem, false, pushDown);
- }
- if (j >= maxDisplayedItemsPerSlot)
- continue;
- Texture texture = displayItem.getThumbnailImage(view.getContext(), sThumbnailConfig);
- if (texture != null) {
- if (index == mCurrentScaleSlot)
- displayItem.mAlive = true;
- if ((!displayItem.isAnimating() || !texture.isLoaded())
- && displayItem.getStackIndex() > GridLayer.MAX_ITEMS_PER_SLOT) {
- displayList.setAlive(displayItem, true);
- continue;
- }
- if (index < firstVisibleSlot || index > lastVisibleSlot) {
- if (view.bind(texture)) {
- displayList.setAlive(displayItem, true);
- }
- continue;
- }
- drawDisplayItem(view, gl, displayItem, texture, PASS_THUMBNAIL_CONTENT, placeholder,
- displayItem.mAnimatedPlaceholderFade);
- } else {
- // Move on to the next stack.
- break;
- }
- if (drawnCounter >= GridLayer.MAX_ITEMS_DRAWABLE - 1 || drawnCounter < 0) {
- break;
- }
- // Insert in order of z.
- itemsDrawn[drawnCounter++] = displayItem;
- itemsDrawn[drawnCounter] = null;
- }
- }
如果是當前選中,則根據當前檢視選擇下陷還是彈起;繪製縮圖(其中的displayList.setAlive還沒有看懂有什麼用);記錄已繪製的items。