Android5.1圖庫Gallery2程式碼分析資料載入流程

來源:互聯網
上載者:User

標籤:

圖片資料載入流程。

Gallery---->GalleryActivity------>AlbumSetPage------->AlbumPage--------->PhotoPage

                                              相簿集                        照片集                 某張圖片

1,AlbumSetPage.java

    private void initializeData(Bundle data) {

        String mediaPath = data.getString(AlbumSetPage.KEY_MEDIA_PATH);

        mMediaSet = mActivity.getDataManager().getMediaSet(mediaPath);

        mSelectionManager.setSourceMediaSet(mMediaSet);

        mAlbumSetDataAdapter = new AlbumSetDataLoader(

                mActivity, mMediaSet, DATA_CACHE_SIZE);

        mAlbumSetDataAdapter.setLoadingListener(new MyLoadingListener());

      }

增加回掉函數,當AlbumSetDataLoader.java發生變化時,更新介面

 

2,AlbumSetDataloader.java

    public void resume() {

        mSource.addContentListener(mSourceListener);

        mReloadTask = new ReloadTask();

        mReloadTask.start();

增加回呼函數,當資料庫發生變化時,載入資料。

 

3,LocalAlbumSet.java (extends MediaSet)

    public void addContentListener(ContentListener listener) {

        /// M: [BUG.MODIFY] avoid ConcurrentModificationException @{

        /*mListeners.put(listener, null);*/

        synchronized(WEAK_HASH_MAP_LOCK) {

            mListeners.put(listener, null);

        }

   增加回調直接和資料庫打交道。

 

4,DataManager.java

    public synchronized void initializeSourceMap() {

        if (!mSourceMap.isEmpty()) return;

 

        // the order matters, the UriSource must come last

        addSource(new LocalSource(mApplication));

建立LocalSource.java

 

5,LocalSource.java(extends MediaSource)

    @Override

    public MediaObject createMediaObject(Path path) {

        GalleryApp app = mApplication;

        switch (mMatcher.match(path)) {

            case LOCAL_ALL_ALBUMSET:

            case LOCAL_IMAGE_ALBUMSET:

            case LOCAL_VIDEO_ALBUMSET:

                return new LocalAlbumSet(path, mApplication);

建立LocalAlbumSet

 

6,LocalAlbumSet.java (extends MediaSet)

    public LocalAlbumSet(Path path, GalleryApp application) {

        super(path, nextVersionNumber());

        mApplication = application;

        mHandler = new Handler(application.getMainLooper());

        mType = getTypeFromPath(path);

        mNotifier = new ChangeNotifier(this, mWatchUris, application);

        mName = application.getResources().getString(

                R.string.set_label_local_albums);

    }

    private static final Uri[] mWatchUris =

        {Images.Media.EXTERNAL_CONTENT_URI, Video.Media.EXTERNAL_CONTENT_URI};

將監控要監控的圖片和視頻url添加到ChangeNotifier中。

 

7,ChangeNotifier.java

    public ChangeNotifier(MediaSet set, Uri[] uris, GalleryApp application) {

        mMediaSet = set;

        for (int i = 0; i < uris.length; i++) {

            application.getDataManager().registerChangeNotifier(uris[i], this);

   註冊url

 

8,DataManager.java

public void registerChangeNotifier(Uri uri, ChangeNotifier notifier) {

        NotifyBroker broker = null;

        synchronized (mNotifierMap) {

            broker = mNotifierMap.get(uri);

            if (broker == null) {

                broker = new NotifyBroker(mDefaultMainHandler);

                mApplication.getContentResolver()

                        .registerContentObserver(uri, true, broker);

                mNotifierMap.put(uri, broker);

            }

        }

        broker.registerNotifier(notifier);

}

將url和NotifyBroker進行關聯,當url發生變化時,就調用回調類broker

 

9,DataManager.java

private static class NotifyBroker extends ContentObserver {

        @Override

        public synchronized void onChange(boolean selfChange) {

            for(ChangeNotifier notifier : mNotifiers.keySet()) {

                notifier.onChange(selfChange);

            }

        }

當url發生變化時,就調用。notifier.onChange(selfChange)

-----àmMediaSet.notifyContentChanged();(ChangeNotifier.java)

--à listener.onContentDirty();(LocalAlbumSet.java (extends MediaSet))

Android5.1圖庫Gallery2程式碼分析資料載入流程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.