Android開源代碼解讀の地圖照片應用Panoramio的實現詳解(四)

本文繼續講解Panoramio的實現,主要介紹ImageAdapter.java和ImageList.java 這兩個文件,這兩個文件實現瞭如下所示的界面,左圖是數據從網絡加載過程中,有圖是加載完成後的效果:

 

 

ImageAdapter繼承自BaseAdapter類,實現圖片適配器的功能,而ImageList則繼承自ListActivity,用於以列表形式顯示搜索到的圖片信息。介紹之前,先來普及下DataSetObserver和DataSetObservable的知識。

從名字可以依稀猜到,DataSetObserver實現瞭觀察者模式中的觀察者角色(Observer)。當數據集發生變化或者變為無效時,DataSetObserver中的方法被回調,典型的數據集有Cursor和Adapter,當某個對象要添加到DataSetObservable中時,這個對象必須從DataSetObserver繼承,DataSetObserver是一個抽象類,定義如下:

[java]
public abstract class DataSetObserver {  

     /**
      * This method is called when the entire data set has changed,
      * most likely through a call to {@link Cursor#requery()} on a {@link Cursor}.
      */
     public void onChanged() {
         // Do nothing 
     }
 
     /**
      * This method is called when the entire data becomes invalid,
      * most likely through a call to {@link Cursor#deactivate()} or {@link Cursor#close()} on a
      * {@link Cursor}.
      */
     public void onInvalidated() {
         // Do nothing 
     }
 }
 public abstract class DataSetObserver {
     /**
      * This method is called when the entire data set has changed,
      * most likely through a call to {@link Cursor#requery()} on a {@link Cursor}.
      */
     public void onChanged() {
         // Do nothing
     }
 
    /**
      * This method is called when the entire data becomes invalid,
      * most likely through a call to {@link Cursor#deactivate()} or {@link Cursor#close()} on a
      * {@link Cursor}.
      */
     public void onInvalidated() {
         // Do nothing
     }
 }
 而DataSetObservable實現瞭觀察者模式中的對象角色(Subject),它是Observable的具體實現,提供瞭調用DataSetObserver中各種回調函數的方法,定義如下所示:
 
[java]
 public class DataSetObservable extends Observable<DataSetObserver> {
     /**
      * Invokes onChanged on each observer. Called when the data set being observed has
      * changed, and which when read contains the new state of the data.
      */
     public void notifyChanged() {
         synchronized(mObservers) {
             for (DataSetObserver observer : mObservers) {
                 observer.onChanged();
             }
         }
     }
 
     /**
      * Invokes onInvalidated on each observer. Called when the data set being monitored
      * has changed such that it is no longer valid.
      */
     public void notifyInvalidated() {
         synchronized (mObservers) {
             for (DataSetObserver observer : mObservers) {
                 observer.onInvalidated();
             }
         }
     }
 }
 public class DataSetObservable extends Observable<DataSetObserver> {
     /**
      * Invokes onChanged on each observer. Called when the data set being observed has
      * changed, and which when read contains the new state of the data.
      */
     public void notifyChanged() {
         synchronized(mObservers) {
             for (DataSetObserver observer : mObservers) {
                 observer.onChanged();
             }
         }
     }
 
    /**
      * Invokes onInvalidated on each observer. Called when the data set being monitored
      * has changed such that it is no longer valid.
      */
     public void notifyInvalidated() {
         synchronized (mObservers) {
             for (DataSetObserver observer : mObservers) {
                 observer.onInvalidated();
             }
         }
     }
 }
 註意這裡的Observable是定義在android.database包中的,而不是JDK中的Observable,它的定義如下:
 
