這個例子須要點基礎知識:
Gallery的使用,這個網上的例子多的是java
下面,咱們來看代碼吧:
首先,是最核心的代碼ImageAdapter的實現,全部核心的功能都是在這裏實現的android
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); //實例化convertView image = 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完成後纔會被調用 @Override protected void onPostExecute(Bitmap bitmap) { //調用setTag保存圖片以便於自動更新圖片 resultView.setTag(bitmap); } //從網上下載圖片 @Override protected Bitmap doInBackground(String... params) { Bitmap image=null; try { //new URL對象 把網址傳入 URL url = new URL(params[0]); //取得連接 URLConnection conn = url.openConnection(); conn.connect(); //取得返回的InputStream InputStream is = conn.getInputStream(); //將InputStream變爲Bitmap image = 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; @Override protected 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"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/gallery_image"/></LinearLayout>