android BannerView实现自动轮播广告的用法(参考自github)

1、首先依赖这个包:

compile 'xyz.eraise:bannerview:1.0.5'
2、最重要的是要添加网络权限:

android:name="android.permission.INTERNET" />
3、写个实体类存放图片的url:

public class AdEntity {
   
   public String pic;
   public String url;
   
   public AdEntity(String pic, String url) {
      this.pic = pic;
      this.url = url;
   }

   public String getPic() {
      return pic;
   }

   public void setPic(String pic) {
      this.pic = pic;
   }
}
4、在activity_main.xml中:

    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/bv_ad"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:ci_width="10dp"
    app:ci_height="10dp"/>
5、在MainActivity中:

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.TypedValue;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import java.util.ArrayList;
import xyz.eraise.bannerview.BannerView;

public class MainActivity extends Activity {

    private BannerView bvAd;
    private BannerAdapterImpl _adAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initWidget();
        initData();
        initListener();
    }

    private void initWidget() {
        bvAd = (BannerView) findViewById(R.id.bv_ad);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        params.height = 500;
        bvAd.setLayoutParams(params);
    }

    private void initData() {
        // 异步加载后需要重新设置Adapter,并且调用 startAutoScroll
        _adAdapter = new BannerAdapterImpl(this);
        /* 模拟异步请求,过上几分钟再把数据添加到界面上 */
        new AsyncTask, Void, ArrayList>() {
            @Override
            protected ArrayList doInBackground(Void... params) {
                SystemClock.sleep(3000);
                ArrayList _adList = new ArrayList<>();
                _adList.add(new AdEntity("http://sh.sinaimg.cn/2011/0629/U5952P18DT20110629112508.jpg", "http://www.sj33.cn/dphoto/stsy/weiju/201105/27977_5.html"));
                _adList.add(new AdEntity("http://wenwen.soso.com/p/20100504/20100504234911-1470813934.jpg", "http://www.baidu.com"));
                _adList.add(new AdEntity("http://www.hanefu.com/images/5041502-12.jpg", "http://www.sina.com"));
                _adList.add(new AdEntity("http://p4.so.qhmsg.com/t01eadb90f083a24fd4.jpg", "http://www.sina.com"));
                _adList.add(new AdEntity("http://p3.so.qhmsg.com/t012beb8b7c9e611c88.jpg", "http://www.sina.com"));
                return _adList;
            }

            @Override
            protected void onPostExecute(ArrayList result) {
                super.onPostExecute(result);
                _adAdapter.setData(result);
                // Adapter设置到banner                bvAd.setAdapter(_adAdapter);
                bvAd.startAutoScroll();
            }
        }.execute();
    }

    private void initListener() {
        bvAd.setOnBannerClickListener(new BannerView.OnBannerClickListener() {
            @Override
            public void onClick(BannerView view, int position) {
                // 通过position获取当前被点击的广告执行相应的操作
                Intent _intent = new Intent(Intent.ACTION_VIEW, Uri.parse(_adAdapter.getItem(position).url));
                startActivity(_intent);
            }
        });
    }

    private int dip2px(int dip) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, getResources().getDisplayMetrics());
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (null != bvAd) {
            bvAd.startAutoScroll();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (null != bvAd) {
            bvAd.stopAutoScroll();
        }
    }

}
6、values.xml中:

name="BannerView">
    name="indicator_gravity">
        name="center" value="0"/>
        name="left" value="1"/>
        name="right" value="2"/>
    
    format="dimension" name="indicator_height"/>
    format="dimension" name="indicator_margin"/>
    format="dimension" name="indicator_marginLeft"/>
    format="dimension" name="indicator_marginRight"/>
    format="dimension" name="indicator_marginTop"/>
    format="dimension" name="indicator_marginBottom"/>
    format="float" name="height_ratio"/>
    format="integer" name="scroll_delay"/>

name="CircleIndicator">
    format="dimension" name="ci_width"/>
    format="dimension" name="ci_height"/>
    format="dimension" name="ci_margin"/>
    format="reference" name="ci_animator"/>
    format="reference" name="ci_animator_reverse"/>
    format="reference" name="ci_drawable"/>
    format="reference" name="ci_drawable_unselected"/>
    format="enum" name="ci_orientation">
        name="horizontal" value="0"/>
        name="vertical" value="1"/>
    
    name="ci_gravity">
        name="top" value="0x30"/>
        name="bottom" value="0x50"/>
        name="left" value="0x03"/>
        name="right" value="0x05"/>
        name="center_vertical" value="0x10"/>
        name="fill_vertical" value="0x70"/>
        name="center_horizontal" value="0x01"/>
        name="fill_horizontal" value="0x07"/>
        name="center" value="0x11"/>
        name="fill" value="0x77"/>
        name="clip_vertical" value="0x80"/>
        name="clip_horizontal" value="0x08"/>
        name="start" value="0x00800003"/>
        name="end" value="0x00800005"/>
    
7、在anim文件下创建scale_with_alpja.xml

xml version="1.0" encoding="utf-8"?>

xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@android:integer/config_shortAnimTime">

            android:propertyName="alpha"
        android:valueType="floatType"
        android:valueFrom="0.5"
        android:valueTo="1.0"/>

            android:propertyName="scaleX"
        android:valueType="floatType"
        android:valueFrom="1.0"
        android:valueTo="1.3"/>

            android:propertyName="scaleY"
        android:valueType="floatType"
        android:valueFrom="1.0"
        android:valueTo="1.3"/>
8、在drawable文件下创建white_radius.xml:

xml version="1.0" encoding="utf-8"?>

xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
            android:color="@android:color/darker_gray"/>
好了基本上是这样的实现的,祝猴子们不出bug

你可能感兴趣的:(Android)