[java]
 package android.database;
 
 import java.util.ArrayList;
 
 /**
  * Provides methods for (un)registering arbitrary observers in an ArrayList.
  */
 public abstract class Observable<T> {
     /**
      * The list of observers.  An observer can be in the list at most
      * once and will never be null.
      */
     protected final ArrayList<T> mObservers = new ArrayList<T>();
 
     /**
      * Adds an observer to the list. The observer cannot be null and it must not already
      * be registered.
      * @param observer the observer to register
      * @throws IllegalArgumentException the observer is null
      * @throws IllegalStateException the observer is already registered
      */
     public void registerObserver(T observer) {
         if (observer == null) {
             throw new IllegalArgumentException("The observer is null.");
         }
         synchronized(mObservers) {
             if (mObservers.contains(observer)) {
                 throw new IllegalStateException("Observer " + observer + " is already registered.");
             }
             mObservers.add(observer);
         }
     }
 
     /**
      * Removes a previously registered observer. The observer must not be null and it
      * must already have been registered.
      * @param observer the observer to unregister
      * @throws IllegalArgumentException the observer is null
      * @throws IllegalStateException the observer is not yet registered
      */
     public void unregisterObserver(T observer) {
         if (observer == null) {
             throw new IllegalArgumentException("The observer is null.");
         }
         synchronized(mObservers) {
             int index = mObservers.indexOf(observer);
             if (index == -1) {
                 throw new IllegalStateException("Observer " + observer + " was not registered.");
             }
             mObservers.remove(index);
         }
     }
     
     /**
      * Remove all registered observer
      */
     public void unregisterAll() {
         synchronized(mObservers) {
             mObservers.clear();
         }        
     }
 }
 package android.database;
 
import java.util.ArrayList;
 
/**
  * Provides methods for (un)registering arbitrary observers in an ArrayList.
  */
 public abstract class Observable<T> {
     /**
      * The list of observers.  An observer can be in the list at most
      * once and will never be null.
      */
     protected final ArrayList<T> mObservers = new ArrayList<T>();
 
    /**
      * Adds an observer to the list. The observer cannot be null and it must not already
      * be registered.
      * @param observer the observer to register
      * @throws IllegalArgumentException the observer is null
      * @throws IllegalStateException the observer is already registered
      */
     public void registerObserver(T observer) {
         if (observer == null) {
             throw new IllegalArgumentException("The observer is null.");
         }
         synchronized(mObservers) {
             if (mObservers.contains(observer)) {
                 throw new IllegalStateException("Observer " + observer + " is already registered.");
             }
             mObservers.add(observer);
         }
     }
 
    /**
      * Removes a previously registered observer. The observer must not be null and it
      * must already have been registered.
      * @param observer the observer to unregister
      * @throws IllegalArgumentException the observer is null
      * @throws IllegalStateException the observer is not yet registered
      */
     public void unregisterObserver(T observer) {
         if (observer == null) {
             throw new IllegalArgumentException("The observer is null.");
         }
         synchronized(mObservers) {
             int index = mObservers.indexOf(observer);
             if (index == -1) {
                 throw new IllegalStateException("Observer " + observer + " was not registered.");
             }
             mObservers.remove(index);
         }
     }
   
     /**
      * Remove all registered observer
      */
     public void unregisterAll() {
         synchronized(mObservers) {
             mObservers.clear();
         }      
     }
 }
 對觀察者模式的詳細描述,可見這篇文章/index.php?m=content&c=content&a=public_preview&steps=1&catid=339&id=120756 。OK,言歸正傳,還是來看下我們的ImageAdapter.java,從上面的分析和代碼裡面的註釋應該很好理解瞭:
 
