Android 使用Aquery加载图片

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.有缓存时直接加载缓存文件不再访问网络,没有缓存时加载网络上的图片。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值