1、使用Android循环滚动控件——ViewFlipper实现方法:https://blog.csdn.net/meixi_android/article/details/84615857
2、使用第三方module实现方法:
需要引入的module下载链接:https://pan.baidu.com/s/1sdlQefBTfkLsNO__ouwz8w
添加module方法步骤:
(1)File----->New------>Import Module找到下载的banner文件
(2)app的build.gradle下的dependencies下添加
compile project(':banner')
2、2依赖引入方法
//广告图
implementation 'com.youth.banner:banner:1.4.10'
xmlns:app="http://schemas.android.com/apk/res-auto"
<com.youth.banner.Bannerandroid:id="@+id/banner"android:layout_width="match_parent"android:layout_height="500pt"app:image_scale_type="fit_xy"app:delay_time="5000"app:indicator_height="11pt"app:indicator_width="11pt" />
图片加载工具方法:
public class GlideImageLoader extends ImageLoader {@Overridepublic void displayImage(Context context, Object path, ImageView imageView) {Glide.with(context).load(path).error(context.getResources().getDrawable(R.mipmap.img_banner)).apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.ALL)).into(imageView);}
}
mBanner.setImageLoader(new GlideImageLoader());
mBanner.setBannerAnimation(Transformer.Accordion);
mBanner.setOnBannerListener(this);
private List<String> strings;
strings = Arrays.asList(dataBean.getImgs().split(","));mBanner.setImages(strings);
mBanner.start();
如:
dependencies {compile fileTree(dir: 'libs', include: ['*.jar'])testCompile 'junit:junit:4.12'compile 'com.android.support:appcompat-v7:23+'compile 'com.android.support:recyclerview-v7:23+'compile "com.github.bumptech.glide:glide:3.7.0"compile 'com.facebook.fresco:fresco:0.12.0'compile 'com.zxy.android:recovery:0.0.8'compile project(':banner')
// compile 'com.youth.banner:banner:+'
}
(3)settings.gradle下添加‘banner’
include ':app',':banner'
.
2、activity调用banner Module
(1)实现接口
public class MainActivity extends AppCompatActivity implements OnBannerListener {
(2)实现数据添加
banner.setIndicatorGravity(BannerConfig.LEFT);//指示器位置左中右
banner.updateBannerStyle(BannerConfig.NUM_INDICATOR);//标题显示与否,指示器显示与否//简单使用
banner.setImages(App.images)//图片.setBannerTitles(App.titles)//标题.setImageLoader(new GlideImageLoader()).setOnBannerListener(this).start();
(3)接口方法
@Override
public void OnBannerClick(int position) {//item图片点击Toast.makeText(getApplicationContext(),"你点击了:"+position,Toast.LENGTH_SHORT).show();
}
2、视频图片混播banner
implementation 'com.lakehubo:hbanner:1.0.8'
<com.lake.banner.HBannerandroid:id="@+id/banner"android:layout_width="match_parent"android:layout_height="match_parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent" />
private HBanner banner;
private void init() {banner.setBannerAnimation(Transformer.Default)//换场方式.setBannerStyle(BannerStyle.CIRCLE_INDICATOR_TITLE)//指示器模式.setCache(true)//可以不用设置,默认为true.setCachePath(getExternalFilesDir(Environment.DIRECTORY_MOVIES).getAbsolutePath() + File.separator + "hbanner").setVideoGravity(VideoGravityType.FULL_SCREEN)//视频布局方式.setImageGravity(ImageGravityType.FIT_XY)//图片布局方式.setPageBackgroundColor(Color.TRANSPARENT)//设置背景.setShowTitle(false)//是否显示标题.setViewPagerIsScroll(true)//是否支持手滑.start();List<ViewItemBean> list2 = new ArrayList<>();Uri path1 = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.default1);
// Uri path2 = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.default2);String path2 = "http://gzlxcdg.app.xiaozhuschool.com/public/uploads/video/20200504/fa530b8ae53f8d2e7698ea0fb22d22d5.mp4";list2.add(new ViewItemBean(VIDEO, "标题1", path1, 15 * 1000));list2.add(new ViewItemBean(VIDEO, "标题2", path2, 18 * 1000));list2.add(new ViewItemBean(IMAGE, "标题3", R.mipmap.b2, 2 * 1000));banner.update(list2);}
图文混播banner方法二
依赖
//glide implementation 'com.github.bumptech.glide:glide:4.9.0'
//图文混播 implementation 'com.github.licheedev:AdPlayer:1.0.2'
<com.licheedev.adplayer.AdViewandroid:id="@+id/ad"android:layout_width="match_parent"android:layout_height="300dp"android:background="#000000"app:ad_background_color="#000000"app:ad_enable_image_anim="true"app:ad_enable_video_anim="true"app:ad_image_scale_type="fitXY"android:layout_below="@+id/banner"android:isScrollContainer="true"app:ad_video_aspect_ratio="ratio_match_parent" />
AdView ad;private AdPlayer<AdData> mAdPlayer;
ad = (AdView) findViewById(R.id.ad); mAdPlayer = new AdPlayer<>(ad);
String urlll = "https://gzlxcdg.https.xiaozhuschool.com/public/uploads/imgs/20200306/b23d77407c27feb9ffdc578158784589.png"; String vds = "https://gzlxcdg.https.xiaozhuschool.com/public/uploads/video/20200504/fa530b8ae53f8d2e7698ea0fb22d22d5.mp4"; String vds2 = "https://gzlxcdg.https.xiaozhuschool.com/public/uploads/imgs/20200411/6329153ceb93a706fe62abdec9cbbe57.jpeg"; List<String> urls = new ArrayList<>(); urls.add(urlll); urls.add(vds); urls.add(vds2); mAdPlayer.setNewData(UrlAdData.convert(urls));
@Override public void onResume() {super.onResume();mAdPlayer.resume(); }@Override public void onPause() {super.onPause();mAdPlayer.pause(); }@Override public void onDestroy() {mAdPlayer.release();super.onDestroy(); }