[java]
 package com.google.android.panoramio;
 
 import android.content.Context;
 import android.database.DataSetObserver;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.BaseAdapter;
 import android.widget.ImageView;
 import android.widget.TextView;
 
 /**
  * 用來給ImageList綁定圖片資源的適配器
  */
 public class ImageAdapter extends BaseAdapter {
 
     /**
      * Maintains the state of our data
      */
     private ImageManager mImageManager;
 
     private Context mContext;
     
     private MyDataSetObserver mObserver;
 
     /**
      * ImageManager扮演的是Subject的角色,這個類的實例將被添加到ImagManager類
      * 中的觀察者列表中,當ImageList中的數據發生變化時(由ImageManager來檢測)
      * ImageManager將通告MyDataSetObserver實例發生的變化
      */
     private class MyDataSetObserver extends DataSetObserver {
         @Override
         public void onChanged() {
             //BaseAdapter維護瞭一個DataSetObservable對象mDataSetObservable 
             //這個函數用於通告mDataSetObservable的所有觀察者數據發生變化 
             notifyDataSetChanged(); 
         }
 
         @Override
         public void onInvalidated() {
             //BaseAdapter維護瞭一個DataSetObservable對象mDataSetObservable 
             //這個函數用於通告mDataSetObservable的所有觀察者數據變為無效 
             notifyDataSetInvalidated();
         }
     }
     
     public ImageAdapter(Context c) {
         mImageManager = ImageManager.getInstance(c);
         mContext = c;
         mObserver = new MyDataSetObserver();
         
         mImageManager.addObserver(mObserver); //將mObserver設置為mImageManager的觀察者 
     }
 
     /**
      * 返回顯示的圖片的數目
      *
      * @see android.widget.Adapter#getCount()
      */
     public int getCount() {
         return mImageManager.size();
     }
 
     /**
      * 返回指定索引的圖片
      *
      * @see android.widget.Adapter#getItem(int)
      */
     public Object getItem(int position) {
         return mImageManager.get(position);
     }
 
     /**
      * 返回指定索引的圖片ID
      *
      * @see android.widget.Adapter#getItemId(int)
      */
     public long getItemId(int position) {
         PanoramioItem s = mImageManager.get(position);
         return s.getId();
     }
 
     /**
      * 返回指定索引處用於顯示圖片的view
      *
      * @param position 索引
      * @param convertView 可以重用的view,可能為null.
      * @param parent 返回的view的父view.
      * @return 用於顯示指定索引處圖片的view
      */
     public View getView(int position, View convertView, ViewGroup parent) {
         View view;
         if (convertView == null) {
             // 創建新的view 
             LayoutInflater inflater = (LayoutInflater) mContext
                     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             view = inflater.inflate(R.layout.image_item, null);
         } else {
             // 使用已經存在的view 
             view = convertView;
         }
         PanoramioItem s = mImageManager.get(position);
 
         ImageView i = (ImageView) view.findViewById(R.id.image);
         i.setImageBitmap(s.getBitmap()); //將位圖設置到view上 
         i.setBackgroundResource(R.drawable.picture_frame); //設置ImageView的背景圖片 
         
         TextView t = (TextView) view.findViewById(R.id.title);
         t.setText(s.getTitle()); //設置圖片名稱 
         
         t = (TextView) view.findViewById(R.id.owner);
         t.setText(s.getOwner()); //設置圖片作者 
         return view;
     }
 
 }
 package com.google.android.panoramio;
 
import android.content.Context;
 import android.database.DataSetObserver;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.BaseAdapter;
 import android.widget.ImageView;
 import android.widget.TextView;
 
/**
  * 用來給ImageList綁定圖片資源的適配器
  */
 public class ImageAdapter extends BaseAdapter {
 
    /**
      * Maintains the state of our data
      */
     private ImageManager mImageManager;
 
    private Context mContext;
   
     private MyDataSetObserver mObserver;
 
    /**
      * ImageManager扮演的是Subject的角色,這個類的實例將被添加到ImagManager類
      * 中的觀察者列表中,當ImageList中的數據發生變化時(由ImageManager來檢測)
      * ImageManager將通告MyDataSetObserver實例發生的變化
      */
     private class MyDataSetObserver extends DataSetObserver {
         @Override
         public void onChanged() {
          //BaseAdapter維護瞭一個DataSetObservable對象mDataSetObservable
          //這個函數用於通告mDataSetObservable的所有觀察者數據發生變化
             notifyDataSetChanged();
         }
 
        @Override
         public void onInvalidated() {
          //BaseAdapter維護瞭一個DataSetObservable對象mDataSetObservable
          //這個函數用於通告mDataSetObservable的所有觀察者數據變為無效
             notifyDataSetInvalidated();
         }
     }
   
     public ImageAdapter(Context c) {
         mImageManager = ImageManager.getInstance(c);
         mContext = c;
         mObserver = new MyDataSetObserver();
       
         mImageManager.addObserver(mObserver); //將mObserver設置為mImageManager的觀察者
     }
 
    /**
      * 返回顯示的圖片的數目
      *
      * @see android.widget.Adapter#getCount()
      */
     public int getCount() {
         return mImageManager.size();
     }
 
    /**
      * 返回指定索引的圖片
      *
      * @see android.widget.Adapter#getItem(int)
      */
     public Object getItem(int position) {
         return mImageManager.get(position);
     }
 
    /**
      * 返回指定索引的圖片ID
      *
      * @see android.widget.Adapter#getItemId(int)
      */
     public long getItemId(int position) {
         PanoramioItem s = mImageManager.get(position);
         return s.getId();
     }
 
    /**
      * 返回指定索引處用於顯示圖片的view
      *
      * @param position 索引
      * @param convertView 可以重用的view,可能為null.
      * @param parent 返回的view的父view.
      * @return 用於顯示指定索引處圖片的view
      */
     public View getView(int position, View convertView, ViewGroup parent) {
         View view;
         if (convertView == null) {
             // 創建新的view
             LayoutInflater inflater = (LayoutInflater) mContext
                     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             view = inflater.inflate(R.layout.image_item, null);
         } else {
             // 使用已經存在的view
             view = convertView;
         }
         PanoramioItem s = mImageManager.get(position);
 
        ImageView i = (ImageView) view.findViewById(R.id.image);
         i.setImageBitmap(s.getBitmap()); //將位圖設置到view上
         i.setBackgroundResource(R.drawable.picture_frame); //設置ImageView的背景圖片
       
         TextView t = (TextView) view.findViewById(R.id.title);
         t.setText(s.getTitle()); //設置圖片名稱
       
         t = (TextView) view.findViewById(R.id.owner);
         t.setText(s.getOwner()); //設置圖片作者
         return view;
     }
 
}
 同理,ImageList.java文件內容如下所示:
 
