Banner框架

Banner是一个框架,此框架是用于实现在Android中,ViewPager的图片无限轮播功能。

在使用Banner框架时我们需要添加它的远程依赖:compile ‘com.youth.banner:banner:1.4.9’

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

1、添加依赖
(1)、点击代码编辑页面右边的Grable;然后选择要添加远程依赖的项目右键选择第一项(如下图)

图一
(2)、点击完成后会跳到下图编辑页面,在相应位置添加Banner的远程依赖的代码
图二
(3)、添加完后点击下图标记的图标同步一下
这里写图片描述
2、在xml布局文件里引用Banner框架

<?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.demo2.MainActivity">

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

</RelativeLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

3、在MainActivity里的具体使用方法

——–Banner框架的使用,1、初始化一个ImageLoder 2、图片路径组成的集合 3、开启轮播————-

package com.example.demo2;

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

import com.example.demo2.Bean.Bean;
import com.example.demo2.Util.MyTask;
import com.google.gson.Gson;
import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    //http://api.expoon.com/AppNews/getNewsList/type/1/p/1
    List<Bean.DataBean> list=new ArrayList<>();
    //图片路径集合
    List<String> list1=new ArrayList<>();
    private Banner banner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //加载布局
        setContentView(R.layout.activity_main);
        //找控件
        banner = (Banner) findViewById(R.id.ban);
        //利用Util包请求数据
        MyTask myTask=new MyTask(new MyTask.Icallbacks() {
            @Override
            public void updateUiByjson(String jsonstr) {
                Gson gson=new Gson();
                Bean bean = gson.fromJson(jsonstr, Bean.class);
                list=bean.getData();
                Log.i("nxx",list.toString());
                //将请求到的全部数据中的图片路径通过遍历数组添加到存储图片路径的集合
                for(int i=0;i<list.size();i++){
                    String url=list.get(i).getPic_url();
                    list1.add(url);
                }
                //测试是否添加成功
                Log.i("nxx",list1.toString());
                //为Banner设置参数(LmageLoader对象)
                banner.setImageLoader(new MyImageLoader());
                //为Banner设置实现轮播的图片路径的集合
                banner.setImages(list1);
                //开启轮播
                banner.start();
            }
        });

        myTask.execute("http://api.expoon.com/AppNews/getNewsList/type/1/p/1");

    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58

4、ImageLoder类的使用(直接复制即可)

(1)、用于初始化ImageLoder

package com.example.demo2;

import android.app.Application;
import android.graphics.Bitmap;

import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.io.File;

/**
 * author:Created by WangZhiQiang on 2017/10/18.
 */

public class MApp extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        /*ImageLoaderConfiguration imageLoaderConfiguration = new ImageLoaderConfiguration.Builder(this).build();
        ImageLoader.getInstance().init(imageLoaderConfiguration);*/
        //storage/sdcard/data/<package>/cache
        //sd/mycache/
        File cacheDir=this.getExternalCacheDir();//自定义缓存路径

        ImageLoaderConfiguration configuration=new ImageLoaderConfiguration.Builder(this)
                .memoryCache(new LruMemoryCache(2 * 1024 * 1024))//设置内存缓存区大小
                .memoryCacheSize(10 * 1024 * 1024)//设置缓存区大小
                .memoryCacheExtraOptions(520,520)//缓存图片最大的宽度与高度 px
                .diskCacheSize(50*1024*1024)//设置sd卡缓存的空间大小
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())//sd卡缓存图片的命名 使用md5加密方式
                .diskCache(new UnlimitedDiscCache(cacheDir))//自定义sd卡的缓存路径
                .diskCacheFileCount(100)//缓存文件的最大数量
                .writeDebugLogs()//写入日志
                .threadPoolSize(3)//线程池
                .build();
        //对imageLoader进行初使化

        ImageLoader.getInstance().init(configuration);

    }
    public final static DisplayImageOptions getDefaultDisplayOption() {
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)//进行内存缓存
                .cacheOnDisk(true)//进行sd卡缓存
                .showImageOnLoading(R.drawable.ic_stub) // 设置正在下载中的图片
                .showImageForEmptyUri(R.drawable.ic_empty) //没有请求地址时
                .showImageOnFail(R.drawable.ic_error)//下载错误时
                .bitmapConfig(Bitmap.Config.RGB_565).build();

        //设置图片质量build(); // 创建配置过得DisplayImageOption对象
        return options;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59

(2)、用于Banner轮播使用的ImageLoder

package com.example.demo2;

import android.content.Context;
import android.widget.ImageView;

import com.youth.banner.loader.ImageLoader;

/**
 * author:Created by WangZhiQiang on 2017/10/18.
 */

public class MyImageLoader extends ImageLoader {
    //此方法用于为Viewpager上展示ImagerView(图片)的控件设置图片
    @Override
    public void displayImage(Context context, Object path, ImageView imageView) {
        //得到ImageLoader的实例
        com.nostra13.universalimageloader.core.ImageLoader instance = com.nostra13.universalimageloader.core.ImageLoader.getInstance();
        //通过ImageLoader设置图片
        instance.displayImage((String) path, imageView);
        ////////////////////////////////////////////////////////////////////////
        ////////////通过Glide加载效果要好一些不会出现宽度不足现象////////////////////
        /////////Glide依赖compile'com.github.bumptech.glide:glide:3.7.0'////////
        ///////////////////////////////////////////////////////////////////////
        ---------Glide.with(context).load(path).into(imageView);-------------------
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

5、通过数据请求工具类请求网络数据
————-工具类在上一篇博客——————–
6、Banner的图片点击事件

banner.setOnBannerListener(new OnBannerListener() {
@Override
public void OnBannerClick(int position) {
Toast.makeText(MainActivity.this, "您正在点击图片" + position, Toast.LENGTH_SHORT).show();
}
});


/http://blog.csdn.net/codefarmercxy/article/details/64125394/

Banner banner = (Banner) findViewById(R.id.banner);
    //设置banner样式
    banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE);
    //设置图片加载器
    banner.setImageLoader(new GlideImageLoader());
    //设置图片集合
    banner.setImages(images);
    //设置banner动画效果
    banner.setBannerAnimation(Transformer.DepthPage);
    //设置标题集合(当banner样式有显示title时)
    banner.setBannerTitles(titles);
    //设置自动轮播,默认为true
    banner.isAutoPlay(true);
    //设置轮播时间
    banner.setDelayTime(1500);
    //设置指示器位置(当banner模式中有指示器时)
    banner.setIndicatorGravity(BannerConfig.CENTER);
    //banner设置方法全部调用完毕时最后调用
    banner.start();
    //结束轮播
    banner.stopAutoPlay();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大伟伟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值