Android Gallery3d源码学习总结(一)——绘制流程drawFocusItems
显示单张图片相关的输入变量
- int selectedSlotIndex = mSelectedSlot;
- GridDrawables drawables = mDrawables;
- GridCamera camera = mCamera;
- DisplayItem[] displayItems = mDisplayItems;
- int firstBufferedVisibleSlot = mBufferedVisibleRange.begin;
- int lastBufferedVisibleSlot = mBufferedVisibleRange.end;
- boolean isCameraZAnimating = mCamera.isZAnimating();
删除当前选中槽位前后2步之外的大缩略图
- for (int i = firstBufferedVisibleSlot; i <= lastBufferedVisibleSlot; ++i) {
- if (selectedSlotIndex != Shared.INVALID && (i
>= selectedSlotIndex - 2 && i <= selectedSlotIndex + 2)) {
- continue;
- }
- DisplayItem displayItem = displayItems[(i - firstBufferedVisibleSlot) * GridLayer.MAX_ITEMS_PER_SLOT];
- if (displayItem != null) {
- displayItem.clearScreennailImage();
- }
- }
得到当前图片的DispalyItem元素
- int centerIndexInDrawnArray = (selectedSlotIndex - firstBufferedVisibleSlot) * GridLayer.MAX_ITEMS_PER_SLOT;
- if (centerIndexInDrawnArray < 0 || centerIndexInDrawnArray >= displayItems.length) {
- return;
- }
- DisplayItem centerDisplayItem = displayItems[centerIndexInDrawnArray];
- if (centerDisplayItem == null || centerDisplayItem.mItemRef.mId == Shared.INVALID) {
- return;
- }
判断大缩略图是否加载完成
- boolean focusItemTextureLoaded = false;
- Texture centerTexture = centerDisplayItem.getScreennailImage(view.getContext());
- if (centerTexture != null && centerTexture.isLoaded()) {
- focusItemTextureLoaded = true;
- }
是否跳过当前图片前一张图片
- float camX = camera.mLookAtX * camera.mScale;
- float centerTranslateX = centerDisplayItem.mAnimatedPosition.x;
- final boolean skipPrevious = centerTranslateX < camX;
开启opengl混合模式并设置混合函数
- gl.glEnable(GL11.GL_BLEND);
- gl.glBlendFunc(GL11.GL_ONE, GL11.GL_ONE);
循环遍历前中后三幅图片分别进行“核心绘制处理”
- for (int i = -1; i <= 1; ++i) {
- if (slideshowMode && timeElapsedSinceView > 1.0f && i != 0)
- continue;
- 。。。
- }
核心绘制处理——输入变量准备
- if (slideshowMode && timeElapsedSinceView > 1.0f && i != 0)
- continue;
- float viewAspect = camera.mAspectRatio;
- int selectedSlotToUse = selectedSlotIndex + i;
- if (selectedSlotToUse >= 0 && selectedSlotToUse <= lastBufferedVisibleSlot) {
- int indexInDrawnArray = (selectedSlotToUse - firstBufferedVisibleSlot) * GridLayer.MAX_ITEMS_PER_SLOT;
- if (indexInDrawnArray < 0 || indexInDrawnArray >= displayItems.length) {
- return;
- }
- DisplayItem displayItem = displayItems[indexInDrawnArray];
- MediaItem item = displayItem.mItemRef;
- final Texture thumbnailTexture = displayItem.getThumbnailImage(view.getContext(), sThumbnailConfig);
- Texture texture = displayItem.getScreennailImage(view.getContext());
在幻灯模式下且超过1秒的切换时间无须显示前后两张图片;
得到视角和当前displayItem、对应媒体对象、小缩略图材质、大缩略图材质。
加载高质量的材质资源
- if (isCameraZAnimating && (texture == null || !texture.isLoaded())) {
- texture = thumbnailTexture;
- mSelectedMixRatio.setValue(0f);
- mSelectedMixRatio.animateValue(1f, 0.75f, view.getFrameTime());
- }
- Texture hiRes = (zoomValue != 1.0f && i
== 0 && item.getMediaType() != MediaItem.MEDIA_TYPE_VIDEO) ?
displayItem
- .getHiResImage(view.getContext())
- : null;
- if (Gallery.PIXEL_DENSITY > 1.0f) {
- hiRes = texture;
- }
- if (i != 0) {
- displayItem.clearHiResImage();
- }
- if (hiRes != null) {
- if (!hiRes.isLoaded()) {
- view.bind(hiRes);
- view.prime(hiRes, true);
- } else {
- texture = hiRes;
- }
- }
如果Camera正在拉远或拉近,且大缩略图材质为空或未加载完成,则选择小缩略图作为材质,将当前图片的“大缩略图混合比例”变量进行初始化(目标值为1秒,渐变时间为0.75秒,渐变开始时间为当前帧时间)。
如果处于放大状态,则加载原图hiRes,加载成功后赋值给材质变量texture;并清除前后图片的原图。
加载材质
- final Texture fsTexture = texture;
- if (texture == null || !texture.isLoaded()) {
- if (Math.abs(centerTranslateX - camX) < 0.1f) {
- if (focusItemTextureLoaded && i != 0) {
- view.bind(texture);
- }
- if (i == 0) {
- view.bind(texture);
- view.prime(texture, true);
- }
- }
- texture = thumbnailTexture;
- if (i == 0) {
- mSelectedMixRatio.setValue(0f);
- mSelectedMixRatio.animateValue(1f, 0.75f, view.getFrameTime());
- }
- }
保留当前的最佳材质,
如果此材质加载未完成,则继续按优先级加载,并把小缩略图(基本上都已经加载成功了)设置为当前材质;
最佳材质未加载完成之前,替换渐变不会开始。
无须绘制
- if (mCamera.isAnimating() || slideshowMode) {
- if (!slideshowMode && skipPrevious && i == -1) {
- continue;
- }
- if (!skipPrevious && i == 1) {
- continue;
- }
- }
- int theta = (int) displayItem.getImageTheta();
如果相机缩放过程中,非幻灯片模式下且镜头中不需要展示前一张的情况下,无须处理前一张;
如果相机缩放或幻灯片绘制过程中,需要展示前一张的情况下,无须处理后一张。
处理前后渐变绘制
- // If it is in slideshow mode, we draw the previous item in
- // the next item's position.
- if (slideshowMode && timeElapsedSinceView < 1.0f && timeElapsedSinceView != 0) {
- if (i == -1) {
- int nextSlotToUse = selectedSlotToUse + 1;
- if (nextSlotToUse >= 0 && nextSlotToUse <= lastBufferedVisibleSlot) {
- int nextIndexInDrawnArray = (nextSlotToUse - firstBufferedVisibleSlot)
- * GridLayer.MAX_ITEMS_PER_SLOT;
- if (nextIndexInDrawnArray >= 0 && nextIndexInDrawnArray < displayItems.length) {
- float currentImageTheta = displayItem.mAnimatedImageTheta;
- displayItem = displayItems[nextIndexInDrawnArray];
- backupImageTheta = displayItem.mAnimatedImageTheta;
- displayItem.mAnimatedImageTheta = currentImageTheta;
- view.setAlpha(1.0f - timeElapsedSinceView);
- }
- }
- } else if (i == 0) {
- displayItem.mAnimatedImageTheta = backupImageTheta;
- view.setAlpha(timeElapsedSinceView);
- }
- }
如果处于幻灯片模式中 渐变过程中,处理上一幅相片时找到它的下一个DispalyItem,处理本张相片则直接使用当前DispalyItem,为的是同样找到当前DisplayItem,并绑定上一张相片和本张相片,两张相片透明度互补达到渐变的效果。
绘制小大缩略图渐变过程-小缩略图
- int vboIndex = i + 1;
- float alpha = view.getAlpha();
- float selectedMixRatio = mSelectedMixRatio.getValue(view.getFrameTime());
- if (selectedMixRatio != 1f) {
- texture = thumbnailTexture;
- view.setAlpha(alpha * (1.0f - selectedMixRatio));
- }
- GridQuad quad = GridDrawables.sFullscreenGrid[vboIndex];
- float u = texture.getNormalizedWidth();
- float v = texture.getNormalizedHeight();
- float imageWidth = texture.getWidth();
- float imageHeight = texture.getHeight();
- boolean portrait = ((theta / 90) % 2 == 1);
- if (portrait) {
- viewAspect = 1.0f / viewAspect;
- }
- quad.resizeQuad(viewAspect, u, v, imageWidth, imageHeight);
- quad.bindArrays(gl);
-
- drawDisplayItem(view, gl, displayItem, texture, PASS_FOCUS_CONTENT, null, 0.0f);
- quad.unbindArrays(gl);
绘制小缩略图,请注意:selectedMixRatio表示大缩略图的绘制透明度,小缩略图的自然就是1.0f - selectedMixRatio。
绘制小大缩略图渐变过程-大缩略图
- if (selectedMixRatio != 0.0f && selectedMixRatio != 1.0f) {
- texture = fsTexture;
- if (texture != null) {
- float drawAlpha = selectedMixRatio;
- view.setAlpha(alpha * drawAlpha);
- u = texture.getNormalizedWidth();
- v = texture.getNormalizedHeight();
- imageWidth = texture.getWidth();
- imageHeight = texture.getHeight();
- quad.resizeQuad(viewAspect, u, v, imageWidth, imageHeight);
- quad.bindArrays(gl);
- drawDisplayItem(view, gl, displayItem, fsTexture, PASS_FOCUS_CONTENT, null, 1.0f);
- quad.unbindArrays(gl);
- }
- }
更新当前图片长宽数据
- if (i == 0 || slideshowMode) {
- mCurrentFocusItemWidth = quad.getWidth();
- mCurrentFocusItemHeight = quad.getHeight();
- if (portrait) {
- // Swap these values.
- float itemWidth = mCurrentFocusItemWidth;
- mCurrentFocusItemWidth = mCurrentFocusItemHeight;
- mCurrentFocusItemHeight = itemWidth;
- }
- }
绘制视频元素
- view.setAlpha(alpha);
- if (item.getMediaType() == MediaItem.MEDIA_TYPE_VIDEO) {
- // The play graphic overlay.
- GridDrawables.sVideoGrid.bindArrays(gl);
- drawDisplayItem(view, gl, displayItem, drawables.mTextureVideo, PASS_VIDEO_LABEL, null, 0);
- GridDrawables.sVideoGrid.unbindArrays(gl);
- }