Banner的使用

xiaoxiao2021-02-28  3

banner和VirePager一样,可以实现图片轮播的效果

创建工程,在包下的build.gradle下加入

compile 'com.youth.banner:banner:1.4.9'

主布局

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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="com.example.bannertext.MainActivity"> <com.youth.banner.Banner android:layout_width="match_parent" android:layout_height="300dp" android:id="@+id/banner" ></com.youth.banner.Banner> </RelativeLayout>

bean类

Mimage

package com.example.bannertext; import android.content.Context; import android.widget.ImageView; import com.youth.banner.loader.ImageLoader; /** * Created by ass on 2017/10/23. */ public class MImage extends ImageLoader { @Override 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.bannertext; import android.app.Application; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; /** * Created by ass on 2017/10/23. */ public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); ImageLoaderConfiguration imageLoaderConfiguration=new ImageLoaderConfiguration.Builder(this).build(); ImageLoader.getInstance().init(imageLoaderConfiguration); } }

MainActivity

package com.example.bannertext; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import com.youth.banner.Banner; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private List<String> list=new ArrayList<String>(); private Banner banner; private UtilsBean bean; @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://img.zcool.cn/community/015c7d574b9f8f6ac72525aee98351.jpg"); list.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505151956771&di=0eb6f306991d24b67a13ceb336f80102&imgtype=0&src=http://i0.hdslb.com/bfs/archive/00613def3f1beb7a35ae136341be2b589bc46a2d.jpg_320x200.jpg"); list.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505151847685&di=c7a4b5d08ec43fa629bcb690039a7629&imgtype=0&src=http://attimg.dospy.com/img/day_080625/20080625_2e91a10c444877e88827vri2ZKdGMvQo.jpg"); list.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505151825129&di=70bf74b87d8a15cb91a2d79f15ed0eaf&imgtype=0&src=http://attimg.dospy.com/img/day_081016/20081016_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://attimg.dospy.com/img/day_090113/20090113_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(); } }

转载请注明原文地址: https://www.6miu.com/read-2000216.html

最新回复(0)