carousel

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="zhanghaijiao.bawei.com.my1601r_3.MainActivity">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:id="@+id/vp"></android.support.v4.view.ViewPager>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/linPoint"
        android:gravity="center"
        android:layout_gravity="bottom"></LinearLayout>

    </FrameLayout>

</LinearLayout>

MainActivity

public class MainActivity extends AppCompatActivity {
    private List<String> imgUrls=new ArrayList<>();
    private ViewPager viewPager;
    private List<ImageView> lists=new ArrayList<>();
    private Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int cindex=viewPager.getCurrentItem();
            cindex++;
            //Set the current page
            viewPager.setCurrentItem(cindex);

            //Call the method
            setSelectedImage(cindex%imgUrls.size());
            //Send the message again
            sendEmptyMessageDelayed(1,1000);

        }
    };
    private LinearLayout linPoint;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = findViewById(R.id.vp);
        linPoint = findViewById(R.id.linPoint);
        //Initialize
        initData();
        MyVpAdapter adapter=new MyVpAdapter(this,imgUrls);
        viewPager.setAdapter(adapter);

        //The page displayed by default
        viewPager.setCurrentItem(imgUrls.size()*10);
        //Delay send message
        mHandler.sendEmptyMessageDelayed(1,1000);

    }
    private void initData() {
        imgUrls.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505151721118&di=649c9a43aed72fbc4d99ec1a031510c6&imgtype=0&src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F015c7d574b9f8f6ac72525aee98351.jpg");
        imgUrls.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505151956771&di=0eb6f306991d24b67a13ceb336f80102&imgtype=0&src=http%3A%2F%2Fi0.hdslb.com%2Fbfs%2Farchive%2F00613def3f1beb7a35ae136341be2b589bc46a2d.jpg_320x200.jpg");
        imgUrls.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505151847685&di=c7a4b5d08ec43fa629bcb690039a7629&imgtype=0&src=http%3A%2F%2Fattimg.dospy.com%2Fimg%2Fday_080625%2F20080625_2e91a10c444877e88827vri2ZKdGMvQo.jpg");
        imgUrls.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505151825129&di=70bf74b87d8a15cb91a2d79f15ed0eaf&imgtype=0&src=http%3A%2F%2Fattimg.dospy.com%2Fimg%2Fday_081016%2F20081016_fee215664d5740e56c13E2YB8giERFEX.jpg");
        imgUrls.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505746504&di=930c4d677a02328a142d6fa85ed14580&imgtype=jpg&er=1&src=http%3A%2F%2Fattimg.dospy.com%2Fimg%2Fday_090113%2F20090113_6ac58b42bea94f0b318e1B6BZb5lPZl5.jpg");

        //动态添加小圆点指示器
        for (int i = 0; i <imgUrls.size() ; i++) {
            ImageView imageView=new ImageView(this);
            if(i==0){
                imageView.setImageResource(R.drawable.point_selected);
            }else {
                imageView.setImageResource(R.drawable.point_mormal);
            }
            linPoint.addView(imageView);
            //Add to the collection
            lists.add(imageView);
        }
    }
    /**
     * Change the image of the small dots
     * @param pageIndex page index
     */
    private void setSelectedImage(int pageIndex){
        for (int i = 0; i <lists.size() ; i++) {
            if(i== pageIndex){
                lists.get(i).setImageResource(R.drawable.point_selected);//Set as selected
            }else {
                lists.get(i).setImageResource(R.drawable.point_mormal);
            }

        }
    }
}

MyVpAdapter
public class MyVpAdapter extends PagerAdapter {
    private Context context;
    private List<String>  urls;
    private DisplayImageOptions options;
    public MyVpAdapter(Context context,List<String> urls) {
        this.context = context;
        this.urls=urls;
        //.displayer(new RoundedBitmapDisplayer(100))//Set the rounded corners of the picture Note: The control must be set Width and height
        this.options=new DisplayImageOptions.Builder()
                .cacheInMemory(true)//Use memory
                cache.cacheOnDisk(true)//Use disk
                cache.showImageOnLoading(R.mipmap.ic_launcher)//Set the image being downloaded
                . showImageForEmptyUri(R.mipmap.ic_launcher)//The image displayed when the url is empty or the requested resource does not
                exist.showImageOnFail(R.mipmap.ic_launcher)//The download fails
                .bitmapConfig(Bitmap.Config.RGB_565)//Set the image color mode 1px=2 bytes ARGB_8888 1px=4 bytes ARGB_4444 1px=2 bytes ALPHA_8 1px=1
                bytes.imageScaleType(ImageScaleType.EXACTLY)/ /Set the zoom mode of the
                image.build();

    }
    //Number of pages
    @Override
    public int getCount() {
        return Integer.MAX_VALUE;//Unlimited
    }
    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }
    //Load page 1, add content 2. Return a key (Object type) associated with the current page
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView imageView=new ImageView(context);
        //Set the zoom method of the image
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);//Tile

        //Download network image
        ImageLoader.getInstance().displayImage(urls.get(position%urls.size()),imageView,options);

        //Add component
        container.addView(imageView);

        return imageView;
    }
    //销毁页面
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
       // super.destroyItem(container, position, object);
        container.removeView((View) object);//删除
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324734253&siteId=291194637