首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

兑现的Gallery加载网络图片

2012-07-03 
实现的Gallery加载网络图片public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInsta

实现的Gallery加载网络图片
public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        //不显示程序的标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        //不显示系统的标题栏
        getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,WindowManager.LayoutParams. FLAG_FULLSCREEN);
       
        setContentView(R.layout.main);
     
     WindowManager windowmanager = getWindowManager();
     Display display = windowmanager.getDefaultDisplay();
     SCREEN_WIDTH = display.getWidth();
     SCREEN_HEIGHT = display.getHeight();
       
        //引用gallery组件
        Gallery g = (Gallery) findViewById(R.id.gallery);
       
        //在此下面定制适配器
        g.setAdapter(new ImageAdapter(this));
       
        //设置画廊中的图片边距
        g.setSpacing(2);
        g.setOnItemSelectedListener(this);
       
        imageview[0] = (ImageView) findViewById(R.id.imageView1);
  imageview[1] = (ImageView) findViewById(R.id.imageView2);
  imageview[2] = (ImageView) findViewById(R.id.imageView3);
  imageview[3] = (ImageView) findViewById(R.id.imageView4);
  imageview[4] = (ImageView) findViewById(R.id.imageView5);
  imageview[5] = (ImageView) findViewById(R.id.imageView6);
  imageview[6] = (ImageView) findViewById(R.id.imageView7);
  tv = (TextView) findViewById(R.id.text);
               
        //设置一个itemclicklistener,
        g.setOnItemClickListener(new OnItemClickListener(){
   public void onItemClick(AdapterView parent, View v, int position,
     long id) {
    //注释后 未显示短按组件
    Toast.makeText(testgallery.this, "" + position, Toast.LENGTH_SHORT).show();
   }
        });

}
           
    public class ImageAdapter extends BaseAdapter{
     int mGalleryItemBackground;
     private Context mContext;
    
     public ImageAdapter(Context c){
      mContext = c;
     }
  public int getCount() {
   return imageurl.length;
  }

  public Object getItem(int position) {
   return position;
  }

  public long getItemId(int position) {
   return position;
  }

  public View getView(final int position, View convertView, ViewGroup parent) {
   ImageView i = new ImageView(mContext);
   //i.setImageResource(mImageIds[position]);
   /*异步进行加载*/
   new Thread(){
    public void run(){
     URL url;
     try {
      if(bm[position] == null){
       url = new URL(imageurl[position]);
       bm[position] = BitmapFactory.decodeStream(url.openStream());
      }
     } catch (MalformedURLException e) {
      e.printStackTrace();
     } catch (IOException e) {
      e.printStackTrace();
     }
    }
   }.start();
  
   if(bm[position] != null){
    i.setImageBitmap(bm[position]);
   }else{
    i.setImageResource(mImageIds[0]);
   }
   i.setScaleType(ImageView.ScaleType.CENTER_CROP);//FIT_XY centerCrop
  
   //i.setLayoutParams(new Gallery.LayoutParams(20, 20));//136  88 //图片的宽高
   i.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
  
   //设置Gallery组件的背景风格
   i.setBackgroundResource(mGalleryItemBackground);
  
   return i;
  }
  private String[] imageurl = {
    "http://pic0.qiyipic.com/common/20110214/768e29bae49148708c8341f8781c2a0a.jpg",
    "http://pic3.qiyipic.com/common/20110214/d5c22c49cce04f3d827961f9f615e9d1.jpg",
    "http://sports.tom.com/uldf/2011/0213/goujianzhen/fpx.jpg",
    "http://www.qiyipic.com/lvyou/fix/csmlga.jpg",
    "http://www.qiyipic.com/lvyou/fix/sqxy.jpg",
    "http://www.qiyipic.com/lvyou/fix/astrj.jpg",
    "http://www.qiyipic.com/thumb/20110223/v73694.jpg"
  };
  private Integer[] mImageIds = {
                R.drawable.gallery_photo_1,
                R.drawable.gallery_photo_2,
                R.drawable.gallery_photo_3,
                R.drawable.gallery_photo_4,
                R.drawable.gallery_photo_5,
                R.drawable.gallery_photo_6,
                R.drawable.gallery_photo_7
        };
    }

热点排行