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。