1. 程式人生 > >Banner 網路圖片無限輪播 ImageLoader

Banner 網路圖片無限輪播 ImageLoader

1---新增依賴  

compile 'com.youth.banner:banner:1.4.9' //Banner最新版本

    compile  'com.nostra13.universalimageloader:universal-image-loader:1.9.5'//imageloader依賴
 2--- 新增許可權
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
3---添加布局
<com.youth.banner.Banner
 xmlns:app="http://schemas.android.com/apk/res-auto"
  android:id="@+id/banner"
  android:layout_width="match_parent"

  android:layout_height="200dp" />

4--Mapp程式碼(之後再許可權頁面加一個  android:name=".mapp"

importandroid.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.getInstance().init(configuration); } }

主頁面:

package com.example.myapplication;

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

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

import 
java.util.ArrayList; import java.util.Arrays; public class MainActivity extends AppCompatActivity { private Banner banner; String[] urls = {"http://img.my.csdn.net/uploads/201407/26/1406383265_8550.jpg", "https://lh4.googleusercontent.com/--dq8niRp7W4/URquVgmXvgI/AAAAAAAAAbs/-gnuLQfNnBA/s160-c/A%252520Song%252520of%252520Ice%252520and%252520Fire.jpg", "https://lh5.googleusercontent.com/-7qZeDtRKFKc/URquWZT1gOI/AAAAAAAAAbs/hqWgteyNXsg/s160-c/Another%252520Rockaway%252520Sunset.jpg", "https://lh3.googleusercontent.com/--L0Km39l5J8/URquXHGcdNI/AAAAAAAAAbs/3ZrSJNrSomQ/s160-c/Antelope%252520Butte.jpg", "https://lh6.googleusercontent.com/-8HO-4vIFnlw/URquZnsFgtI/AAAAAAAAAbs/WT8jViTF7vw/s160-c/Antelope%252520Hallway.jpg", "https://lh4.googleusercontent.com/-WIuWgVcU3Qw/URqubRVcj4I/AAAAAAAAAbs/YvbwgGjwdIQ/s160-c/Antelope%252520Walls.jpg"}; ArrayList<String> list = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Banner banner = (Banner) findViewById(R.id.banner); banner.setImageLoader((ImageLoaderInterface) new BannerImageLoader()); banner.setImages(Arrays.asList(urls)); banner.start(); } }

BannerImageLoader頁面:
package com.example.myapplication;
import android.content.Context;
import android.widget.ImageView;
import com.youth.banner.loader.ImageLoader;
public class BannerImageLoader extends ImageLoader {
    @Override
public void displayImage(Context context, Object obj, ImageView imageView) {

        com.nostra13.universalimageloader.core.ImageLoader imageLoaderInstance = com.nostra13.universalimageloader.core.ImageLoader.getInstance();
        imageLoaderInstance.displayImage((String) obj, imageView);

    }
}