[java]
 package com.google.android.panoramio;
 
 import android.app.ListActivity;
 import android.content.Context;
 import android.content.Intent;
 import android.database.DataSetObserver;
 import android.os.Bundle;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.Window;
 import android.widget.ListView;
 
 /**
  * 顯示圖片列表的Activity
  */
 public class ImageList extends ListActivity {
     
     ImageManager mImageManager;
     
     private MyDataSetObserver mObserver = new MyDataSetObserver();
 
     /**
      * 保存用戶在主界面選取搜索區域時所用的縮放級別
      */
     private int mZoom;
 
     /**
      * 保存用戶在主界面選取的搜索區域的中心點緯度
      */
     private int mLatitudeE6;
 
     /**
      * 保存用戶在主界面選取的搜索區域的中心點經度
      */
     private int mLongitudeE6;
 
     /**
      * 註冊為ImageManager實例的觀察者,用於當ImageManager結束圖片下載時將ImageList界面上的加載進度顯示關閉
      */
     private class MyDataSetObserver extends DataSetObserver {
         @Override
         public void onChanged() {
             if (!mImageManager.isLoading()) {
                 getWindow().setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS,
                         Window.PROGRESS_VISIBILITY_OFF);
             }
         }
 
         @Override
         public void onInvalidated() {
         }
     }
     
     @Override
     public void onCreate(Bundle savedInstanceState) {
         requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);//顯示不確定進度 
         super.onCreate(savedInstanceState);
         
         mImageManager = ImageManager.getInstance(this);
         
         //獲取ListView,並在底部添加版權信息 
         ListView listView = getListView();
         LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         View footer = inflater.inflate(R.layout.list_footer, listView, false);
         listView.addFooterView(footer, null, false);
         
         //將自定義的ImageAdapter設置給這個ListActivity 
         setListAdapter(new ImageAdapter(this));
 
         //在AndroidManifest.xml文件中為我們的List設置瞭Theme.Light,這裡將背景移除 
         listView.setBackgroundDrawable(null);
         //當ImageManager還在下載資源時,顯示進度條為忙,並註冊觀察者用於下載結束時隱藏進度條 
         if (mImageManager.isLoading()) {
             getWindow().setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS,
                     Window.PROGRESS_VISIBILITY_ON);
             mImageManager.addObserver(mObserver);
         }
         
         //保存從Panoramio主界面傳過來的搜索區域相關信息 
         Intent i = getIntent();
         mZoom = i.getIntExtra(ImageManager.ZOOM_EXTRA, Integer.MIN_VALUE);
         mLatitudeE6 = i.getIntExtra(ImageManager.LATITUDE_E6_EXTRA, Integer.MIN_VALUE);
         mLongitudeE6 = i.getIntExtra(ImageManager.LONGITUDE_E6_EXTRA, Integer.MIN_VALUE);
     }
 
     @Override
     protected void onListItemClick(ListView l, View v, int position, long id) {
         PanoramioItem item = mImageManager.get(position);   
         
         //創建Intent用於傳遞相關數據給ViewImage,用於顯示單張圖片信息 
         Intent i = new Intent(this, ViewImage.class);
         i.putExtra(ImageManager.PANORAMIO_ITEM_EXTRA, item);
         i.putExtra(ImageManager.ZOOM_EXTRA, mZoom);
         i.putExtra(ImageManager.LATITUDE_E6_EXTRA, mLatitudeE6);
         i.putExtra(ImageManager.LONGITUDE_E6_EXTRA, mLongitudeE6);
         startActivity(i);
     }   
     
 }
 
 摘自ASCE1885

發佈留言