上一篇已经讲了如何利用Afianl加载网络图片和下载文件,这篇文章将继续讲解使用Afinal加载网络图片的使用,主要结合listview的使用:
看效果图:
listview在滑动过程中没用明显卡顿,很流畅,这点优化的很不错,Afianl使用前当然是要先添加jar包啦,接下来看代码:
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fadingEdge="none"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:dividerHeight="10dp" />
</RelativeLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:orientation="vertical" >
<ImageView
android:id="@+id/img"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher" />
</LinearLayout>
package com.example.afinaltest2;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import net.tsz.afinal.FinalBitmap;
import android.os.Bundle;
import android.app.Activity;
import android.widget.ImageView;
import android.widget.ListView;
public class MainActivity extends Activity {
ImageView img=null;
FinalBitmap finalBitMap=null;
ListView listview;
ListAdapter listAdapter;
HashMap<String, String> map ;
ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] imgurl={
"http://a.hiphotos.baidu.com/image/pic/item/1f178a82b9014a90e68f8138ab773912b21bee86.jpg",
"http://f.hiphotos.baidu.com/image/pic/item/b58f8c5494eef01faf8fd3dde2fe9925bc317d0b.jpg",
"http://imgt8.bdstatic.com/it/u=2,687769429&fm=25&gp=0.jpg",
"http://imgt6.bdstatic.com/it/u=2,687777173&fm=25&gp=0.jpg",
"http://imgt7.bdstatic.com/it/u=2,687769721&fm=25&gp=0.jpg",
"http://imgt7.bdstatic.com/it/u=2,687776524&fm=25&gp=0.jpg",
"http://h.hiphotos.baidu.com/image/pic/item/1b4c510fd9f9d72a2fd4db05d62a2834349bbb72.jpg",
"http://imgt6.bdstatic.com/it/u=2,687777467&fm=25&gp=0.jpg",
"http://a.hiphotos.baidu.com/image/pic/item/a5c27d1ed21b0ef4fb685fdbdfc451da80cb3eb7.jpg",
"http://d.hiphotos.baidu.com/image/pic/item/0b7b02087bf40ad141490d60552c11dfa8ecce80.jpg",
"http://g.hiphotos.baidu.com/image/pic/item/03087bf40ad162d9cc4ab20413dfa9ec8a13cd06.jpg",
"http://imgt7.bdstatic.com/it/u=2,687775967&fm=25&gp=0.jpg",
"http://imgt8.bdstatic.com/it/u=2,687775693&fm=25&gp=0.jpg",
"http://imgt9.bdstatic.com/it/u=2,686139825&fm=25&gp=0.jpg",
"http://imgt7.bdstatic.com/it/u=2,687769677&fm=25&gp=0.jpg",
"http://d.hiphotos.baidu.com/image/pic/item/0bd162d9f2d3572c22bf5b598813632763d0c3d2.jpg"
};
img=(ImageView) findViewById(R.id.img);
listview=(ListView) findViewById(R.id.listview);
for(int i=0;i<15;i++){
map = new HashMap<String, String>();
map.put("imgurl", imgurl[i]);
listItem.add(map);
}
listAdapter=new ListAdapter(this, listItem);
listview.setAdapter(listAdapter);
}
}
MainActivity未继承FianlActivity即未用注解方式,不过大家可以使用这种方式;
package com.example.afinaltest2;
import java.util.ArrayList;
import java.util.HashMap;
import net.tsz.afinal.FinalBitmap;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
public class ListAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public FinalBitmap imageLoader;
public ListAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=FinalBitmap.create(activity.getApplicationContext());
imageLoader.configLoadingImage(R.drawable.default_img);
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_item, null);
ImageView img = (ImageView) vi.findViewById(R.id.img);
HashMap<String, String> map = new HashMap<String, String>();
map = data.get(position);
imageLoader.display(img, map.get("imgurl"));
return vi;
}
}
其中,imageLoader.configLoadingImage(R.drawable.default_img);
是设置图片加载未完成时显示的默认图片,最后依然不要忘了加权限。
使用Afianl框架时,不要只是将其中的方法拿来使用就算了,要学习它的编程思想,去思考为什么用这种方法,也可以指出它的不足之处,达到学以致用,而不是盲目的拿来主义。
Afianl加载网络图片(续),布布扣,bubuko.com
原文:http://blog.csdn.net/baiyuliang2013/article/details/25257213