package com.example.roolimage;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
public class MainActivity extends Activity {
private LinearLayout ll_dots;
private ViewPager viewPager;
private String[] imageUrls = new String[] {
"http://192.168.137.110:8080/ab.jpg",
"http://192.168.137.110:8080/ae.jpg",
"http://192.168.137.110:8080/af.jpg",
"http://192.168.137.110:8080/ah.jpg" };
private ArrayList<ImageView> dotsList;
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
if (msg.what == 0) {
// 先获取当前页码值
int currentItem = viewPager.getCurrentItem();
// 设置条目页码加加
currentItem++;
viewPager.setCurrentItem(currentItem);
// 再发送消息
move();
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.viewPager);
ll_dots = (LinearLayout) findViewById(R.id.ll_dots);
// 设置viewPager的数据适配器
viewPager.setAdapter(new MyPagerAdapter(this, imageUrls, handler));
// 设置当前的页码
viewPager.setCurrentItem(10000000);
initDots();
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
for (int i = 0; i < dotsList.size(); i++) {
if (position % imageUrls.length == i) {
dotsList.get(i).setImageDrawable(
getResources().getDrawable(
R.drawable.dots_focused));
} else {
dotsList.get(i).setImageDrawable(
getResources().getDrawable(
R.drawable.dots_normal));
}
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
// 先执行发送消息
move();
}
public void move() {
handler.sendEmptyMessageDelayed(0, 2000);
}
// 初始化小点
private void initDots() {
dotsList = new ArrayList<ImageView>();
ll_dots.removeAllViews();
dotsList.clear();
for (int i = 0; i < imageUrls.length; i++) {
ImageView imageView = new ImageView(this);
// 加小点
if (i == 0) {
// 默认就是一个亮的小点
imageView.setImageDrawable(getResources().getDrawable(
R.drawable.dots_focused));
} else {
imageView.setImageDrawable(getResources().getDrawable(
R.drawable.dots_normal));
}
// 设置小点的宽高 dp---dip
LayoutParams params = new LayoutParams(20, 20);
// 将imageView添加到线性布局里边
// 设置边距
params.setMargins(5, 0, 5, 0);
ll_dots.addView(imageView, params);
// 将小点添加到集合中
dotsList.add(imageView);
}
}
}
///
package com.example.roolimage;
import android.content.Context;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.lidroid.xutils.BitmapUtils;
public class MyPagerAdapter extends PagerAdapter {
protected static final String TAG = “MyPagerAdapter”;
private String[] imageUrls;
private Context context;
private Handler handler;
public MyPagerAdapter(Context context, String[] imageUrls, Handler handler) {
this.context = context;
this.imageUrls = imageUrls;
this.handler = handler;
}
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
imageView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
// 按下
case MotionEvent.ACTION_DOWN:
// 移除所有消息和任务
handler.removeCallbacksAndMessages(null);
Log.i(TAG, "down......");
break;
// 手指抬起
case MotionEvent.ACTION_UP:
handler.sendEmptyMessageDelayed(0, 2000);
Log.i(TAG, "up......");
break;
// 取消触摸事件
case MotionEvent.ACTION_CANCEL:
Log.i(TAG, "cancel......");
handler.sendEmptyMessageDelayed(0, 2000);
break;
default:
break;
}
// 是否消费该事件 事件的传递机制
return true;
}
});
BitmapUtils bitmapUtils = new BitmapUtils(context);
bitmapUtils.display(imageView, imageUrls[position % imageUrls.length]);
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}