Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 開源組件推薦:用 LoopView 輕鬆實現廣告輪播

編輯:關於 Android 開發

Android 開源組件推薦:用 LoopView 輕鬆實現廣告輪播

一、組件介紹

幾乎每款 App 的信息列表頂部都有自動輪轉的廣告圖。用 ViewPager 雖然能實現,但編碼較為繁瑣。這裡推薦開源控件 LoopView——一個強大的輪播大圖組件,提供了豐富的配置方法,足以滿足各種應用場景。

二、環境配置

若項目使用 Gradle 構建,只需在 build.gradle 的 dependencies 中加入一行:

compile 'com.kevin:loopview:1.0.4'

三、如何使用

1. 在 layout.xml 中配置 LoopView

在佈局文件中加入 <com.kevin.loopview.AdLoopView>:

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    xmlns:kevin="https://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.kevin.loopview.AdLoopView
        android:id="@+id/adloop_act_adloopview"
        android:layout_width="match_parent"
        android:layout_height="192dp"
        kevin:loop_interval="5000"
        kevin:loop_dotMargin="5dp"
        kevin:loop_autoLoop="true"
        kevin:loop_dotSelector="@drawable/ad_dots_selector"
        kevin:loop_layout="@layout/ad_loopview_layout">
    </com.kevin.loopview.AdLoopView>
</RelativeLayout>

2. 在 Activity 中添加代碼:

public class AdLoopActivity extends Activity implements BaseLoopAdapter.OnItemClickListener {

    AdLoopView mLoopView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_adloopview);
        initViews();
        initEvents();
    }

    private void initViews() {
        mLoopView = (AdLoopView) this.findViewById(R.id.adloop_act_adloopview);
        initRotateView();
    }

    /**
     * 初始化 LoopView
     */
    private void initRotateView() {
        // 設置自定義佈局
        // mLoopView.setLoopLayout(R.layout.ad_loopview_layout);

        // 設置數據
        String json = LocalFileUtils.getStringFormAsset(this, "loopview_date.json");
        LoopData loopData = JsonTool.toBean(json, LoopData.class);
        if (null != loopData) {
            mLoopView.refreshData(loopData);
        }

        // 設置頁面切換過度事件
        mLoopView.setScrollDuration(2000);

        // 設置頁面切換時間間隔
        mLoopView.setInterval(3000);
    }

    /**
     * 初始化事件
     */
    private void initEvents() {
        mLoopView.setOnClickListener(this);
    }

    @Override
    public void onItemClick(PagerAdapter parent, View view, int position, int realPosition) {
        LoopData loopData = mLoopView.getLoopData();
        String url = loopData.items.get(position).link;
        Intent intent = new Intent();
        intent.setData(Uri.parse(url));
        intent.setAction(Intent.ACTION_VIEW);
        startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}

3. LocalFileUtils 的主要方法(讀取 Asset 文本)

public class LocalFileUtils {
    /**
     * 獲取 Asset 下文本內容
     */
    public static String getStringFormAsset(Context context, String str) {
        BufferedReader in = null;
        try {
            in = new BufferedReader(new InputStreamReader(context.getAssets().open(str)));
            String line;
            StringBuilder buffer = new StringBuilder();
            while ((line = in.readLine()) != null) {
                buffer.append(line).append('\n');
            }
            return buffer.toString();
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        } finally {
            if (in != null) {
                try {
                    in.close();
                    in = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

四、LoopView 主要方法速查

// 設置 ViewPager 頁面切換時間
mLoopView.setScrollDuration(1000);
// 設置輪轉時間間隔
mLoopView.setInterval(3000);
// 以集合方式初始化數據
mLoopView.setLoopViewPager(List<Map<String, String>> data);
// 以 JSON 方式初始化數據
mLoopView.setLoopViewPager(String jsonData);
// 以數據實體方式初始化數據
mLoopView.setLoopViewPager(LoopData rotateData);
// 以集合方式刷新數據
mLoopView.refreshData(final List<Map<String, String>> data);
// 以數據實體方式刷新數據
mLoopView.refreshData(LoopData loopData);
// 以 JSON 方式刷新數據
mLoopView.refreshData(String jsonData);
// 獲取配置的輪播數據
mLoopView.getLoopData();
// 開始自動輪轉
mLoopView.startAutoLoop();
// 延遲指定時間後自動輪轉
mLoopView.startAutoLoop(long delayTimeInMills);
// 停止自動輪轉
mLoopView.stopAutoLoop();
// 設置自定義佈局
mLoopView.setLoopLayout(int layoutResId);

借助 LoopView,幾行代碼就能搞定App頂部的廣告輪播,省去大量重複造輪子的時間。

作者:傑瑞教育
出處:https://www.cnblogs.com/jerehedu/

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved