1. 程式人生 > >Banner無限輪播

Banner無限輪播

Banner無限輪播

1、匯入依賴
compile ‘com.youth.banner:banner:1.4.9’
2、新增許可權:

一、佈局檔案:

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

<android.support.constraint.ConstraintLayout 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”
tools:context=".MainActivity">

<com.youth.banner.Banner
    android:id="@+id/banner"
    android:layout_width="match_parent"
    android:layout_height="150dp"></com.youth.banner.Banner>

</android.support.constraint.ConstraintLayout>

二、建立一個類得到ImageLoader
1、匯入依賴:
compile ‘com.nostra13.universalimageloader:universal-image-loader:1.9.5’
2、具體程式碼
package gj.com.day13_2_banner;

	import android.app.Application;
	
	import com.nostra13.universalimageloader.core.ImageLoader;
	import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
	
	public class Mapp extends Application {
	    @Override
	    public void onCreate() {
	        super.onCreate();
	        ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this).build();
	        ImageLoader instance = ImageLoader.getInstance();
	        instance.init(configuration);
	    }
	}

建立完後要在清單檔案註冊 application下

3、Banner的MainActivity中具體程式碼

package gj.com.day13_2_banner;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;

import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    //網路圖片路徑
    String urlBitmap = "http://img.my.csdn.net/uploads/201407/26/1406383265_8550.jpg";
    private Banner banner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化元件
        banner = findViewById(R.id.banner);

        //設定ImageLoader
        banner.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object path, ImageView imageView) {
                //獲取ImageLoader例項   com.nostra13.universalimageloader.core.ImageLoader
                com.nostra13.universalimageloader.core.ImageLoader instance =
                        com.nostra13.universalimageloader.core.ImageLoader.getInstance();
                instance.displayImage((String) path,imageView);

            }
        });

	//建立存放圖片路徑的集合
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0;i<5;i++){
            list.add(urlBitmap);
        }
        //將圖片集合放入setImages
        banner.setImages(list);
        //必須呼叫
        banner.start();
    }
}