Afianl加载网络图片(延续)

时间:2023-03-09 03:40:20
Afianl加载网络图片(延续)

上一页“已经谈到了如何使用Afianl网络负载的图片和下载文件,本文将继续介绍使用Afinal使用网络负载图片,主绑定listview采用:

看效果图:

Afianl加载网络图片(延续)

Afianl加载网络图片(延续)

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>

listview的条目布局list_item.xml:

<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>

MainActivity:

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即未用注解方式。只是大家能够使用这样的方式;

ListAdapter:

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框架时。不要仅仅是将当中的方法拿来使用就算了。要学习它的编程思想,去思考为什么用这样的方法。也能够指出它的不足之处。达到学因此,使用,而不是盲目的拿来主义。

版权声明:本文博主原创文章。博客,未经同意不得转载。