网络图片Viewpager轮播图的实现



由于是网络请求,所以利用Xutils的httputils的网络请求

添加进xutils的jar宝和Gson包

先写xml



    

    	android:orientation="horizontal" >
 
  


之后进行画小点的颜色,在res中新建drawable文件  其中选择shape的xml,画出点的两个形态

选中形态dots_facuse.xml




    

    


未选中形态dots_normal.xml




    

    





这里的数据利用了网络的图片链接 所以写了个bean

package com.lqy.text;

import java.io.Serializable;
import java.sql.Savepoint;
import java.util.ArrayList;

public class bean implements Serializable {
	private Mydata data;

	public bean() {
		super();
		// TODO Auto-generated constructor stub
	}

	public bean(Mydata data) {
		super();
		this.data = data;
	}

	@Override
	public String toString() {
		return "bean [data=" + data + "]";
	}

	public Mydata getData() {
		return data;
	}

	public void setData(Mydata data) {
		this.data = data;
	}

	public class Mydata implements Serializable {
		private ArrayList ad1;

		public Mydata() {
			super();
			// TODO Auto-generated constructor stub
		}

		public Mydata(ArrayList ad1) {
			super();
			this.ad1 = ad1;
		}

		@Override
		public String toString() {
			return "Mydata [ad1=" + ad1 + "]";
		}

		public ArrayList getAd1() {
			return ad1;
		}

		public void setAd1(ArrayList ad1) {
			this.ad1 = ad1;
		}
	}

	public class AD1 implements Serializable {
		private String image;

		public AD1() {
			super();
			// TODO Auto-generated constructor stub
		}

		public AD1(String image) {
			super();
			this.image = image;
		}

		@Override
		public String toString() {
			return "AD1 [image=" + image + "]";
		}

		public String getImage() {
			return image;
		}

		public void setImage(String image) {
			this.image = image;
		}
	}
}

之后 便是主axctivity

package com.lqy.text;

import java.util.ArrayList;

import com.google.gson.Gson;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lqy.text.bean.AD1;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.R.drawable;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.Toast;

public class MainActivity extends Activity {
	protected static final String TAG = "off";
	// 从网络请求的轮播图图片路径
	private String url = "http://m.yunifang.com/yunifang/mobile/home?random=59053&encode=50f40a0c6ea9f7ce18fc7a2fc3d0742f";
	private bean json;
	ArrayList adlist = new ArrayList();
	private ViewPager viewpager;
	private LinearLayout dots;
	private ArrayList dotslist;

	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (msg.what == 1) {
				int currentItem = viewpager.getCurrentItem();
				currentItem++;
				viewpager.setCurrentItem(currentItem);

				Log.i(TAG, ".............");
				startRool();
			}
		};
	};

	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		viewpager = (ViewPager) findViewById(R.id.viewpager);
		dots = (LinearLayout) findViewById(R.id.dots);

		// 初始化小点
		http();
		// 利用xutils.jar包进行解析

		viewpager.setCurrentItem(adlist.size() * 1000);
		// viewpager的滑动
		viewpagerscroview();

	}

	@SuppressWarnings("deprecation")
	private void viewpagerscroview() {
		viewpager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				// TODO Auto-generated method stub
				for (int i = 0; i < dotslist.size(); i++) {

					if (i == arg0 % dotslist.size()) {
						dotslist.get(i).setImageDrawable(
								getResources().getDrawable(
										R.drawable.dots_facuse));
					} else {
						dotslist.get(i).setImageDrawable(
								getResources().getDrawable(
										R.drawable.dots_normal));
					}
				}

			}

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

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub

			}
		});

		startRool();
	}

	private void http() {
		HttpUtils http = new HttpUtils();
		http.send(HttpMethod.GET, url, new RequestCallBack() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				// TODO Auto-generated method stub

			}

			@SuppressWarnings("deprecation")
			@Override
			public void onSuccess(ResponseInfo arg0) {
				// TODO Auto-generated method stub
				// 得到的字符串
				String info = arg0.result;
				System.out.println(info + "=====================");
				Gson gson = new Gson();
				json = gson.fromJson(info, bean.class);
				adlist.addAll(json.getData().getAd1());
				viewpager.setAdapter(new PagerAdapter() {

					@Override
					public boolean isViewFromObject(View arg0, Object arg1) {
						// TODO Auto-generated method stub
						return arg0 == arg1;
					}

					@Override
					public int getCount() {
						// TODO Auto-generated method stub
						return Integer.MAX_VALUE;
					}

					@Override
					public void destroyItem(ViewGroup container, int position,
							Object object) {
						// TODO Auto-generated method stub
						// super.destroyItem(container, position, object);
						container.removeView((View) object);
					}

					@Override
					public Object instantiateItem(ViewGroup container,
							int position) {
						// TODO Auto-generated method stub
						ImageView image = new ImageView(MainActivity.this);
						BitmapUtils map = new BitmapUtils(MainActivity.this);
						map.display(image, adlist.get(position % adlist.size())
								.getImage());

						image.setOnTouchListener(new OnTouchListener() {

							@Override
							public boolean onTouch(View v, MotionEvent event) {
								// TODO Auto-generated method stub
								switch (event.getAction()) {
								case MotionEvent.ACTION_DOWN:
									handler.removeCallbacksAndMessages(null);
									break;
								case MotionEvent.ACTION_UP:
									handler.sendEmptyMessageDelayed(1, 2000);
									break;
								case MotionEvent.ACTION_CANCEL:
									handler.sendEmptyMessageDelayed(1, 2000);
									break;

								default:
									break;
								}
								return true;
							}

						});
						container.addView(image);
						return image;
					}
				});

				initdots();
			}
		});
	}

	private void startRool() {
		// 开始轮播
		handler.sendEmptyMessageDelayed(1, 2000);
	}

	// 小点的实现
	private void initdots() {
		dotslist = new ArrayList();
		dotslist.clear();
		// 移除小点
		dots.removeAllViews();
		for (int i = 0; i < adlist.size(); i++) {

			ImageView image = new ImageView(MainActivity.this);
			// 小点初始化
			Drawable drawable = null;
			
			if (i == 0) {
				// 亮色图片哦、

				drawable = getResources().getDrawable(R.drawable.dots_facuse);
			} else {
				drawable = getResources().getDrawable(R.drawable.dots_normal);
			}
			// 添加进图片小点
			image.setImageDrawable(drawable);
			// 考虑到屏幕适配
			LayoutParams params = new LayoutParams(dip2px(this, 10), dip2px(
					this, 10));

			params.setMargins(dip2px(this, 5), 0, dip2px(this, 5), 0);
			dots.addView(image, params);
			// 将小点的空间添加到集合中
			dotslist.add(image);
		}

	}

	/**
	 * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
	 */
	public int dip2px(Context context, float dpValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}

	@Override
	protected void onDestroy() {
		// 移除所有的handler信息
		handler.removeCallbacksAndMessages(null);
		super.onDestroy();
	}

}



该来的总会来   你早晚得开始  何不早点呐


你可能感兴趣的:(网络图片Viewpager轮播图的实现)