Android實(shí)現(xiàn)簡單的banner輪播圖
本文實(shí)例為大家分享了Android實(shí)現(xiàn)簡單banner輪播圖的具體代碼,供大家參考,具體內(nèi)容如下
說明:想玩一個簡單的輪播圖效果 用的第三方的框架玩一下,支持設(shè)置輪播圖多種樣式
1.效果圖

2.添加依賴
implementation 'com.youth.banner:banner:1.4.10' implementation "com.github.bumptech.glide:glide:4.6.1"
3.主界面
package com.example.myapplication34;
import android.os.Bundle;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import java.util.ArrayList;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
//界面控件
private Banner mbanner;
//輪播圖的數(shù)據(jù)
private MyImageLoader myImageLoader;
private ArrayList<Integer> photos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle bundle = getIntent().getBundleExtra("bundle");
initData();
initView();
}
//界面初始化
private void initView() {
mbanner = (Banner) findViewById(R.id.banner);
//設(shè)置輪播的樣式
mbanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR); //CIRCLE_INDICATOR
//設(shè)置圖片加載器
mbanner.setImageLoader(myImageLoader);
//設(shè)置輪播的動畫效果,里面有很多種特效,可以都看看效果。
mbanner.setBannerAnimation(Transformer.ZoomOutSlide);
//設(shè)置輪播間隔時間
mbanner.setDelayTime(3000);
//設(shè)置是否為自動輪播,默認(rèn)是true
mbanner.isAutoPlay(true);
//設(shè)置指示器的位置,小點(diǎn)點(diǎn),居中顯示
mbanner.setIndicatorGravity(BannerConfig.CENTER);
//設(shè)置圖片加載地址
mbanner.setImages(photos)
//開始調(diào)用的方法,啟動輪播圖。
.start();
}
//數(shù)據(jù)初始化
private void initData() {
myImageLoader = new MyImageLoader();
//將輪播的圖片放在photos 那里
photos = new ArrayList<Integer>();
photos.add(R.mipmap.photo2);
photos.add(R.mipmap.photo1);
photos.add(R.mipmap.photo3);
photos.add(R.mipmap.photo4);
photos.add(R.mipmap.photo5);
photos.add(R.mipmap.photo6);
}
}
4.設(shè)置圖片源數(shù)據(jù)
package com.example.myapplication34;
import android.content.Context;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.youth.banner.loader.ImageLoader;
public class MyImageLoader extends ImageLoader {
@Override
public void displayImage(Context context, Object path, ImageView imageView) {
Glide.with(context.getApplicationContext())
.load(path)
.into(imageView);
}
}
5.布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MainActivity">
<com.youth.banner.Banner
android:id="@+id/banner"
android:layout_width="match_parent"
android:layout_height="200dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
wenserver獲取天氣預(yù)報(bào)數(shù)據(jù)實(shí)例分享
wenserver獲取天氣預(yù)報(bào)數(shù)據(jù),實(shí)現(xiàn)android顯示天氣信息2013-12-12
Android編程中出現(xiàn)The connection to adb is down問題的解決方法
這篇文章主要介紹了Android編程中出現(xiàn)The connection to adb is down問題的解決方法,涉及Android進(jìn)程與服務(wù)的相關(guān)操作技巧,需要的朋友可以參考下2015-12-12
android中RecyclerView自定義分割線實(shí)現(xiàn)
本篇文章主要介紹了android中RecyclerView自定義分割線實(shí)現(xiàn),由于RecyclerView的布局方式多種多樣,所以它的分割線也根據(jù)布局的不同有所差異,本文只針對LinearLayoutManager線性布局。2017-03-03
Android 側(cè)滑抽屜菜單的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android 側(cè)滑抽屜菜單的實(shí)現(xiàn)代碼,本文通過實(shí)例圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03
Android studio gradle環(huán)境變量配置教程
這篇文章主要為大家詳細(xì)介紹了Android studio gradle環(huán)境變量配置教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05
解析Android截取手機(jī)屏幕兩種實(shí)現(xiàn)方案
這篇文章主要介紹了解析Android截取手機(jī)屏幕兩種實(shí)現(xiàn)方案,非常具有實(shí)用價值,需要的朋友可以參考下2017-04-04
Android實(shí)現(xiàn)帶有記住密碼功能的登陸界面
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)帶有記住密碼功能的登陸界面,主要采用SharedPreferences來保存用戶數(shù)據(jù),感興趣的小伙伴們可以參考一下2016-05-05

