android 获取网络图片显示在Gallery中
2010-09-14 23:52:00 来源:WEB开发网R.styleable.Gallery_android_galleryItemBackground, 0);
// 返回 TypedArray
a.recycle();
}
/* */
public int getCount()
{
return myImageURL.length;
}
/* ID */
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
/* */
public float getScale(boolean focused, int offset)
{
/* Formula: 1 / (2 ^ offset) */
return Math.max(0, 1.0f / (float) Math.pow(2, Math
.abs(offset)));
}
public View getView(int position, View convertView,
ViewGroup parent)
{
/* ImageView */
ImageView imageView = new ImageView(this.myContext);
try
{
URL aryURI = new URL(myImageURL[position]);
/* 打开连接 */
URLConnection conn = aryURI.openConnection();
conn.connect();
/* 转变为 InputStream */
InputStream is = conn.getInputStream();
/* 将InputStream转变为Bitmap */
Bitmap bm = BitmapFactory.decodeStream(is);
/* 关闭InputStream */
is.close();
/*添加图片*/
imageView.setImageBitmap(bm);
} catch (IOException e)
{
e.printStackTrace();
}
// 填充ImageView
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
/* 设置布局参数*/
imageView.setLayoutParams(new Gallery.LayoutParams(200, 150));
/* 设置背景资源 */
imageView.setBackgroundResource(mGalleryItemBackground);
return imageView;
}
}
更多精彩
赞助商链接