Android 开发中 用Banner实现无限(自动)轮播

先添加依赖:(右上角有个Gradle,进入之后找到自己要添加依赖的项目 --> 右击鼠标,选择 Open Gradle config,在compile'' 队列中添加下一行代码,点击刷新即可).
compile 'com.youth.banner:banner:1.4.9'

MainActivity:

package com.example.zhangkai20171023;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private Banner banner;
    private List list=new ArrayList();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        list.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");
        list.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");
        list.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");
        list.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");
        list.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");

        banner = (Banner) findViewById(R.id.banner);


        banner.setImageLoader(new MImage());

        banner.setImages(list);

        banner.isAutoPlay(true);

        banner.setDelayTime(3000);

        banner.start();


        //banner.stopAutoPlay();

    }
}
MImage:

package com.example.zhangkai20171023;

import android.content.Context;
import android.widget.ImageView;


import com.youth.banner.loader.ImageLoader;



/**
 * Created by Administrator on 2017/10/23 0023.
 */

public class MImage  extends ImageLoader {

    public void displayImage(Context context, Object path, ImageView imageView) {

        com.nostra13.universalimageloader.core.ImageLoader instance = com.nostra13.universalimageloader.core.ImageLoader.getInstance();
        instance.displayImage((String) path, imageView);
    }

}
MyApplication:

package com.example.zhangkai20171023;

import android.app.Application;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

/**
 * Created by Administrator on 2017/10/23 0023.
 */

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        ImageLoaderConfiguration  imageLoaderConfiguration=new ImageLoaderConfiguration.Builder(this).build();

        ImageLoader.getInstance().init(imageLoaderConfiguration);

    }
}
activity_main.xml:




    



AndroidManifest.xml:



    
	
    
	
        
            
                

                
            
        
    








你可能感兴趣的:(Android 开发中 用Banner实现无限(自动)轮播)