Android 使用Aquery加载图片
import java.io.File;
import com.androidquery.AQuery;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class MainActivity extends Activity {
private GridView gv;
private AQuery aq;
private LayoutInflater layoutInflater;
private String[] strs = new String[] {
"http://192.168.0.86:8080/DownGoingMIPDFile/image/sort/mobile/29a.png",
"http://192.168.0.86:8080/DownGoingMIPDFile/image/sort/mobile/30a.png",
"http://192.168.0.86:8080/DownGoingMIPDFile/image/sort/mobile/32a.png",
"http://192.168.0.86:8080/DownGoingMIPDFile/image/sort/mobile/36a.png",
"http://192.168.0.86:8080/DownGoingMIPDFile/image/sort/mobile/34a.png",
"http://192.168.0.86:8080/DownGoingMIPDFile/image/sort/mobile/35a.png",
"http://192.168.0.86:8080/DownGoingMIPDFile/image/sort/mobile/31a.png",
"http://192.168.0.86:8080/DownGoingMIPDFile/image/sort/mobile/33a.png",
"http://192.168.0.86:8080/DownGoingMIPDFile/image/sort/mobile/37a.png" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
aq = new AQuery(this);
gv = (GridView) findViewById(R.id.gv);
layoutInflater = LayoutInflater.from(this);
GVAdapter adapter = new GVAdapter();
gv.setAdapter(adapter);
}
private class GVAdapter extends BaseAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return strs.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return strs[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.gv_item, null);
}
ImageView iv = (ImageView) convertView.findViewById(R.id.iv);
File file = aq.getCachedFile(strs[position]);
if (file != null && file.exists()) {
aq.id(iv).image(file, 0);
System.out.println("缓存是存在的!" + "路径为=" + file.getAbsolutePath());
} else {
aq.id(iv).image(strs[position]);
System.out.println("缓存不存在!");
}
return convertView;
}
}
}
布局文件只有一个GridView,GridView的item里面只有一个ImageView.有缓存时直接加载缓存文件不再访问网络,没有缓存时加载网络上的图片。