android Gallery实现加载网络图片
2013-03-14 16:06
423 查看
核心提示:其实很简单,Gallery 里设置 setAdapter() 里面添加图片的资源,android 获取网络图片显示在Gallery中,这需要继承BaseAdapter 重写里面的函数,值得一提的是需要写一个xml< ?xml version="1.0" encoding="utf-8
其实很简单,Gallery 里设置 setAdapter() 里面添加图片的资源,这需要继承BaseAdapter 重写里面的函数,值得一提的是需要写一个xml。在res/values下建立attrs.xml
< ?xml version="1.0" encoding="utf-8"?>
< resources>
< declare-styleable name="Gallery">
< attr name="android:galleryItemBackground" />
< /declare-styleable>
< /resources>
主要代码:
private Gallery myGallery01;
/* 图片资源 */
private String[] myImageURL = new String[]
{
"http://www.chinajilin.com.cn/att/site1/20071116/"
+ "img-1196620280651.jpg",
"http://image.szonline.net/UploadFile/album/2010/7/71315/2/"
+ "20100702111104_64763.jpg",
"http://www.fzl020.com/uploads/userup/0904/"
+ "30031RI2U.jpg",
"http://lh6.ggpht.com/_2N-HvtdpHZY/SZ357lAfZNE/AAAAAAAABOE/"
+ "dfxBtdINgPA/s144-c/20090220.jpg",
"http://news.xinhuanet.com/travel/2008-03/18/"
+ "xin_0620305181402218149794.jpg" };
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myGallery01 = (Gallery) findViewById(R.id.myGallery01);
myGallery01.setAdapter(new myInternetGalleryAdapter(this));
}
/* BaseAdapter */
public class myInternetGalleryAdapter extends BaseAdapter
{
private Context myContext;
private int mGalleryItemBackground;
/* 构造函数 Context */
public myInternetGalleryAdapter(Context c)
{
this.myContext = c;
// 检索 这方面的主题风格的属性
TypedArray a = myContext
.obtainStyledAttributes(R.styleable.Gallery);
//得到资源标识
mGalleryItem a.getResourceId(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;
}
}
其实很简单,Gallery 里设置 setAdapter() 里面添加图片的资源,这需要继承BaseAdapter 重写里面的函数,值得一提的是需要写一个xml。在res/values下建立attrs.xml
< ?xml version="1.0" encoding="utf-8"?>
< resources>
< declare-styleable name="Gallery">
< attr name="android:galleryItemBackground" />
< /declare-styleable>
< /resources>
主要代码:
private Gallery myGallery01;
/* 图片资源 */
private String[] myImageURL = new String[]
{
"http://www.chinajilin.com.cn/att/site1/20071116/"
+ "img-1196620280651.jpg",
"http://image.szonline.net/UploadFile/album/2010/7/71315/2/"
+ "20100702111104_64763.jpg",
"http://www.fzl020.com/uploads/userup/0904/"
+ "30031RI2U.jpg",
"http://lh6.ggpht.com/_2N-HvtdpHZY/SZ357lAfZNE/AAAAAAAABOE/"
+ "dfxBtdINgPA/s144-c/20090220.jpg",
"http://news.xinhuanet.com/travel/2008-03/18/"
+ "xin_0620305181402218149794.jpg" };
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myGallery01 = (Gallery) findViewById(R.id.myGallery01);
myGallery01.setAdapter(new myInternetGalleryAdapter(this));
}
/* BaseAdapter */
public class myInternetGalleryAdapter extends BaseAdapter
{
private Context myContext;
private int mGalleryItemBackground;
/* 构造函数 Context */
public myInternetGalleryAdapter(Context c)
{
this.myContext = c;
// 检索 这方面的主题风格的属性
TypedArray a = myContext
.obtainStyledAttributes(R.styleable.Gallery);
//得到资源标识
mGalleryItem a.getResourceId(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;
}
}
相关文章推荐
- android Gallery实现异步加载网络图片 并只加载当前停止页面图
- Android_开发 Gallery实现异步加载网络图片 并只加载当前停止页面图
- android 网络加载图片,对图片资源进行优化,并且实现内存双缓存 + 磁盘缓存
- android 加载网络图片 实现图片轮播
- Android循环滚动广告条的完美实现,封装方便,平滑过渡,从网络加载图片,点击广告进入对应网址
- Android实战简易教程<四十九>(两种方式实现网络图片异步加载)
- Android利用Gallery和ImageSwitcher实现在线相册图片预览功能(异步加载图片)
- Android 使用Glide加载网络图片等比例缩放的实现方法
- android的ViewPager实现加载网络图片并自动轮播
- Android循环滚动广告条的完美实现,封装方便,平滑过渡,从网络加载图片,点击广告进入对应网址
- android中listView实现异步加载网络图片
- android Gallery实现异步加载图片
- Android 使用Picasso加载网络图片等比例缩放的实现方法
- Android实现异步从网络加载图片列表 .
- Android开发实现加载网络图片并下载至本地SdCard的方法
- 网络相册:Gallery+AsyncTask+URLConnection 实现异步加载网络图片
- 用volley实现android加载网络图片(一)以及感受
- Android实现加载网络端图片
- android 网络加载图片,对图片资源进行优化,并且实现内存双缓存 + 磁盘缓存