android动态显示图片

package com.example.addtextview;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

       private String []product_pic_array = {"url","url"};
  private String []product_title_array = {"11","22"};
 
  private TextView title;
  private ImageView pic ;
  private LinearLayout ll_product ;
  private int row = 0;
         /**
          * 远程读取文件
          * @param imagePath
          * @return
          */
   public static Bitmap getBitmapFromServer(String imagePath) {

    HttpGet get = new HttpGet(imagePath);
    HttpClient client = new DefaultHttpClient();
    Bitmap pic = null;
    try {
     HttpResponse response = client.execute(get);
     HttpEntity entity = response.getEntity();
     InputStream is = entity.getContent();

     pic = BitmapFactory.decodeStream(is);   // 关键是这句代码

    } catch (ClientProtocolException e) {
     e.printStackTrace();
    } catch (IOException e) {
     e.printStackTrace();
    }
    return pic;
   }

  
 public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView( R.layout.activity_main );
  
       // 取得LinearLayout 物件
        ll_product = (LinearLayout)findViewById(R.id.ll_product);
       // 将TextView 加入到LinearLayout 中
        
        if(product_pic_array!=null && product_pic_array.length==1){
            new GetDataTask().execute(); //必须在新的线程中执行读取文件
        }else{
          new GetDataTask().execute();
        }
    
    }
 
 
 //异步请求类
   private class GetDataTask extends AsyncTask<Void, Void, List<ImageView>> {
       
            protected List<ImageView> doInBackground(Void... params) { 
             List<ImageView> list = new ArrayList<ImageView>();
              try {
               for(int i=0;i<product_pic_array.length;i++){
                Bitmap bitmap = getBitmapFromServer(product_pic_array[i]);
                pic = new ImageView(MainActivity.this);
                pic.setImageBitmap(bitmap);
                list.add(pic);
               }
   } catch (Exception e) {
    System.out.println(e);
   }
   return list;
             
            } 
            protected void onPostExecute(List<ImageView> list) {
             //在线程执行时不能把添加布局
             if(list!=null && list.size()==1){
               pic = (ImageView)list.get(0);
               ll_product.addView(pic);//向布局中动态输入图片
              
               title = new TextView(MainActivity.this); //向布局中动态输入TextView
                  title.setText(product_title_array[0]);
               ll_product. addView (title);
             }else{
              for(int i=0;i<list.size();i++){
               pic = (ImageView)list.get(i);
               //设置图片高度宽度
               LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(100, 100);
               pic.setLayoutParams(layoutParams);
                  ll_product.addView(pic);
                 }
             }
             
             
            } 
       
   }
}

相关推荐