ViewPage添加小圆点

首先在drawable中,绘制两个不同颜色的圆形图片

1.红色的

xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
    >

    <solid android:color="#ff0000"/>

    <size android:height="10dp"
        android:width="10dp"/>
shape>
2白色的

xml version="1.0"encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
    >

    <solid android:color="#ffffff"/>

    <size android:height="10dp"
        android:width="10dp"/>
shape>
2.定义Imageloader

public class Application extends android.app.Application{

    @Override
    public void onCreate() {
        super.onCreate();
        imageloader();
    }

    private void imageloader() {

        //设置imageloader配置
        DisplayImageOptions options=new DisplayImageOptions.Builder()
                .cacheOnDisk(true)
                .cacheInMemory(true)
                .build();

        ImageLoaderConfiguration configuration=new ImageLoaderConfiguration.Builder(this)
                .defaultDisplayImageOptions(options)
                .build();

        //初始化ImageLoader
        ImageLoader.getInstance().init(configuration);
    }
}
3.定义viewpage的适配器
public class Adapter extends PagerAdapter {

   private ArrayList list;
    private Context context;

    public Adapter(ArrayList list, Context context) {
        this.list = list;
        this.context = context;
    }



    //修改成最大值,实现无限轮播
    @Override
    public int getCount() {
        return  Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        //定义图片
        ImageView imageView=new ImageView(context);
        //图品下载
        ImageLoader.getInstance().displayImage(list.get(position%list.size()),imageView);
        //设置图片平铺
      imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        container.addView(imageView);
        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
}

4.在MainActivity中进行引用
public class MainActivity extends AppCompatActivity {

    private ViewPager vp;
    private ArrayList list;
    private LinearLayout ll_yuan;
    private ArrayList dotlist;


    /**
     * 自动轮播创建Handler
     */
   private Handler handler = new Handler()
    {
        @Override
        public void handleMessage(Message msg) {
        // TODO Auto-generated method stub
        if(msg.what==101)
        {
            //⑤.得到viewpager的position
            int position = vp.getCurrentItem();
            position++;
            vp.setCurrentItem(position);
        }
        super.handleMessage(msg);
    }
    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         vp = (ViewPager) findViewById(R.id.vp);
         ll_yuan = (LinearLayout) findViewById(R.id.ll_yuan);
         vp.setCurrentItem(9999);

        list = new ArrayList();
        
        //图片加载到集合里
        list.add("http://img3.imgtn.bdimg.com/it/u=1865208273,4010052244&fm=26&gp=0.jpg");
        list.add("http://img1.imgtn.bdimg.com/it/u=3308987153,427503974&fm=26&gp=0.jpg");
        list.add("http://img2.imgtn.bdimg.com/it/u=2029864078,51167410&fm=26&gp=0.jpg");

        Adapter adapter=new Adapter(list,MainActivity.this);
        vp.setAdapter(adapter);
  
        addDots();
        add();

        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                for (int i = 0; i < list.size(); i++) {
                    if(position%list.size()==i)
                    {
                        //设置第一个为选中颜色
                        dotlist.get(i).setImageResource(R.drawable.shape_red);
                    }
                    else
                    {
                        dotlist.get(i).setImageResource(R.drawable.shape_width);
                    }
                }
            }

            @Override
            public void onPageSelected(int position) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    /**
     * 自动轮播
     */
    private void add() {
        Timer timer=new Timer();
        TimerTask task=new TimerTask() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                //④.发送一个空消息
                handler.sendEmptyMessage(101);
            }
        };
        timer.schedule(task, 0, 2000);
    }


    private void addDots() {
        dotlist = new ArrayList<>();
        for (int i = 0; i < list.size(); i++)
        {
            //有多少个图片定义多少imageview
            ImageView i1=new ImageView(this);
            if(i==0)
            {
                i1.setImageResource(R.drawable.shape_red);
            }
            else
            {
                i1.setImageResource(R.drawable.shape_width);
            }
            //圆点大小
            LinearLayout.LayoutParams ll=new LinearLayout.LayoutParams(10,10);
            //圆形间距
            ll.setMargins(5,0,5,0);
            //把ImageView放在LinearLayout中  把圆点放在LinearLayout
            ll_yuan.addView(i1,ll);
            dotlist.add(i1);
        }
    }

}


你可能感兴趣的:(ViewPage添加小圆点)