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


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值