仅供学习,希望能帮到你,有不对的,缺少的,请指出来,谢谢
xml中我定义的布局
主xml的布局中:
<android.support.v4.view.ViewPager
android:id="@+id/v_vp"
android:layout_width="match_parent"
android:layout_height="150dp"
/>
<LinearLayout
android:id="@+id/l_dots"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_alignBottom="@id/v_vp"
>
</LinearLayout>
<===============================================================>
自定义圆点的xml-fouce
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<corners android:radius="8dp"/>
<solid android:color="#ff0000"/>
</shape>
<===============================================================>
自定义圆点的xml-normul
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<corners android:radius="8dp"/>
<solid android:color="#ff0000"/>
</shape>
<===============================================================>
public class MainActivity extends Activity {
//定义控件
private ViewPager vp;
private String[] paths;
private LinearLayout l_dots;
private ImageView iv;
private ArrayList<ImageView> list;
//轮播器
Handler hand=new Handler(){
public void handleMessage(android.os.Message msg) {
if(msg.what==0){
//得到当前的条目id
int currentItem = vp.getCurrentItem();
//让当前的id增加
currentItem++;
//加载下一页viewpager视图
vp.setCurrentItem(currentItem);
//再次发送handler,viewpager进入无限循环中,
hand.sendEmptyMessageDelayed(0, 2000);
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//得到数据
getData();
//找控件
findComp();
}
//找控件
private void findComp() {
list = new ArrayList<ImageView>();
vp = (ViewPager) findViewById(R.id.v_vp);
//在xml中设置的圆点布局
l_dots = (LinearLayout) findViewById(R.id.l_dots);
//循环,有几张图片,加载几个圆点
for(int i=0;i<paths.length;i++){
//自定义控件
iv = new ImageView(MainActivity.this);
//初始化,圆点状态
if(i==0){
iv.setImageResource(R.drawable.dots_fouse);
}else{
iv.setImageResource(R.drawable.dots_normal);
}
//把创建的圆点放入集合中
list.add(iv);
//设置控件imageview的长宽
LayoutParams params=new LayoutParams(20, 20);
//设置控件imageview彼此之间距离
params.setMargins(5,0,5,0);
//在xml布局中,加载new出来的imageview,并设置间距,和圆点的大小,长宽
l_dots.addView(iv,params);
}
//由于viewpager在第一页时,无法再往前,滑动了,此时设置path.length*5000可以使viewpage无限次的往前滑动
vp.setCurrentItem(paths.length*5000);
//适配viewpager
vp.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 Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
//创建一个imageview对象
ImageView iv=new ImageView(MainActivity.this);
//监听imageview控件,当手指触摸时,事件被触发
iv.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
// 手指按下的时候,停止自动轮播的任务
// 移除所有的消息及回调 null 移除所有
case MotionEvent.ACTION_DOWN:
hand.removeCallbacksAndMessages(null);
break;
case MotionEvent.ACTION_UP:
//再次开启循环
hand.sendEmptyMessageDelayed(0, 2000);
break;
case MotionEvent.ACTION_CANCEL:
//再次开启循环
hand.sendEmptyMessageDelayed(0, 2000);
break;
default:
break;
}
// 消费事件
// 不消费事件
return true;
}
});
//这是加载的第三方架包,为了显示图片
BitmapUtils u=new BitmapUtils(MainActivity.this);
u.display(iv, paths[position%paths.length]);
//要把加载的试图,加载到container变量中
container.addView(iv);
//返回试图
return iv;
}
@Override
public void destroyItem(ViewGroup container, int position,
Object object) {
//当超过加载的默认页数时,viewpager,会调用该方法,删除多余的
container.removeView((View) object);
}
});
//viewpager的监听器,当页面变动时,监听
vp.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
//这里是监听,当图片自动滑动,或者手动滑动时,圆点变化
for(int i=0;i<paths.length;i++){
//由于我们在viewpager适配中getcount()方法中设置了,Integer.MAXVALUES,这时我们就要对viewpager中加载的页码,进行处理,用页码取余(%),得到真正地页码
比如我一共有三页放入集合中i中,但是我把getcount()方法设置成了100,那么0%i.size()=0,1%i.size()=1,2%i.size()=2,当为三时,3%i.size()=0,4%i.size()=1,5%i.size()=2,6%i.size=0
if(i==arg0%paths.length){
list.get(i).setImageResource(R.drawable.dots_fouse);
}else{
list.get(i).setImageResource(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
}
});
//开启无限循环2秒之后发送
hand.sendEmptyMessageDelayed(0, 2000);
}
//得到数据,这时我tomcat中的路径
private void getData() {
paths = new String[] {
"http://10.1.3.22:8080/image/a.jpg",
"http://10.1.3.22:8080/image/b.jpg",
"http://10.1.3.22:8080/image/r.jpg"
};
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}