android實現輪播圖(從互聯網上尋找圖片)


首先開啟網絡權限

 <uses-permission android:name="android.permission.INTERNET" /> //網絡權限
 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> //讀取SD卡

 接下來是xml文件

 <LinearLayout
        android:paddingTop="?android:attr/actionBarSize"
        android:paddingLeft="15sp"
        android:paddingRight="15sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <!--<android.support.constraint.ConstraintLayout-->
        <!--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"-->
        <!--android:layout_marginTop="500sp"-->
        <!--tools:context=".MainActivity">-->

        <com.youth.banner.Banner
            android:id="@+id/banner"
            android:layout_width="match_parent"
            android:layout_height="150dp"

            />

    <!--</android.support.constraint.ConstraintLayout>-->


    </LinearLayout>

然后是java文件

   //輪播圖
    private void initView() {

        banner = (Banner) findViewById(R.id.banner);
        //放圖片地址的集合
        list_path = new ArrayList<>();
        //放標題的集合
        list_title = new ArrayList<>();

        list_path.add("http://ww4.sinaimg.cn/large/006uZZy8jw1faic21363tj30ci08ct96.jpg");
        list_path.add("http://ww4.sinaimg.cn/large/006uZZy8jw1faic259ohaj30ci08c74r.jpg");
        list_path.add("http://ww4.sinaimg.cn/large/006uZZy8jw1faic2b16zuj30ci08cwf4.jpg");
        list_path.add("http://ww4.sinaimg.cn/large/006uZZy8jw1faic2e7vsaj30ci08cglz.jpg");
        list_title.add("好學習");
        list_title.add("天天向上");
        list_title.add("熱愛勞動");
        list_title.add("不亂搞");
        //設置內置樣式,共有六種可以點入方法內逐一體驗使用。
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);
        //設置圖片加載器,圖片加載器在下方
        banner.setImageLoader(new MyLoader());
        //設置圖片網址或地址的集合
        banner.setImages(list_path);
        //設置輪播的動畫效果,內含多種特效,可點入方法內查找后內逐一體驗
        banner.setBannerAnimation(Transformer.Default);
        //設置輪播圖的標題集合
        banner.setBannerTitles(list_title);
        //設置輪播間隔時間
        banner.setDelayTime(10000);
        //設置是否為自動輪播,默認是“是”。
        banner.isAutoPlay(true);
        //設置指示器的位置,小點點,左中右。
        banner.setIndicatorGravity(BannerConfig.CENTER)
                //以上內容都可寫成鏈式布局,這是輪播圖的監聽。比較重要。方法在下面。
                .setOnBannerListener(this)
                //必須最后調用的方法,啟動輪播圖。
                .start();

    }

    /**
     * 輪播監聽
     *
     * @param position
     */
    @Override
    public void OnBannerClick(int position) {
        Toast.makeText(this, "你點了第" + (position + 1) + "張輪播圖", Toast.LENGTH_SHORT).show();
    }

    /**
     * 網絡加載圖片
     * 使用了Glide圖片加載框架
     */
    private class MyLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            Glide.with(context.getApplicationContext())
                    .load((String) path)
                    .into(imageView);
        }
    }

最后在java文件onCreate方法中直接調用initView()
方法就好了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM