Android开发实现网络加载多张图片并可以滑动

直接上代码:关键代码已标红,

public class Page_XiangQing_TongYong extends Activity implements OnClickListener{
    TextView tv_jiantou_bianmin_chuxing;//返回箭头 下为返回
    TextView tv_fanhui_bianmin_chuxing;
    TextView tv_bianmin_banshi_xiangqing_title;//标题
    TextView tv_bianmin_banshi_xiangqing_time;//时间
    WebView tv_content_bianmin_banshi_xiangqing;//内容
    ImageView img_content_bianmin_banshi_xiangqing;//图片
    //实现图片的异步加载
    public ImageLoader imageLoader;
    User_BianMin_SheQuHuoDong user;
    User_BianMin_XiangQing userHuoqu;
    Handler hand;
    //所有图盘的URL
    List list1;
    //实现图片滚动
     private ViewPager viewPager;  
     private ArrayList pageViews;  
     private ImageView imageView;  
     private ImageView[] imageViews;
     // ���ͼƬLinearLayout
     private ViewGroup main;
     // ���СԲ���LinearLayout
     private ViewGroup group;
@Override
protected void onCreate(Bundle savedInstanceState) {

    // TODO Auto-generated method stub
    //setContentView(R.layout.page_bianmin_banshi_xiangqing);//此处需注意
    super.onCreate(savedInstanceState);
    

    //imageload
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
                .threadPriority(Thread.NORM_PRIORITY - 2).denyCacheImageMultipleSizesInMemory()
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                .tasksProcessingOrder(QueueProcessingType.LIFO).build();
            ImageLoader.getInstance().init(config);
            imageLoader=ImageLoader.getInstance();
            Log.e("-进入通用界面----------------------", "");    

            
            //获取传输过来的数据
            Intent intent=getIntent();
            user=(User_BianMin_SheQuHuoDong) intent.getSerializableExtra("CODE");
              Log.e("-获取到信息id------",user.getId());  
            new getdata().start();
            
            
    hand=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            switch (msg.what) {
            case 1:
                  Log.e("---图片URL--------", list1.size()+"sadhkj");
                   //int[] img=new int[]{R.drawable.a1,R.drawable.a2,R.drawable.a3,R.drawable.a4,R.drawable.ic_launcher};
                   LayoutInflater inflater = getLayoutInflater();  
                   pageViews = new ArrayList();   
                   for(int j=0;j                        LinearLayout layout=new LinearLayout(getApplicationContext());
                       LayoutParams ltp=new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
                       final ImageView imageView=new ImageView(getApplicationContext());
                       imageView.setScaleType(ScaleType.FIT_XY);
                   //imageload
                       imageLoader.displayImage(list1.get(j), imageView,getDefaultOptions());
                       //super.onCreate(savedInstanceState);
                       layout.addView(imageView, ltp);
                       pageViews.add(layout);
                   }
                   imageViews = new ImageView[pageViews.size()];  
                   main = (ViewGroup)inflater.inflate(R.layout.page_bianmin_banshi_xiangqing, null);  
                   
                   group = (ViewGroup)main.findViewById(R.id.viewGroup);  
                   viewPager = (ViewPager)main.findViewById(R.id.guidePages);  
                   
                   for (int it = 0;it < pageViews.size(); it++) {  
                       imageView = new ImageView(getApplicationContext());  
                       imageView.setLayoutParams(new LayoutParams(10,10));  
                       imageView.setPadding(20, 0, 20, 0);  
                       imageViews[it] = imageView;            
                       if (it == 0) {  
                           imageViews[it].setBackgroundResource(R.drawable.select);  
                       } else {  
                           imageViews[it].setBackgroundResource(R.drawable.disselect);  
                       }             
                       group.addView(imageViews[it]);  
                   }       
                   setContentView(main);    
                   viewPager.setAdapter(new GuidePageAdapter());  
                   viewPager.setOnPageChangeListener(new GuidePageChangeListener());  

                   //
                   tv_jiantou_bianmin_chuxing=(TextView) findViewById(R.id.tv_jiantou_bianmin_chuxing);
                   tv_fanhui_bianmin_chuxing=(TextView) findViewById(R.id.tv_fanhui_bianmin_chuxing);
                   tv_bianmin_banshi_xiangqing_title=(TextView) findViewById(R.id.tv_bianmin_banshi_xiangqing_title);
                   tv_content_bianmin_banshi_xiangqing=(WebView) findViewById(R.id.tv_content_bianmin_banshi_xiangqing);
                   tv_bianmin_banshi_xiangqing_time=(TextView) findViewById(R.id.tv_bianmin_banshi_xiangqing_time);
                   
                   
                tv_bianmin_banshi_xiangqing_title.setText("    "+userHuoqu.getTitle());
                tv_bianmin_banshi_xiangqing_time.setText(userHuoqu.getDate());
                tv_content_bianmin_banshi_xiangqing.loadDataWithBaseURL("", userHuoqu.getContent(), "text/html", "UTF-8", "");;
                
                init();
                break;

            default:
                break;
            }
            super.handleMessage(msg);
        }
    };
    

    
}
public void init(){
    tv_jiantou_bianmin_chuxing=(TextView) findViewById(R.id.tv_jiantou_bianmin_chuxing);
    tv_fanhui_bianmin_chuxing=(TextView) findViewById(R.id.tv_fanhui_bianmin_chuxing);
    tv_bianmin_banshi_xiangqing_title=(TextView) findViewById(R.id.tv_bianmin_banshi_xiangqing_title);
    tv_content_bianmin_banshi_xiangqing=(WebView) findViewById(R.id.tv_content_bianmin_banshi_xiangqing);
    tv_bianmin_banshi_xiangqing_time=(TextView) findViewById(R.id.tv_bianmin_banshi_xiangqing_time);
    tv_fanhui_bianmin_chuxing=(TextView) findViewById(R.id.tv_fanhui_bianmin_chuxing);
    tv_fanhui_bianmin_chuxing.setOnClickListener(this);
    //img_content_bianmin_banshi_xiangqing=(ImageView) findViewById(R.id.img_content_bianmin_banshi_xiangqing);
}
//加载图片的方法
/**
 * 设置默认图片
 * @return
 */
public static DisplayImageOptions getDefaultOptions() {
    //设置图片加载的属性
    Builder b = new DisplayImageOptions.Builder();
    b.showImageForEmptyUri(R.drawable.no_pic);
    b.showImageOnFail(R.drawable.no_pic);
    b.showImageOnLoading(R.drawable.loading);
    b.resetViewBeforeLoading(Boolean.TRUE);
    b.cacheOnDisk(Boolean.TRUE);
    b.cacheInMemory(Boolean.TRUE);
    b.imageScaleType(ImageScaleType.EXACTLY_STRETCHED);
    return b.bitmapConfig(Bitmap.Config.RGB_565).build();
}
//获取服务器数据
class getdata extends Thread{
    @Override
    public void run() {
        // TODO Auto-generated method stub
        SoapObject soapobject=new SoapObject(name, method);
        Log.e("-获取的userid--------", user.getId());
        //soapobject.addProperty("condition","");
        soapobject.addProperty("id",user.getId());
        // TODO Auto-generated method stub
        WebServiceFormat webservice=new WebServiceFormat();
        String result=webservice.getData(name, method, url, soapobject);
        Log.e("------------------社区活动界面----------------------", result);
        try {
            JSONObject jsono=new JSONObject(result);
            userHuoqu=new User_BianMin_XiangQing();
            userHuoqu.setContent(jsono.getString("content"));
            userHuoqu.setDate(jsono.getString("releaseDate"));
            userHuoqu.setTitle(jsono.getString("title"));
            JSONArray jsona=jsono.getJSONArray("picPathSet");
            Log.e("jsona的长度", jsona.length()+"");
            list1=new ArrayList();
            int j;
            //获取图片的URL
            for(j=0;j                 JSONObject tupian=jsona.getJSONObject(j);
                list1.add(tupian.getString("picPath"));
                
            }
            userHuoqu.setPicpath(list1);
            
            Log.e("url总数",list1.size()+"" );
            
            
            
//            userHuoqu.setPicpath(tupian.getString("picPath"));
//            Log.e("--图片地址---------------------", tupian.getString("picPath"));
            if(userHuoqu!=null){
                Message mess=new Message();
                mess.what=1;
                hand.sendMessage(mess);
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        super.run();
    }
}
//获取
class GuidePageAdapter extends PagerAdapter {  
      
    @Override  
    public int getCount() {  
        return pageViews.size();  
    }  

    @Override  
    public boolean isViewFromObject(View arg0, Object arg1) {  
        return arg0 == arg1;  
    }  

    @Override  
    public int getItemPosition(Object object) {  
        // TODO Auto-generated method stub  
        return super.getItemPosition(object);  
    }  

    @Override  
    public void destroyItem(View arg0, int arg1, Object arg2) {  
        // TODO Auto-generated method stub  
        ((ViewPager) arg0).removeView(pageViews.get(arg1));  
    }  

    @Override  
    public Object instantiateItem(View arg0, int arg1) {  
        // TODO Auto-generated method stub  
        ((ViewPager) arg0).addView(pageViews.get(arg1));  
        return pageViews.get(arg1);  
    }  

    @Override  
    public void restoreState(Parcelable arg0, ClassLoader arg1) {  
        // TODO Auto-generated method stub  

    }  

    @Override  
    public Parcelable saveState() {  
        // TODO Auto-generated method stub  
        return null;  
    }  

    @Override  
    public void startUpdate(View arg0) {  
        // TODO Auto-generated method stub  

    }  

    @Override  
    public void finishUpdate(View arg0) {  
        // TODO Auto-generated method stub  

    }  
}
class GuidePageChangeListener implements OnPageChangeListener {  
      
    @Override  
    public void onPageScrollStateChanged(int arg0) {  
        // TODO Auto-generated method stub  

    }  

    @Override  
    public void onPageScrolled(int arg0, float arg1, int arg2) {  
        // TODO Auto-generated method stub  

    }  

    @Override  
    public void onPageSelected(int arg0) {  
        for (int i = 0; i < imageViews.length; i++) {  
            imageViews[arg0].setBackgroundResource(R.drawable.select);
            if (arg0 != i) {  
                imageViews[i].setBackgroundResource(R.drawable.disselect);  
            }  
        }
    }  
}
@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    switch (arg0.getId()) {
    case R.id.tv_fanhui_bianmin_chuxing:
        finish();
        break;

    default:
        break;
    }
}  

}
================================================分割线,下为布局文件=======================================================================================

              android:layout_marginBottom="30dp"
        android:layout_below="@+id/re1"
        android:layout_width="fill_parent"
        android:layout_height="150dp">
                    android:layout_gravity="center_horizontal"
            android:id="@+id/guidePages"
            android:layout_width="250dp"
            android:layout_height="150dp"/>
                    android:id="@+id/viewGroup"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:gravity="center_horizontal"
            android:orientation="horizontal">

   

你可能感兴趣的:(Android)