1. 程式人生 > >android BannerView實現自動輪播廣告的用法(參考自github)

android BannerView實現自動輪播廣告的用法(參考自github)

1、首先依賴這個包:

compile 'xyz.eraise:bannerview:1.0.5'
2、最重要的是要新增網路許可權:
<uses-permission 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中:
<xyz.eraise.bannerview.BannerView
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, Void, ArrayList<AdEntity>>() { @Override protected ArrayList<AdEntity> doInBackground(Void... params) { SystemClock.sleep(3000); ArrayList<AdEntity> _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<AdEntity> result) { super.onPostExecute(result); _adAdapter.setData(result); // Adapter設定到bannerbvAd.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中:
<declare-styleable name="BannerView">
    <attr name="indicator_gravity">
        <enum name="center" value="0"/>
        <enum name="left" value="1"/>
        <enum name="right" value="2"/>
    </attr>
    <attr format="dimension" name="indicator_height"/>
    <attr format="dimension" name="indicator_margin"/>
    <attr format="dimension" name="indicator_marginLeft"/>
    <attr format="dimension" name="indicator_marginRight"/>
    <attr format="dimension" name="indicator_marginTop"/>
    <attr format="dimension" name="indicator_marginBottom"/>
    <attr format="float" name="height_ratio"/>
    <attr format="integer" name="scroll_delay"/>
</declare-styleable>
<declare-styleable name="CircleIndicator">
    <attr format="dimension" name="ci_width"/>
    <attr format="dimension" name="ci_height"/>
    <attr format="dimension" name="ci_margin"/>
    <attr format="reference" name="ci_animator"/>
    <attr format="reference" name="ci_animator_reverse"/>
    <attr format="reference" name="ci_drawable"/>
    <attr format="reference" name="ci_drawable_unselected"/>
    <attr format="enum" name="ci_orientation">
        <enum name="horizontal" value="0"/>
        <enum name="vertical" value="1"/>
    </attr>
    <attr name="ci_gravity">
        <flag name="top" value="0x30"/>
        <flag name="bottom" value="0x50"/>
        <flag name="left" value="0x03"/>
        <flag name="right" value="0x05"/>
        <flag name="center_vertical" value="0x10"/>
        <flag name="fill_vertical" value="0x70"/>
        <flag name="center_horizontal" value="0x01"/>
        <flag name="fill_horizontal" value="0x07"/>
        <flag name="center" value="0x11"/>
        <flag name="fill" value="0x77"/>
        <flag name="clip_vertical" value="0x80"/>
        <flag name="clip_horizontal" value="0x08"/>
        <flag name="start" value="0x00800003"/>
        <flag name="end" value="0x00800005"/>
    </attr>
</declare-styleable>
7、在anim檔案下建立scale_with_alpja.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- ViewPager 指示器動畫 -->
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_shortAnimTime">
    <objectAnimator
android:propertyName="alpha"
android:valueType="floatType"
android:valueFrom="0.5"
android:valueTo="1.0"/>
    <objectAnimator
android:propertyName="scaleX"
android:valueType="floatType"
android:valueFrom="1.0"
android:valueTo="1.3"/>
    <objectAnimator
android:propertyName="scaleY"
android:valueType="floatType"
android:valueFrom="1.0"
android:valueTo="1.3"/>
</set>
8、在drawable檔案下建立white_radius.xml:
<?xml version="1.0" encoding="utf-8"?>
<!-- 指示器圖片 -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
    <solid
android:color="@android:color/darker_gray"/>
</shape>
好了基本上是這樣的實現的,祝猴子們不出bug