[转]Android平台Gallery2应用分析(四)---AlbumSetPage
AlbumSetPage的几个重要成员,如图:
AlbumSetPage刷新AlbumSet,每个看到的相集都是SlotView。而SlotView主要由TiledTexture和label组成。如图:
简单的说,SlotView的绘制过程为:SlotView::render(…) -> renderItem() -> AlbumSetSlotRender::renderSlot() -> renderContent(…) 、renderLabel(…)、renderOverlay(…)。流程图如下:
下面详细说明下SlotView的刷新流程,先看代码流程图,后面附加流程说明:
1) AlbumSetPage的onResume()中,mAlbumSetView.resume()。它会调用mDataWindow.resume()。mDataWindow是AlbumSetSlidingWindow类型。代码如下:
- public void resume() {
- mIsActive = true;
- TiledTexture.prepareResources();
- for (int i = mContentStart, n = mContentEnd; i < n; ++i) {
- prepareSlotContent(i);
- }
- updateAllImageRequests();
在for循环中调用prepareSlotContent(i)逐个更新相集显示内容并往mData中记录AlbumSetEntry数据。代码如下:
- private void prepareSlotContent(int slotIndex) {
- AlbumSetEntry entry = new AlbumSetEntry();
- updateAlbumSetEntry(entry, slotIndex);
- mData[slotIndex % mData.length] = entry;
- }
2) 在updateAlbumSetEntry中会获取到相集的title、album、cover、labelLoader以及coverLoader。
- private void updateAlbumSetEntry(AlbumSetEntry entry, int slotIndex) {
- MediaSet album = mSource.getMediaSet(slotIndex);
- MediaItem cover = mSource.getCoverItem(slotIndex);
- int totalCount = mSource.getTotalCount(slotIndex);
- entry.album = album;
- ……
- entry.setPath = (album == null) ? null : album.getPath();
- String title = (album == null) ? "" : Utils.ensureNotNull(album.getName());
- int sourceType = DataSourceType.identifySourceType(album);
- if (isLabelChanged(entry, title, totalCount, sourceType)) {
- entry.title = title;
- entry.totalCount = totalCount;
- ……
- if (album != null) {
- entry.labelLoader = new AlbumLabelLoader(
- slotIndex, title, totalCount, sourceType);
- }
- }
- entry.coverItem = cover;
- if (getDataVersion(cover) != entry.coverDataVersion) {
- ……
- if (cover != null) {
- entry.coverLoader = new AlbumCoverLoader(slotIndex, cover);
- }
- }
3) 回头再看第一步中调用的updateAllImageRequests(),代码如下:
- private void updateAllImageRequests() {
- mActiveRequestCount = 0;
- for (int i = mActiveStart, n = mActiveEnd; i < n; ++i) {
- AlbumSetEntry entry = mData[i % mData.length];
- if (startLoadBitmap(entry.coverLoader)) ++mActiveRequestCount;
- if (startLoadBitmap(entry.labelLoader)) ++mActiveRequestCount;
- }
- ……
- }
在for循环中将逐个加载cover、label。startLoadBitmap(entry.coverLoader和startLoadBitmap(entry.labbelLoader从参数类型BitmapLoader看,调用startLoader()会执行各个loader的submitBitmapTask()。详细代码如下:
- AlbumSetSlidingWindow.java
- private static boolean startLoadBitmap(BitmapLoader loader) {
- if (loader == null) return false;
- loader.startLoad();
- return loader.isRequestInProgress();
- }
- BitmapLoader.java
- public synchronized void startLoad() {
- if (mState == STATE_INIT) {
- mState = STATE_REQUESTED;
- if (mTask == null) mTask = submitBitmapTask(this);
- }
- }
这个在前面介绍ThreadPool的时候提到过,submit之后会在线程池中执行任务加载图片,从线程池的代码run()中知道完成图片回去后,会调用Listener.onFeatureDone,从future.get中得到Bitmap传给mLoadComplete(mBitmap)。 发送消息MSG_UPDATE_ALBUM_ENTRY调用Loader的updateEntry()。
mRootPane的onLayout调用过程详解:
这个调用过程和Activity的启动流程有关。mRootPane作为一个GLView, 在onResume()时调用setContentPane(mRootPane)设置。在Activity的启动流程中,ActivityThread会调用handleResumeActivity中,先执行performResumeActivity, 后执行wm.addView()。最终会调用到ViewRootImpl的requestLayout() –>View.dispatchAttachedToWindow -> onAttachedToWindow -> GLSurfaceView.onAttachedToWindow -> GLThread.start() -> GLThread.guardedRun() -> onSurfaceCreated()、onSurfaceChanged()、 onDrawFrame() -> onDrawFrameLocked() -> layoutContentPane() -> mContentView.layout()。
其中mContentView即在onResume中SetContentPane()传入的参数mRootPane。
欢迎转载和技术交流,转载请帮忙注明出处,http://blog.csdn.net/discovery_by_joseph,谢谢!

浙公网安备 33010602011771号