1分鐘實現(xiàn)網(wǎng)絡(luò)圖片輪播圖#
一.添加依賴
在build.gradle
里添加依賴.
//輪播圖
api 'com.youth.banner:banner:1.4.9' //最新版本
//網(wǎng)絡(luò)圖片加載
compile 'com.squareup.picasso:picasso:2.5.2'
二.在布局文件插入輪播圖
<!--輪播圖-->
<com.youth.banner.Banner
android:id="@+id/banner"
android:layout_width="match_parent"
android:layout_height="200dp"/>
三.邏輯代碼的添加
引入
import com.youth.banner.Banner;
private Banner mBanner;
mBanner = (Banner) findViewById(R.id.banner);
/**
* 輪播圖初始化
*/
private void initImageLoader() {
//設(shè)置圖片加載器
mBanner.setImageLoader(new GlideImageLoader());
//設(shè)置圖片集合
List<Uri> images = new ArrayList<>();
images.add(Uri.parse("http://d.hiphotos.baidu.com/image/pic/item/2fdda3cc7cd98d104a601b0a2c3fb80e7bec9050.jpg"));
images.add(Uri.parse("http://e.hiphotos.baidu.com/image/pic/item/8c1001e93901213f5480ffe659e736d12f2e955d.jpg"));
images.add(Uri.parse("http://c.hiphotos.baidu.com/image/pic/item/8694a4c27d1ed21b3c778fdda06eddc451da3f4f.jpg"));
mBanner.setImages(images);
//banner設(shè)置方法全部調(diào)用完畢時最后調(diào)用
mBanner.start();
//banner點擊事件
mBanner.setOnBannerListener(new OnBannerListener() {
@Override
public void OnBannerClick(int position) {
switch (position){
case 0:
ToastUtils.show(mContext,"跳轉(zhuǎn)到:"+0);
break;
case 1:
ToastUtils.show(mContext,"跳轉(zhuǎn)到:"+1);
break;
case 2:
ToastUtils.show(mContext,"跳轉(zhuǎn)到:"+2);
break;
}
}
});
}
四.重寫glide加載圖片方式
/**
* 創(chuàng)建者: billy
* 創(chuàng)建時間: 2018/9/3 16:52
* 描述: 重寫glide加載圖片的方式
*/
public class GlideImageLoader extends ImageLoader {
@Override
public void displayImage(Context context, Object path, ImageView imageView) {
/**
注意:
1.圖片加載器由自己選擇馍悟,這里不限制,只是提供幾種使用方法
2.返回的圖片路徑為Object類型,由于不能確定你到底使用的那種圖片加載器,
傳輸?shù)牡降氖鞘裁锤袷剑敲催@種就使用Object接收和返回,你只需要強轉(zhuǎn)成你傳輸?shù)念愋途托校? 切記不要胡亂強轉(zhuǎn)腻惠!
*/;
//Picasso 加載圖片簡單用法
Picasso.with(context).load((Uri) path).into(imageView);
}
}