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。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值