android的GridView显示网络图片的方法 .

首先Activity.java的写法如下

[java] view plain copy print ?
  1. package com.lujianfei.active;  
  2.   
  3.    
  4. import java.util.ArrayList;  
  5. import java.util.HashMap;  
  6.   
  7. import android.app.Activity;  
  8. import android.graphics.Bitmap;  
  9. import android.os.Bundle;  
  10. import android.view.View;  
  11. import android.widget.GridView;  
  12. import android.widget.ImageView;  
  13. import android.widget.SimpleAdapter;  
  14. import android.widget.SimpleAdapter.ViewBinder;  
  15.   
  16. import com.lujianfei.data.IData;  
  17. import com.lujianfei.data.IDataFactory;  
  18. import com.lujianfei.util.BitmapHelper;  
  19.   
  20.   
  21. public class VideoprojActivity extends Activity {  
  22.       
  23.     IData data = null;  
  24.     IDataFactory dataFactory = null;  
  25.       
  26.     GridView gridview = null;    
  27.     
  28.   
  29.     /** Called when the activity is first created. */  
  30.     @Override  
  31.     public void onCreate(Bundle savedInstanceState) {  
  32.         super.onCreate(savedInstanceState);  
  33.         setContentView(R.layout.videoproj_gridview_layout);  
  34.         gridview = (GridView) findViewById(R.id.gridview);      
  35.           
  36.           
  37.         ArrayList> list=new ArrayList>();    
  38.         HashMap map=null;    
  39.            
  40.          
  41.             
  42.         for(int i=0;i<3;i++)    
  43.         {    
  44.         map=new HashMap();    
  45.         map.put("ItemImage",BitmapHelper.getBitmap("http://192.168.1.119:8080/strutsproj/upload/type1.jpg"));    
  46.         map.put("ItemText","ItemText"+i);    
  47.         list.add(map);    
  48.         }    
  49.         SimpleAdapter adapter = new SimpleAdapter(this,list,R.layout.videoproj_gridview_item_layout,new String[]{"ItemImage","ItemText"},new int[]{R.id.ItemImage,R.id.ItemText});    
  50.         adapter.setViewBinder(new ViewBinder(){    
  51.     
  52.           public boolean setViewValue(View view, Object data,     
  53.                   String textRepresentation) {     
  54.                 //判断是否为我们要处理的对象       
  55.                 if(view instanceof ImageView && data instanceof Bitmap){     
  56.                   ImageView iv = (ImageView) view;     
  57.                   iv.setImageBitmap((Bitmap) data);     
  58.                   return true;     
  59.                 }else     
  60.                 return false;     
  61.               }     
  62.     
  63.         
  64.       });    
  65.   
  66.         gridview.setAdapter(adapter);      
  67.   
  68.     }  
  69.        
  70. }  
package com.lujianfei.active;

 
import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.SimpleAdapter.ViewBinder;

import com.lujianfei.data.IData;
import com.lujianfei.data.IDataFactory;
import com.lujianfei.util.BitmapHelper;


public class VideoprojActivity extends Activity {
	
	IData data = null;
	IDataFactory dataFactory = null;
	
    GridView gridview = null;  
  

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.videoproj_gridview_layout);
        gridview = (GridView) findViewById(R.id.gridview);    
        
        
        ArrayList> list=new ArrayList>();  
        HashMap map=null;  
         
       
          
        for(int i=0;i<3;i++)  
        {  
        map=new HashMap();  
        map.put("ItemImage",BitmapHelper.getBitmap("http://192.168.1.119:8080/strutsproj/upload/type1.jpg"));  
        map.put("ItemText","ItemText"+i);  
        list.add(map);  
        }  
        SimpleAdapter adapter = new SimpleAdapter(this,list,R.layout.videoproj_gridview_item_layout,new String[]{"ItemImage","ItemText"},new int[]{R.id.ItemImage,R.id.ItemText});  
        adapter.setViewBinder(new ViewBinder(){  
  
          public boolean setViewValue(View view, Object data,   
                  String textRepresentation) {   
                //判断是否为我们要处理的对象    
                if(view instanceof ImageView && data instanceof Bitmap){   
                  ImageView iv = (ImageView) view;   
                  iv.setImageBitmap((Bitmap) data);   
                  return true;   
                }else   
                return false;   
              }   
  
      
      });  

        gridview.setAdapter(adapter);    

    }
     
}

 

核心类 BitmapHelper.java

[java] view plain copy print ?
  1. package com.lujianfei.util;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5. import java.net.HttpURLConnection;  
  6. import java.net.MalformedURLException;  
  7. import java.net.URL;  
  8.   
  9. import android.graphics.Bitmap;  
  10. import android.graphics.BitmapFactory;  
  11.   
  12. public class BitmapHelper {  
  13.     public static Bitmap getBitmap(String imageUrl) {  
  14.         Bitmap mBitmap = null;  
  15.         try {  
  16.             URL url = new URL(imageUrl);  
  17.             HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
  18.             InputStream is = conn.getInputStream();  
  19.             mBitmap = BitmapFactory.decodeStream(is);  
  20.   
  21.         } catch (MalformedURLException e) {  
  22.             e.printStackTrace();  
  23.         } catch (IOException e) {  
  24.             e.printStackTrace();  
  25.         }  
  26.   
  27.         return mBitmap;  
  28.     }  
  29.   
  30. }  
package com.lujianfei.util;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class BitmapHelper {
	public static Bitmap getBitmap(String imageUrl) {
		Bitmap mBitmap = null;
		try {
			URL url = new URL(imageUrl);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			InputStream is = conn.getInputStream();
			mBitmap = BitmapFactory.decodeStream(is);

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return mBitmap;
	}

}


 


videoproj_gridview_layout.xml

[html] view plain copy print ?
  1. xml version="1.0" encoding="utf-8"?>      
  2. <GridView xmlns:android="http://schemas.android.com/apk/res/android"       
  3.     android:id="@+id/gridview"      
  4.     android:layout_width="fill_parent"       
  5.     android:layout_height="fill_parent"      
  6.     android:numColumns="auto_fit"      
  7.     android:verticalSpacing="10dp"      
  8.     android:horizontalSpacing="10dp"      
  9.     android:columnWidth="90dp"      
  10.     android:stretchMode="columnWidth"      
  11.     android:gravity="center"      
  12. />    
    
  


 

videoproj_gridview_item_layout.xml

[html] view plain copy print ?
  1. xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:paddingBottom="4dip" >  
  6.   
  7.     <ImageView  
  8.         android:id="@+id/ItemImage"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_centerHorizontal="true" >  
  12.     ImageView>  
  13.   
  14.     <TextView  
  15.         android:id="@+id/ItemText"  
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content"  
  18.         android:layout_below="@+id/ItemImage"  
  19.         android:layout_centerHorizontal="true"  
  20.         android:text="TextView01" >  
  21.     TextView>  
  22.   
  23. RelativeLayout>  



    
    

    
    

 

运行效果如下

你可能感兴趣的:(Android开发)