我用的是异步加载图片的方式,并且使用了缓存的方式。
1、这是继承了AsyncTask<String, Void, Bitmap>的MyTask类,用异步加载图片
public class MyTask extends AsyncTask<String, Void, Bitmap> { private ImageView imageView; private LruCache<String, Bitmap> lruCache; /** * 构造方法,需要把ImageView控件和LruCache对象传进来 * * @param imageView * 加载图片到此 * @param lruCache * 缓存图片的对象 */ public MyTask(ImageView imageView, LruCache<String, Bitmap> lruCache) { super(); this.imageView = imageView; this.lruCache = lruCache; } /** * 调用LruCache的put 方法将图片加入内存缓存中,要给这个图片一个key 方便下次从缓存中取出来 * * @param key * @param bitmap */ private void addBitmapToMemoryCache(String key, Bitmap bitmap) { if (getBitmapFromMemoryCache(key) == null) { lruCache.put(key, bitmap); } } /** * 调用Lrucache的get 方法从内存缓存中去图片 * * @param key * @return */ public Bitmap getBitmapFromMemoryCache(String key) { return lruCache.get(key); } @Override protected Bitmap doInBackground(String... params) { // TODO Auto-generated method stub Bitmap bitmap = null; try { HttpURLConnection connection = (HttpURLConnection) new URL( params[0]).openConnection(); connection.setConnectTimeout(10000); connection.setRequestMethod("GET"); connection.setDoInput(true); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { InputStream instr = connection.getInputStream(); bitmap = BitmapFactory.decodeStream(instr); instr.close(); } } catch (Exception e) { e.printStackTrace(); } addBitmapToMemoryCache(params[0], bitmap); return bitmap; } @Override protected void onPostExecute(Bitmap result) { this.imageView.setImageBitmap(result); } }
2、这是继承了BaseAdapter的MyAdapter类
public class MyAdapter extends BaseAdapter { private Context context; private LayoutInflater inflater; private List<String> picPath; private final int maxMemory = (int) Runtime.getRuntime().maxMemory(); // 获取当前应用程序所分配的最大内存 private final int cacheSize = maxMemory / 5; // 只分5分之一来做缓存 private LruCache<String, Bitmap> mlruCache = new LruCache<String, Bitmap>( cacheSize) { protected int sizeOf(String key, Bitmap bitmap) { // 复写sizeof()方法 return bitmap.getRowBytes() * bitmap.getHeight() / 1024;// 这是按多少KB来算 }; }; public MyAdapter(Context context, List<String> picPath) { super(); this.context = context; this.picPath = picPath; inflater = LayoutInflater.from(context); } @Override public int getCount() { // TODO Auto-generated method stub return picPath.size(); } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int arg0) { // TODO Auto-generated method stub return 0; } @Override public View getView(int arg0, View v, ViewGroup arg2) { ViewHolder holder = null; if (v == null) { holder = new ViewHolder(); v = inflater.inflate(R.layout.item_xml, null); holder.showiamge = (ImageView) v.findViewById(R.id.showiamge); v.setTag(holder); } else { holder = (ViewHolder) v.getTag(); } if (v != null) { loadBitmap(picPath.get(arg0), holder.showiamge); } return v; } /** * @param urlStr * 所需要加载的图片的url。以String形式传进来,可以把这个作为缓存图片的key * @param image * ImageView 控件 */ private void loadBitmap(String urlStr, ImageView image) { MyTask myTask = new MyTask(image, mlruCache); // 声明一个异步图片加载对象 Bitmap bitmap = myTask.getBitmapFromMemoryCache(urlStr); // 首先从内存缓存中获取图片 if (bitmap != null) { image.setImageBitmap(bitmap);// 如果缓存中存在这张图片则直接设置给ImageView } else { image.setImageResource(R.drawable.ic_launcher);// 否则先设置成默认的图片 myTask.execute(urlStr);// 然后执行异步任务AsycnTask 去网上加载图片 } } } class ViewHolder { public ImageView showiamge; }
3、MainActivity类
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); context = getApplicationContext(); picPath=Utils.addUrl(); listView = (ListView) findViewById(R.id.listView1); MyAdapter adapter = new MyAdapter(context,picPath); listView.setAdapter(adapter); }
4、图片地址的工具类Utils。此图片地址是我电脑上服务器的,运行时需要改成想显示图片的地址。
public class Utils { /**图片的下载地址 * @return */ public static List<String> addUrl() { List<String> urlList = new ArrayList<String>(); urlList.add("http://192.168.4.101/u=323544003,3655607636&fm=21&gp=0.jpg"); urlList.add("http://192.168.4.101/u=337829107,754021853&fm=21&gp=0.jpg"); urlList.add("http://192.168.4.101/u=516070125,1299753376&fm=21&gp=0.jpg"); urlList.add("http://192.168.4.101/u=805375513,1202624231&fm=21&gp=0.jpg"); urlList.add("http://192.168.4.101/u=856554332,2565501919&fm=21&gp=0.jpg"); urlList.add("http://192.168.4.101/u=1107822669,2961831310&fm=21&gp=0.jpg"); urlList.add("http://192.168.4.101/u=1285718395,2833183433&fm=21&gp=0.jpg"); urlList.add("http://192.168.4.101/u=1826201872,3302300664&fm=21&gp=0.jpg"); urlList.add("http://192.168.4.101/u=3384623684,3529436072&fm=21&gp=0.jpg"); urlList.add("http://192.168.4.101/u=3936768754,2397124933&fm=21&gp=0.jpg"); urlList.add("http://192.168.4.101/u=4271458677,998187374&fm=21&gp=0.jpg"); urlList.add("http://192.168.4.101/1.jpg"); urlList.add("http://192.168.4.101/2.jpg"); urlList.add("http://192.168.4.101/3.jpg"); urlList.add("http://192.168.4.101/4.jpg"); urlList.add("http://192.168.4.101/5.jpg"); urlList.add("http://192.168.4.101/6.jpg"); urlList.add("http://192.168.4.101/7.jpg"); return urlList; } }
本人菜鸟一枚,这是本人第一次网上分享本人的成果,上述代码整合了多位大神的结晶,感觉还不错,如果不对的地方请指示出来。谢谢!
来源:https://www.cnblogs.com/dylh2010/p/4487053.html