此处的教程http://www.edumobile.org/android/android-beginner-tutorials/creating-image-gallery/教授如何将资源中的图像放入画廊
这是设置要显示的图像的部分:
private Integer[] mImageIds = {
R.drawable.icon,
R.drawable.icon,
R.drawable.icon
};
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
i.setImageResource(mImageIds[position]);
i.setLayoutParams(new Gallery.LayoutParams(150, 100));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
但我想显示一些来自网络的图像。所以这就是我所做的:
String gallery1 = "http://www.myimages.com/1.png";
URL ulrn = new URL(gallery1);
HttpURLConnection con = (HttpURLConnection)ulrn.openConnection();
InputStream is = con.getInputStream();
Bitmap bmp = BitmapFactory.decodeStream(is);
if (null != bmp){
bmp = getResizedBitmap(bmp,150,120); //resize the thumb
i.setImageBitmap(bmp);
i.setLayoutParams(new Gallery.LayoutParams(150, 100));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
但是使用我上面做的方法我只能在画廊内显示一张图片,而我还需要在画廊内显示另一张图片,例如来自http://www.myimages.com/2.png和http:// /www.myimages.com/3.png。我怎样才能做到这一点?