Afianl加载网络图片(续)

上一篇已经讲了如何利用Afianl加载网络图片和下载文件,这篇文章将继续讲解使用Afinal加载网络图片的使用,主要结合listview的使用:

看效果图:

  

listview在滑动过程中没用明显卡顿,很流畅,这点优化的很不错,Afianl使用前当然是要先添加jar包啦,接下来看代码:

activity_main.xml:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:18px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.       
  8.     <ListView  
  9.         android:id="@+id/listview"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:fadingEdge="none"   
  13.         android:layout_marginLeft="3dp"  
  14.         android:layout_marginRight="3dp"  
  15.         android:dividerHeight="10dp" />  
  16.   
  17. </RelativeLayout>  
  18. </span>  

listview的条目布局list_item.xml:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:background="#FFFFFF"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <ImageView  
  9.         android:id="@+id/img"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:scaleType="fitXY"  
  13.         android:src="@drawable/ic_launcher" />  
  14.   
  15. </LinearLayout></span>  

MainActivity:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:18px;">package com.example.afinaltest2;  
  2.   
  3.   
  4. import java.util.ArrayList;  
  5. import java.util.HashMap;  
  6. import java.util.List;  
  7.   
  8. import net.tsz.afinal.FinalBitmap;  
  9. import android.os.Bundle;  
  10. import android.app.Activity;  
  11. import android.widget.ImageView;  
  12. import android.widget.ListView;  
  13.   
  14. public class MainActivity extends Activity {  
  15.       
  16.     ImageView img=null;  
  17.     FinalBitmap finalBitMap=null;  
  18.     ListView listview;  
  19.     ListAdapter listAdapter;  
  20.     HashMap<String, String> map ;  
  21.     ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();    
  22.     @Override  
  23.     protected void onCreate(Bundle savedInstanceState) {  
  24.         super.onCreate(savedInstanceState);  
  25.         setContentView(R.layout.activity_main);  
  26.           
  27.         String[] imgurl={  
  28.                 "http://a.hiphotos.baidu.com/image/pic/item/1f178a82b9014a90e68f8138ab773912b21bee86.jpg",  
  29.                 "http://f.hiphotos.baidu.com/image/pic/item/b58f8c5494eef01faf8fd3dde2fe9925bc317d0b.jpg",  
  30.                 "http://imgt8.bdstatic.com/it/u=2,687769429&fm=25&gp=0.jpg",  
  31.                 "http://imgt6.bdstatic.com/it/u=2,687777173&fm=25&gp=0.jpg",  
  32.                 "http://imgt7.bdstatic.com/it/u=2,687769721&fm=25&gp=0.jpg",  
  33.                 "http://imgt7.bdstatic.com/it/u=2,687776524&fm=25&gp=0.jpg",  
  34.                 "http://h.hiphotos.baidu.com/image/pic/item/1b4c510fd9f9d72a2fd4db05d62a2834349bbb72.jpg",  
  35.                 "http://imgt6.bdstatic.com/it/u=2,687777467&fm=25&gp=0.jpg",  
  36.                 "http://a.hiphotos.baidu.com/image/pic/item/a5c27d1ed21b0ef4fb685fdbdfc451da80cb3eb7.jpg",  
  37.                 "http://d.hiphotos.baidu.com/image/pic/item/0b7b02087bf40ad141490d60552c11dfa8ecce80.jpg",  
  38.                 "http://g.hiphotos.baidu.com/image/pic/item/03087bf40ad162d9cc4ab20413dfa9ec8a13cd06.jpg",  
  39.                 "http://imgt7.bdstatic.com/it/u=2,687775967&fm=25&gp=0.jpg",  
  40.                 "http://imgt8.bdstatic.com/it/u=2,687775693&fm=25&gp=0.jpg",  
  41.                 "http://imgt9.bdstatic.com/it/u=2,686139825&fm=25&gp=0.jpg",  
  42.                 "http://imgt7.bdstatic.com/it/u=2,687769677&fm=25&gp=0.jpg",  
  43.                 "http://d.hiphotos.baidu.com/image/pic/item/0bd162d9f2d3572c22bf5b598813632763d0c3d2.jpg"  
  44.         };  
  45.           
  46.         img=(ImageView) findViewById(R.id.img);  
  47.           
  48.         listview=(ListView) findViewById(R.id.listview);  
  49.           
  50.         for(int i=0;i<15;i++){  
  51.             map = new HashMap<String, String>();  
  52.             map.put("imgurl", imgurl[i]);  
  53.             listItem.add(map);  
  54.         }  
  55.           
  56.         listAdapter=new ListAdapter(this, listItem);  
  57.         listview.setAdapter(listAdapter);  
  58.           
  59.           
  60.           
  61.     }  
  62.   
  63.   
  64. }  
  65. </span>  

MainActivity未继承FianlActivity即未用注解方式,不过大家可以使用这种方式;


ListAdapter:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:18px;">package com.example.afinaltest2;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5.   
  6. import net.tsz.afinal.FinalBitmap;  
  7.   
  8.   
  9. import android.app.Activity;  
  10. import android.content.Context;  
  11. import android.view.LayoutInflater;  
  12. import android.view.View;  
  13. import android.view.ViewGroup;  
  14. import android.widget.BaseAdapter;  
  15. import android.widget.ImageView;  
  16.   
  17. public class ListAdapter extends BaseAdapter {  
  18.       
  19.     private Activity activity;  
  20.     private ArrayList<HashMap<String, String>> data;  
  21.     private static LayoutInflater inflater=null;  
  22.     public FinalBitmap imageLoader;   
  23.       
  24.     public ListAdapter(Activity a, ArrayList<HashMap<String, String>> d) {  
  25.         activity = a;  
  26.         data=d;  
  27.         inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  28.         imageLoader=FinalBitmap.create(activity.getApplicationContext());  
  29.         imageLoader.configLoadingImage(R.drawable.default_img);  
  30.     }  
  31.   
  32.     public int getCount() {  
  33.         return data.size();  
  34.     }  
  35.   
  36.     public Object getItem(int position) {  
  37.         return position;  
  38.     }  
  39.   
  40.     public long getItemId(int position) {  
  41.         return position;  
  42.     }  
  43.       
  44.     public View getView(int position, View convertView, ViewGroup parent) {  
  45.         View vi=convertView;  
  46.         if(convertView==null)  
  47.             vi = inflater.inflate(R.layout.list_item, null);  
  48.   
  49.         ImageView img = (ImageView) vi.findViewById(R.id.img);   
  50.   
  51.         HashMap<String, String> map = new HashMap<String, String>();  
  52.         map = data.get(position);  
  53.   
  54.         imageLoader.display(img, map.get("imgurl"));  
  55.           
  56.         return vi;  
  57.     }  
  58.        
  59. }  
  60. </span>  
其中,
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:18px;">imageLoader.configLoadingImage(R.drawable.default_img);</span>  
是设置图片加载未完成时显示的默认图片,最后依然不要忘了加权限。

使用Afianl框架时,不要只是将其中的方法拿来使用就算了,要学习它的编程思想,去思考为什么用这种方法,也可以指出它的不足之处,达到学以致用,而不是盲目的拿来主义。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值