android 获取网络图片显示在Gallery中

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);
     
      //得到资源标识
      mGalleryItemBackground = 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;
    }
}

 

转自:http://hi.baidu.com/cq_yajun/blog/item/d90923f98d3c0969034f5683.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值