动态圆点轮播图

来源:互联网 发布:华沙 知乎 编辑:程序博客网 时间:2024/05/17 07:13
   布局 
<shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="oval"    >    <solid android:color="@color/colorPrimary"/>    <stroke android:width="1dp"/></shape>

<shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="oval"    >    <solid android:color="@color/colorAccent"/>    <stroke android:width="1dp"/></shape>

代码
    private ViewPager vp;    private LinearLayout ll;    //创建图片集合    private List<String> imageList;    private List<View> viewList = new ArrayList<>();    private int above = 0;    private int c = 0;    //定义handler    private Handler handler = new Handler(){        @Override        public void handleMessage(Message msg) {            int what = msg.what;            vp.setCurrentItem(what);        }    };    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_a);        //查找控件        vp = (ViewPager) findViewById(R.id.vp);        ll = (LinearLayout) findViewById(R.id.ll);        //获取图片路径        imageList = Arrays.asList("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3170338295,3308067375&fm=117&gp=0.jpg",                "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3641902857,2040981264&fm=26&gp=0.jpg",                "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2194630571,1526007154&fm=26&gp=0.jpg",                "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=2665764299,1613523446&fm=26&gp=0.jpg");        //创建图片适配器        MyPagerAdapter pagerAdapter = new MyPagerAdapter(MainActivity.this,imageList);        //关联数据        vp.setAdapter(pagerAdapter);        //调用方法        init();        //开启线程        new Thread(){            @Override            public void run() {                while (true){                    try {                        //每隔3秒图片自动播放                        sleep(3000);                        c++;                        handler.sendEmptyMessage(c);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                }            }        }.start();    }    private void init() {        for (int i=0;i<imageList.size();i++){            //定义一个view            View view = new View(this);            //圆点的大小            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(50,50);            //圆点的左右位置            params.rightMargin = 10;            params.leftMargin = 10;            view.setLayoutParams(params);            if (i == 0){                //选中圆点                view.setBackgroundResource(R.drawable.shape_select);            }else{                //未选中圆点                view.setBackgroundResource(R.drawable.shape_normal);            }            ll.addView(view);            viewList.add(view);        }        //监听        vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {            @Override            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {                viewList.get(position % imageList.size()).setBackgroundResource(R.drawable.shape_select);                viewList.get(above % imageList.size()).setBackgroundResource(R.drawable.shape_normal);                above = position;            }            @Override            public void onPageSelected(int position) {            }            @Override            public void onPageScrollStateChanged(int state) {            }        });    }
原创粉丝点击