这个例子需要点基础知识:
Gallery的使用,这个网上的例子多的是
- 如何使用AsyncTask
- 使用URLConnection从网络下载图片
- 异步加载在Android应用上用处很广泛,这篇例子的Adapter部分完全可以移植到其他的组件中,如ListView。
下面,我们来看代码吧:
首先,是最核心的代码ImageAdapter的实现,所有核心的功能都是在这里实现的
public class ImageAdapter extends BaseAdapter{private List<String> imageUrls;private Context context;public ImageAdapter(List<String> imageUrls, Context context) {this.imageUrls = imageUrls;this.context = context;}public int getCount() {return imageUrls.size();}public Object getItem(int position) {return imageUrls.get(position);}public long getItemId(int position) {return position;}public View getView(int position, View convertView, ViewGroup parent) {Bitmap image;if(convertView==null){convertView = LayoutInflater.from(context).inflate(R.layout.item,null); //实例化convertViewimage = UrlConnectionActivity.imagesCache.get(imageUrls.get(position)); //从缓存中读取图片if(image==null){//当缓存中没有要使用的图片时,先显示默认的图片image = UrlConnectionActivity.imagesCache.get("background_non_load");//异步加载图片LoadImageTask task = new LoadImageTask(convertView);task.execute(imageUrls.get(position));}convertView.setTag(image);}else{image = (Bitmap) convertView.getTag();}ImageView imageView = (ImageView) convertView.findViewById(R.id.gallery_image);imageView.setImageBitmap(image);imageView.setScaleType(ImageView.ScaleType.FIT_XY);return convertView;}//加载图片的异步任务class LoadImageTask extends AsyncTask<String,Void,Bitmap>{private View resultView;LoadImageTask(View resultView) {this.resultView = resultView;}// doInBackground完成后才会被调用@Overrideprotected void onPostExecute(Bitmap bitmap) {//调用setTag保存图片以便于自动更新图片resultView.setTag(bitmap);}//从网上下载图片@Overrideprotected Bitmap doInBackground(String... params) {Bitmap image=null;try {//new URL对象 把网址传入URL url = new URL(params[0]);//取得链接URLConnection conn = url.openConnection();conn.connect();//取得返回的InputStreamInputStream is = conn.getInputStream();//将InputStream变为Bitmapimage = BitmapFactory.decodeStream(is);is.close();UrlConnectionActivity.imagesCache.put(params[0],image); //把下载好的图片保存到缓存中} catch (Exception e) {e.printStackTrace();}return image;}}}
在Activity中使用
public class MyActivity extends Activity{public static HashMap<String,Bitmap> imagesCache=new HashMap<String, Bitmap>(); //图片缓存private Gallery images_ga;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.url_connection_image);init();}private void init(){Bitmap image= BitmapFactory.decodeResource(getResources(),R.drawable.jiazaizhong);imagesCache.put("background_non_load",image); //设置缓存中默认的图片images_ga = (Gallery) findViewById(R.id.image_wall_gallery);List<String> urls = new ArrayList<String>(); //图片地址List//奶茶MM的图片,嘻嘻urls.add("http://fujian.xabbs.com/forum/201109/02/160646nn9hjjiimixvkxhe.jpg");urls.add("http://img1.cache.netease.com/catchpic/A/A9/A9D98040B397C366AE93E67871346561.jpg");urls.add("http://new.aliyiyao.com/UpFiles/Image/2011/01/13/nc_129393721364387442.jpg");urls.add("http://pic.viewpics.cn/2011/07/03/naichaMMzhangzetianzuixinzhaopian/18.jpg");urls.add("http://i1.sinaimg.cn/ent/m/c/2010-01-18/U1819P28T3D2847679F326DT20100118115712.jpg");urls.add("http://comic.sinaimg.cn/2011/0824/U5237P1157DT20110824161051.jpg");ImageAdapter imageAdapter = new ImageAdapter(urls,this);images_ga.setAdapter(imageAdapter);}}
在ImageAdapter中用到的XML文件:res/layout/item.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/gallery_image"/></LinearLayout>
浙公网安备 33010602011771号