SmartImageView

SmartImageView的設計初衷是來取代Android自帶的ImgageView組件,但它的功能遠不只imageview這麼簡單,它還提供了一些ImageView遠遠沒有卻常常在Android應用中經常用到的功能,如:

(1)支持通過URL來加載圖片;

(2)支持從電話簿中加載圖片;

(3)異步加載圖片;

(4)圖片被緩存在內存,以便下次快速加載顯示;

(5)SmartImageView類可以被很容易擴展成對其它資源的調用使用;

在做一些需要從網上獲取圖片的APP時,就難免要做很多處理。

這個項目就是針對這些做了很多處理。


Github項目地址:https://github.com/loopj/android-smart-image-view

作者地址:http://loopj.com/android-smart-image-view/



XML添加一個控件

[html] view plain copy
 
 print?在CODE上查看代碼片派生到我的代碼片
  1. <com.loopj.android.image.SmartImageView   
  2.         android:id="@+id/smart_image"  
  3.         android:layout_above="@id/button_layout"   
  4.         android:layout_width="match_parent"   
  5.         android:layout_height="match_parent"  
  6.         android:layout_alignParentTop="true"  
  7.         />   

獲取引用

mImageView = (SmartImageView)findViewById(R.id.smart_image);

獲取網絡圖片,這個過程本身就是異步。不必再進行處理,也不必擔心線程阻塞

         網絡獲取到的圖片都進行了緩存的處理。會在程序的cache目錄下建/web_image_cache/,圖片存在這裏,如下圖所示:



下次使用的時候,如果緩存圖片已經存在,則不再從網絡獲取圖片

         mImageView.setImageUrl("http://d.hiphotos.baidu.com/image/pic/item/838ba61ea8d3fd1f5a4c0234334e251f95ca5f72.jpg");

有一些功能,作者主頁並沒有說明,但是查看源碼可以看到

         先看.setImageUrl都有什麼方法

 

         1、最普通的一個,直接設置圖片地址

             // Helpers to set image by URL

             public void setImageUrl(String url) {

                 setImage(new WebImage(url));

             }

         2、有一個接口,完成下載的時候調用

             public void setImageUrl(String url,SmartImageTask.OnCompleteListener completeListener) {

                 setImage(new WebImage(url),completeListener);

             }

         3、從字面意思可以看出,是一個備用的資源。如果從網絡獲取圖片失敗,則使用備用資源

             public void setImageUrl(String url, finalInteger fallbackResource) {

                 setImage(new WebImage(url),fallbackResource);

             }

        

         4、類似上面

             public void setImageUrl(String url, finalInteger fallbackResource, SmartImageTask.OnCompleteListener completeListener) {

                 setImage(new WebImage(url),fallbackResource, completeListener);

             }

         5、多了一個loadingResource,就是正在下載的時候展示的圖片

             public void setImageUrl(String url, finalInteger fallbackResource, final Integer     loadingResource) {

                 setImage(new WebImage(url),fallbackResource, loadingResource);

             }

         6、類似上面

             public void setImageUrl(String url, finalInteger fallbackResource, final Integer loadingResource,SmartImageTask.OnCompleteListener completeListener) {

                 setImage(new WebImage(url),fallbackResource, loadingResource, completeListener);

             }

         SmartImageView確實很方便,能解決大部分問題。有不符合自己要求的地方,還可以根據源碼去修改。

項目運行示意圖:第1張爲開始界面,第二張爲網絡瀏覽,第三張爲通訊錄瀏覽



下面是項目實現:

項目目錄結構圖:



其中主佈局文件activity_main.xml爲

[html] view plain copy
 
 print?在CODE上查看代碼片派生到我的代碼片
  1. <strong><RelativeLayout  
  2.     xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <LinearLayout  
  9.         android:id="@+id/button_layout"   
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_alignParentBottom="true"  
  13.         android:orientation="horizontal">  
  14.         <Button    
  15.         android:id="@+id/net_button"    
  16.         android:layout_width="wrap_content"   
  17.         android:layout_height="wrap_content"    
  18.         android:text="@string/net_button_text"    
  19.         android:onClick="onClick"   
  20.         style=""   
  21.          />   
  22.          <Button    
  23.         android:id="@+id/phone_button"    
  24.         android:layout_width="wrap_content"    
  25.         android:layout_height="wrap_content"    
  26.         android:text="@string/phone_button_text"    
  27.         android:onClick="onClick"  
  28.         style=""     
  29.          />    
  30.          <TextView   
  31.              android:id="@+id/contact_id"  
  32.              android:layout_width="wrap_content"  
  33.              android:layout_height="wrap_content"/>  
  34.     </LinearLayout>  
  35.     <com.loopj.android.image.SmartImageView    
  36.         android:id="@+id/smart_image"  
  37.         android:layout_above="@id/button_layout"    
  38.         android:layout_width="match_parent"    
  39.         android:layout_height="match_parent"   
  40.         android:layout_alignParentTop="true"   
  41.         />    
  42. </RelativeLayout></strong>  

