免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2500 | 回复: 1
打印 上一主题 下一主题

Android Gallery3d源码学习总结(一)——绘制流程drawFocusItems [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-29 22:02 |只看该作者 |倒序浏览

Android Gallery3d源码学习总结(一)——绘制流程drawFocusItems









显示单张图片相关的输入变量
  1. 1.int selectedSlotIndex = mSelectedSlot;

  2. 2.        GridDrawables drawables = mDrawables;

  3. 3.        GridCamera camera = mCamera;

  4. 4.        DisplayItem[] displayItems = mDisplayItems;

  5. 5.        int firstBufferedVisibleSlot = mBufferedVisibleRange.begin;

  6. 6.        int lastBufferedVisibleSlot = mBufferedVisibleRange.end;

  7. 7.        boolean isCameraZAnimating = mCamera.isZAnimating();
  8. 复制代码
复制代码
删除当前选中槽位前后2步之外的大缩略图

  1. 1.for (int i = firstBufferedVisibleSlot; i <= lastBufferedVisibleSlot; ++i) {

  2. 2.            if (selectedSlotIndex != Shared.INVALID && (i >= selectedSlotIndex - 2 && i <= selectedSlotIndex + 2)) {

  3. 3.                continue;

  4. 4.            }

  5. 5.            DisplayItem displayItem = displayItems[(i - firstBufferedVisibleSlot) * GridLayer.MAX_ITEMS_PER_SLOT];

  6. 6.            if (displayItem != null) {

  7. 7.                displayItem.clearScreennailImage();

  8. 8.            }

  9. 9.        }
  10. 复制代码
复制代码
得到当前图片的DispalyItem元素

  1. 1.int centerIndexInDrawnArray = (selectedSlotIndex - firstBufferedVisibleSlot) * GridLayer.MAX_ITEMS_PER_SLOT;

  2. 2.            if (centerIndexInDrawnArray < 0 || centerIndexInDrawnArray >= displayItems.length) {

  3. 3.                return;

  4. 4.            }

  5. 5.            DisplayItem centerDisplayItem = displayItems[centerIndexInDrawnArray];

  6. 6.            if (centerDisplayItem == null || centerDisplayItem.mItemRef.mId == Shared.INVALID) {

  7. 7.                return;

  8. 8.            }
  9. 复制代码
复制代码
判断大缩略图是否加载完成
  1. 1.boolean focusItemTextureLoaded = false;

  2. 2.            Texture centerTexture = centerDisplayItem.getScreennailImage(view.getContext());

  3. 3.            if (centerTexture != null && centerTexture.isLoaded()) {

  4. 4.                focusItemTextureLoaded = true;

  5. 5.            }
  6. 复制代码
复制代码
是否跳过当前图片前一张图片

  1. 1.float camX = camera.mLookAtX * camera.mScale;

  2. 2.float centerTranslateX = centerDisplayItem.mAnimatedPosition.x;

  3. 3.            final boolean skipPrevious = centerTranslateX < camX;
  4. 复制代码
复制代码
开启opengl混合模式并设置混合函数

  1. 1.gl.glEnable(GL11.GL_BLEND);

  2. 2.            gl.glBlendFunc(GL11.GL_ONE, GL11.GL_ONE);
  3. 复制代码
复制代码
循环遍历前中后三幅图片分别进行“核心绘制处理”
  1. 1.for (int i = -1; i <= 1; ++i) {

  2. 2.                if (slideshowMode && timeElapsedSinceView > 1.0f && i != 0)

  3. 3.                    continue;

  4. 4.。。。

  5. 5.}
  6. 复制代码
复制代码
核心绘制处理——输入变量准备
  1. 1.if (slideshowMode && timeElapsedSinceView > 1.0f && i != 0)

  2. 2.                    continue;

  3. 3.                float viewAspect = camera.mAspectRatio;

  4. 4.                int selectedSlotToUse = selectedSlotIndex + i;

  5. 5.                if (selectedSlotToUse >= 0 && selectedSlotToUse <= lastBufferedVisibleSlot) {

  6. 6.                    int indexInDrawnArray = (selectedSlotToUse - firstBufferedVisibleSlot) * GridLayer.MAX_ITEMS_PER_SLOT;

  7. 7.                    if (indexInDrawnArray < 0 || indexInDrawnArray >= displayItems.length) {

  8. 8.                        return;

  9. 9.                    }

  10. 10.                    DisplayItem displayItem = displayItems[indexInDrawnArray];

  11. 11.                    MediaItem item = displayItem.mItemRef;

  12. 12.                    final Texture thumbnailTexture = displayItem.getThumbnailImage(view.getContext(), sThumbnailConfig);

  13. 13.                    Texture texture = displayItem.getScreennailImage(view.getContext());
  14. 复制代码
复制代码
在幻灯模式下且超过1秒的切换时间无须显示前后两张图片;
得到视角和当前displayItem、对应媒体对象、小缩略图材质、大缩略图材质。
加载高质量的材质资源
  1. 1.if (isCameraZAnimating && (texture == null || !texture.isLoaded())) {

  2. 2.                        texture = thumbnailTexture;

  3. 3.                        mSelectedMixRatio.setValue(0f);

  4. 4.                        mSelectedMixRatio.animateValue(1f, 0.75f, view.getFrameTime());

  5. 5.                    }

  6. 6.                    Texture hiRes = (zoomValue != 1.0f && i == 0 && item.getMediaType() != MediaItem.MEDIA_TYPE_VIDEO) ? displayItem

  7. 7.                            .getHiResImage(view.getContext())

  8. 8.                            : null;

  9. 9.                    if (Gallery.PIXEL_DENSITY > 1.0f) {

  10. 10.                        hiRes = texture;

  11. 11.                    }

  12. 12.                    if (i != 0) {

  13. 13.                        displayItem.clearHiResImage();

  14. 14.                    }

  15. 15.                    if (hiRes != null) {

  16. 16.                        if (!hiRes.isLoaded()) {

  17. 17.                            view.bind(hiRes);

  18. 18.                            view.prime(hiRes, true);

  19. 19.                        } else {

  20. 20.                            texture = hiRes;

  21. 21.                        }

  22. 22.                    }
  23. 复制代码
复制代码
如果Camera正在拉远或拉近,且大缩略图材质为空或未加载完成,则选择小缩略图作为材质,将当前图片的“大缩略图混合比例”变量进行初始化(目标值为1秒,渐变时间为0.75秒,渐变开始时间为当前帧时间)。
如果处于放大状态,则加载原图hiRes,加载成功后赋值给材质变量texture;并清除前后图片的原图。
加载材质

  1. 1.final Texture fsTexture = texture;

  2. 2.                    if (texture == null || !texture.isLoaded()) {

  3. 3.                        if (Math.abs(centerTranslateX - camX) < 0.1f) {

  4. 4.                            if (focusItemTextureLoaded && i != 0) {

  5. 5.                                view.bind(texture);

  6. 6.                            }

  7. 7.                            if (i == 0) {

  8. 8.                                view.bind(texture);

  9. 9.                                view.prime(texture, true);

  10. 10.                            }

  11. 11.                        }

  12. 12.                        texture = thumbnailTexture;

  13. 13.                        if (i == 0) {

  14. 14.                            mSelectedMixRatio.setValue(0f);

  15. 15.                            mSelectedMixRatio.animateValue(1f, 0.75f, view.getFrameTime());

  16. 16.                        }

  17. 17.                    }
  18. 复制代码
复制代码
保留当前的最佳材质,
如果此材质加载未完成,则继续按优先级加载,并把小缩略图(基本上都已经加载成功了)设置为当前材质;
最佳材质未加载完成之前,替换渐变不会开始。
无须绘制
  1. 1.if (mCamera.isAnimating() || slideshowMode) {

  2. 2.                        if (!slideshowMode && skipPrevious && i == -1) {

  3. 3.                            continue;

  4. 4.                        }

  5. 5.                        if (!skipPrevious && i == 1) {

  6. 6.                            continue;

  7. 7.                        }

  8. 8.                    }

  9. 9.                    int theta = (int) displayItem.getImageTheta();
  10. 复制代码
复制代码
如果相机缩放过程中,非幻灯片模式下且镜头中不需要展示前一张的情况下,无须处理前一张;
如果相机缩放或幻灯片绘制过程中,需要展示前一张的情况下,无须处理后一张。
处理前后渐变绘制

  1. 1.// If it is in slideshow mode, we draw the previous item in

  2. 2.                    // the next item's position.

  3. 3.                    if (slideshowMode && timeElapsedSinceView < 1.0f && timeElapsedSinceView != 0) {

  4. 4.                        if (i == -1) {

  5. 5.                            int nextSlotToUse = selectedSlotToUse + 1;

  6. 6.                            if (nextSlotToUse >= 0 && nextSlotToUse <= lastBufferedVisibleSlot) {

  7. 7.                                int nextIndexInDrawnArray = (nextSlotToUse - firstBufferedVisibleSlot)

  8. 8.                                        * GridLayer.MAX_ITEMS_PER_SLOT;

  9. 9.                                if (nextIndexInDrawnArray >= 0 && nextIndexInDrawnArray < displayItems.length) {

  10. 10.                                    float currentImageTheta = displayItem.mAnimatedImageTheta;

  11. 11.                                    displayItem = displayItems[nextIndexInDrawnArray];

  12. 12.                                    backupImageTheta = displayItem.mAnimatedImageTheta;

  13. 13.                                    displayItem.mAnimatedImageTheta = currentImageTheta;

  14. 14.                                    view.setAlpha(1.0f - timeElapsedSinceView);

  15. 15.                                }

  16. 16.                            }

  17. 17.                        } else if (i == 0) {

  18. 18.                            displayItem.mAnimatedImageTheta = backupImageTheta;

  19. 19.                            view.setAlpha(timeElapsedSinceView);

  20. 20.                        }

  21. 21.                    }
  22. 复制代码
复制代码
如果处于幻灯片模式中 渐变过程中,处理上一幅相片时找到它的下一个DispalyItem,处理本张相片则直接使用当前DispalyItem,为的是同样找到当前DisplayItem,并绑定上一张相片和本张相片,两张相片透明度互补达到渐变的效果。
绘制小大缩略图渐变过程-小缩略图
  1. 1.int vboIndex = i + 1;

  2. 2.                        float alpha = view.getAlpha();

  3. 3.                        float selectedMixRatio = mSelectedMixRatio.getValue(view.getFrameTime());

  4. 4.                        if (selectedMixRatio != 1f) {

  5. 5.                            texture = thumbnailTexture;

  6. 6.                            view.setAlpha(alpha * (1.0f - selectedMixRatio));

  7. 7.                        }

  8. 8.                        GridQuad quad = GridDrawables.sFullscreenGrid[vboIndex];

  9. 9.                        float u = texture.getNormalizedWidth();

  10. 10.                        float v = texture.getNormalizedHeight();

  11. 11.                        float imageWidth = texture.getWidth();

  12. 12.                        float imageHeight = texture.getHeight();

  13. 13.                        boolean portrait = ((theta / 90) % 2 == 1);

  14. 14.                        if (portrait) {

  15. 15.                            viewAspect = 1.0f / viewAspect;

  16. 16.                        }

  17. 17.                        quad.resizeQuad(viewAspect, u, v, imageWidth, imageHeight);

  18. 18.                        quad.bindArrays(gl);

  19. 19.                        

  20. 20.                        drawDisplayItem(view, gl, displayItem, texture, PASS_FOCUS_CONTENT, null, 0.0f);

  21. 21.                        quad.unbindArrays(gl);
  22. 复制代码
复制代码
绘制小缩略图,请注意:selectedMixRatio表示大缩略图的绘制透明度,小缩略图的自然就是1.0f - selectedMixRatio。
绘制小大缩略图渐变过程-大缩略图
  1. 1.if (selectedMixRatio != 0.0f && selectedMixRatio != 1.0f) {

  2. 2.                            texture = fsTexture;

  3. 3.                            if (texture != null) {

  4. 4.                                float drawAlpha = selectedMixRatio;

  5. 5.                                view.setAlpha(alpha * drawAlpha);

  6. 6.                                u = texture.getNormalizedWidth();

  7. 7.                                v = texture.getNormalizedHeight();

  8. 8.                                imageWidth = texture.getWidth();

  9. 9.                                imageHeight = texture.getHeight();

  10. 10.                                quad.resizeQuad(viewAspect, u, v, imageWidth, imageHeight);

  11. 11.                                quad.bindArrays(gl);

  12. 12.                                drawDisplayItem(view, gl, displayItem, fsTexture, PASS_FOCUS_CONTENT, null, 1.0f);

  13. 13.                                quad.unbindArrays(gl);

  14. 14.                            }

  15. 15.                        }
  16. 复制代码
复制代码
更新当前图片长宽数据
  1. 1.if (i == 0 || slideshowMode) {

  2. 2.                            mCurrentFocusItemWidth = quad.getWidth();

  3. 3.                            mCurrentFocusItemHeight = quad.getHeight();

  4. 4.                            if (portrait) {

  5. 5.                                // Swap these values.

  6. 6.                                float itemWidth = mCurrentFocusItemWidth;

  7. 7.                                mCurrentFocusItemWidth = mCurrentFocusItemHeight;

  8. 8.                                mCurrentFocusItemHeight = itemWidth;

  9. 9.                            }

  10. 10.                        }
复制代码
复制代码
绘制视频元素
  1. 1.view.setAlpha(alpha);

  2. 2.                        if (item.getMediaType() == MediaItem.MEDIA_TYPE_VIDEO) {

  3. 3.                            // The play graphic overlay.

  4. 4.                            GridDrawables.sVideoGrid.bindArrays(gl);

  5. 5.                            drawDisplayItem(view, gl, displayItem, drawables.mTextureVideo, PASS_VIDEO_LABEL, null, 0);

  6. 6.                            GridDrawables.sVideoGrid.unbindArrays(gl);

  7. 7.                        }
复制代码

论坛徽章:
0
2 [报告]
发表于 2011-12-29 22:02 |只看该作者
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP