Android 輪播圖

來源:互聯網
上載者:User

標籤:dma   檔案   架構   delay   ica   指示   onload   體驗   源檔案   

package com.example.carson_ho.android_banner;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.youth.banner.Banner;

public class MainActivity extends AppCompatActivity {
private Banner banner;
//設定圖片資源:url或本地資源
String[] images= new String[] {
"http://img.zcool.cn/community/018fdb56e1428632f875520f7b67cb.jpg",
"http://img.zcool.cn/community/01c8dc56e1428e6ac72531cbaa5f2c.jpg",
"http://img.zcool.cn/community/01fda356640b706ac725b2c8b99b08.jpg",
"http://img.zcool.cn/community/01fd2756e142716ac72531cbf8bbbf.jpg",
"http://img.zcool.cn/community/0114a856640b6d32f87545731c076a.jpg"};

//設定圖片標題:自動對應
String[] titles=new String[]{"12趁現在","嗨購5折不要停,12.12趁現在","實打實大頂頂頂頂"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


banner = (Banner) findViewById(R.id.banner);

//設定樣式,預設為:Banner.NOT_INDICATOR(不顯示指標和標題)
//可選樣式如下:
//1. Banner.CIRCLE_INDICATOR 顯示圓形指標
//2. Banner.NUM_INDICATOR 顯示數字指標
//3. Banner.NUM_INDICATOR_TITLE 顯示數字指標和標題
//4. Banner.CIRCLE_INDICATOR_TITLE 顯示圓形指標和標題
banner.setBannerStyle(Banner.CIRCLE_INDICATOR_TITLE);

//設定輪播樣式(沒有標題預設為右邊,有標題時預設左邊)
//可選樣式:
//Banner.LEFT 指標居左
//Banner.CENTER 指標置中
//Banner.RIGHT 指標居右
banner.setIndicatorGravity(Banner.CENTER);

//設定輪播要顯示的標題和圖片對應(如果不傳預設不顯示標題)
banner.setBannerTitle(titles);

//設定是否自動輪播(不設定則預設自動)
banner.isAutoPlay(true) ;

//設定輪播圖片間隔時間(不設定預設為2000)
banner.setDelayTime(5000);
//設定圖片資源:可選圖片網址/資源檔,預設用Glide載入,也可自訂圖片的載入架構
//所有設定參數方法都放在此方法之前執行
//banner.setImages(images);

//自訂圖片載入架構
banner.setImages(images, new Banner.OnLoadImageListener() {
@Override
public void OnLoadImage(ImageView view, Object url) {
System.out.println("載入中");
Glide.with(getApplicationContext()).load(url).into(view);
System.out.println("載入完");
}
});
//設定點擊事件,下標是從1開始
banner.setOnBannerClickListener(new Banner.OnBannerClickListener() {//設定點擊事件
@Override
public void OnBannerClick(View view, int position) {
Toast.makeText(getApplicationContext(), "你點擊了:" + position, Toast.LENGTH_LONG).show();
}
});

}
//如果你需要考慮更好的體驗,可以這麼操作
@Override
protected void onStart() {
super.onStart();
Log.i("--", "onStart");
banner.isAutoPlay(true);
}

@Override
protected void onStop() {
super.onStop();
Log.i("--", "onStop");
banner.isAutoPlay(false);
}
}

布局介面 activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.carson_ho.android_banner.MainActivity">

<com.youth.banner.Banner
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/banner"
android:layout_width="match_parent"
android:layout_height="300dp" />
</RelativeLayout>

app/build.gradle 檔案添加
compile ‘com.youth.banner:banner:1.1.5‘

《AndroidManifest.xml》資訊清單檔網路請求添加許可權
<uses-permission android:name="android.permission.INTERNET" />


Android 輪播圖

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.