图片加载过程中
图片加载结束
MainActivity.java
package com.entel.research; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.widget.GridView; public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); GridView gridView = (GridView) findViewById(R.id.gridview); List<ImageAndText> list = new ArrayList<ImageAndText>(); list.add(new ImageAndText("http://www.qqtheme.com/touxiang/UploadPic/2009-11/2009119101320367.gif", "灰太狼")); list.add(new ImageAndText("http://www.qqtai.com/qqhead/UploadFiles_3178/200809/2008092004121344.jpg", "小米")); list.add(new ImageAndText("http://i2.itc.cn/20111107/965_cbc1dac4_dcb2_1638_c3b1_d3d57d816c78_1.jpg", "谷歌")); list.add(new ImageAndText("http://www.baidu.com/img/baidu_sylogo1.gif", "百度")); list.add(new ImageAndText("http://i1.itc.cn/20111107/70b_3bbc312b_a7d6_24bf_5e85_f8f181f25d0a_1.jpg", "灰太狼")); list.add(new ImageAndText("http://i1.itc.cn/20111107/2b7c_ca94ecbd_c09e_1d97_1de1_f14109b686e1_1.jpg", "小米")); list.add(new ImageAndText("http://i3.itc.cn/20111107/3ab_8ac7b5d8_442b_d8fe_a4fd_62df63bd0a54_1.jpg", "灰太狼")); list.add(new ImageAndText("http://i3.itc.cn/20111107/29e_9251f52f_9701_341e_672c_5caa426c8501_1.jpg", "小米")); list.add(new ImageAndText("http://i2.itc.cn/20111107/focus_5a_c1ecc3bf_05db_49ff_b005_37a10bc87e86_0.jpg", "灰太狼")); list.add(new ImageAndText("http://i2.itc.cn/20111107/616_fd2be256_04f2_02d5_cf93_31c9028f65b2_1.jpg", "小米")); list.add(new ImageAndText("http://www.qqtheme.com/touxiang/UploadPic/2009-11/2009119101320367.gif", "灰太狼")); list.add(new ImageAndText("http://i2.itc.cn/20111107/22d_4642f7a7_4676_a98f_7d72_5977d0980cac_1.jpg", "小米")); gridView.setAdapter(new ImageAndTextListAdapter(this, list, gridView)); } }
ImageAndTextListAdapter.java
package com.entel.research; import java.util.List; import android.app.Activity; import android.graphics.drawable.Drawable; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.GridView; import android.widget.ImageView; import android.widget.TextView; import com.entel.research.AsyncImageLoader.ImageCallback; public class ImageAndTextListAdapter extends ArrayAdapter<ImageAndText> { private GridView gridView; private AsyncImageLoader asyncImageLoader; public ImageAndTextListAdapter(Activity activity, List<ImageAndText> imageAndTexts, GridView gridView) { super(activity, 0, imageAndTexts); this.gridView = gridView; asyncImageLoader = new AsyncImageLoader(); } public View getView(int position, View convertView, ViewGroup parent) { Activity activity = (Activity) getContext(); // Inflate the views from XML View rowView = convertView; ViewCache viewCache; if (rowView == null) { LayoutInflater inflater = activity.getLayoutInflater(); rowView = inflater.inflate(R.layout.imageitem, null); viewCache = new ViewCache(rowView); rowView.setTag(viewCache); } else { viewCache = (ViewCache) rowView.getTag(); } ImageAndText imageAndText = getItem(position); // Load the image and set it on the ImageView String imageUrl = imageAndText.getImageUrl(); ImageView imageView = viewCache.getImageView(); imageView.setTag(imageUrl); Drawable cachedImage = asyncImageLoader.loadDrawable(imageUrl, new ImageCallback() { public void imageLoaded(Drawable imageDrawable, String imageUrl) { ImageView imageViewByTag = (ImageView) gridView .findViewWithTag(imageUrl); if (imageViewByTag != null) { imageViewByTag.setImageDrawable(imageDrawable); } } }); if (cachedImage == null) { imageView.setImageResource(R.drawable.icon); Log.e("Adapter", "null"); } else { imageView.setImageDrawable(cachedImage); } // Set the text on the TextView TextView textView = viewCache.getTextView(); textView.setText(imageAndText.getText()); return rowView; } }
ImageAndText.java
package com.entel.research; public class ImageAndText { private String imageUrl; private String text; public ImageAndText(String imageUrl, String text) { this.imageUrl = imageUrl; this.text = text; } public String getImageUrl() { return imageUrl; } public String getText() { return text; } }
ViewCache.java
package com.entel.research; import android.view.View; import android.widget.ImageView; import android.widget.TextView; public class ViewCache { private View baseView; private TextView textView; private ImageView imageView; public ViewCache(View baseView) { this.baseView = baseView; } public TextView getTextView() { if (textView == null) { textView = (TextView) baseView.findViewById(R.id.ItemText); } return textView; } public ImageView getImageView() { if (imageView == null) { imageView = (ImageView) baseView.findViewById(R.id.ItemImage); } return imageView; } }
AsyncImageLoader.java
package com.entel.research; import java.io.IOException; import java.io.InputStream; import java.lang.ref.SoftReference; import java.net.MalformedURLException; import java.net.URL; import java.util.HashMap; import java.util.Random; import android.graphics.drawable.Drawable; import android.os.Handler; import android.os.Message; public class AsyncImageLoader { private HashMap<String, SoftReference<Drawable>> imageCache; public AsyncImageLoader() { imageCache = new HashMap<String, SoftReference<Drawable>>(); } public Drawable loadDrawable(final String imageUrl, final ImageCallback imageCallback) { if (imageCache.containsKey(imageUrl)) { SoftReference<Drawable> softReference = imageCache.get(imageUrl); Drawable drawable = softReference.get(); if (drawable != null) { return drawable; } } final Handler handler = new Handler() { public void handleMessage(Message message) { imageCallback.imageLoaded((Drawable) message.obj, imageUrl); } }; new Thread() { @Override public void run() { Drawable drawable = loadImageFromUrl(imageUrl); imageCache.put(imageUrl, new SoftReference<Drawable>(drawable)); Message message = handler.obtainMessage(0, drawable); try { Thread.sleep(new Random().nextInt(3000)); } catch (Exception e) { e.printStackTrace(); } handler.sendMessage(message); } }.start(); return null; } public static Drawable loadImageFromUrl(String url) { URL m; InputStream i = null; try { m = new URL(url); i = (InputStream) m.getContent(); } catch (MalformedURLException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } Drawable d = Drawable.createFromStream(i, "src"); return d; } public interface ImageCallback { public void imageLoaded(Drawable imageDrawable, String imageUrl); } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:layout_width="fill_parent"> <TextView android:id="@+id/text" android:layout_height="wrap_content" android:layout_width="fill_parent"> </TextView> <GridView android:id="@+id/gridview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:numColumns="auto_fit" android:verticalSpacing="10dp" android:horizontalSpacing="10dp" android:columnWidth="70dp" android:stretchMode="columnWidth" android:gravity="center" android:layout_below="@id/text"/> </RelativeLayout>
imageitem.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:paddingBottom="4dip" android:layout_width="fill_parent"> <ImageView android:layout_height="wrap_content" android:id="@+id/ItemImage" android:layout_width="wrap_content" android:layout_centerHorizontal="true"> </ImageView> <TextView android:layout_width="wrap_content" android:layout_below="@+id/ItemImage" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:id="@+id/ItemText"> </TextView> </RelativeLayout>