主Activity的java文件爲:

[java] view plain copy
 
 print?在CODE上查看代碼片派生到我的代碼片
  1. package com.shen.smartimageview;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.ContentResolver;  
  5. import android.database.Cursor;  
  6. import android.net.Uri;  
  7. import android.os.Bundle;  
  8. import android.provider.ContactsContract;  
  9. import android.util.Log;  
  10. import android.view.Menu;  
  11. import android.view.MenuItem;  
  12. import android.view.View;  
  13. import android.widget.TextView;  
  14.   
  15. import com.loopj.android.image.SmartImageView;  
  16.   
  17. public class MainActivity extends Activity {  
  18.   
  19.     private static final String  mWebPath = "http://d.hiphotos.baidu.com/image/pic/item/838ba61ea8d3fd1f5a4c0234334e251f95ca5f72.jpg";  
  20.     private static final Uri CONTACTS_URI = ContactsContract.Contacts.CONTENT_URI;  
  21.     private static final String _ID = ContactsContract.Contacts._ID;  
  22.     private static final String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;  
  23.       
  24.     private SmartImageView mImageView;  
  25.     private TextView mIdName;  
  26.     @Override  
  27.     protected void onCreate(Bundle savedInstanceState) {  
  28.         super.onCreate(savedInstanceState);  
  29.         setContentView(R.layout.activity_main);  
  30.          // 開源代碼實現好的SmartImageView    
  31.         mImageView = (SmartImageView) findViewById(R.id.smart_image);   
  32.         mIdName = (TextView)findViewById(R.id.contact_id);  
  33.     }  
  34.   
  35.      public void onClick(View view) {  
  36.          switch (view.getId()) {  
  37.         case R.id.net_button:  
  38.                
  39.             // setImageUrl(String url, Integer fallbackResource, Integer    
  40.             // loadingResource)    
  41.             // fallbackResource:圖片下載失敗時顯示的圖片 loadingResource:圖片正在下載顯示的圖片    
  42.             mImageView.setImageUrl(mWebPath, R.drawable.ic_launcher,    
  43.                     R.drawable.ic_launcher);    
  44.             break;  
  45.         case R.id.phone_button:  
  46.               
  47.             ContentResolver resolver = getContentResolver();   
  48.             Cursor c = resolver.query(CONTACTS_URI, nullnullnullnull);  
  49.             c.moveToFirst();  
  50.             long _id = c.getLong(c.getColumnIndex(_ID));  
  51.             String displayName = c.getString(c.getColumnIndex(DISPLAY_NAME));  
  52.             Log.d("id", String.valueOf(_id) + displayName);  
  53.             mIdName.setText("name:" + displayName);  
  54.             mImageView.setImageContact(_id,R.drawable.ic_launcher,R.drawable.ic_launcher);  
  55.             break;  
  56.         default:  
  57.             break;  
  58.         }  
  59.              
  60.         }    
  61.     @Override  
  62.     public boolean onCreateOptionsMenu(Menu menu) {  
  63.         // Inflate the menu; this adds items to the action bar if it is present.  
  64.         getMenuInflater().inflate(R.menu.main, menu);  
  65.         return true;  
  66.     }  
  67.   
  68.     @Override  
  69.     public boolean onOptionsItemSelected(MenuItem item) {  
  70.         // Handle action bar item clicks here. The action bar will  
  71.         // automatically handle clicks on the Home/Up button, so long  
  72.         // as you specify a parent activity in AndroidManifest.xml.  
  73.         int id = item.getItemId();  
  74.         if (id == R.id.action_settings) {  
  75.             return true;  
  76.         }  
  77.         return super.onOptionsItemSelected(item);  
  78.     }  
  79. }  

其他的文件可以從上面github或作者的網站上進行下載在這裏就不多說了

下面介紹一下對smartImageView的認識




轉自:http://blog.csdn.NET/cqtddt/article/details/42044875

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章