AndroidImageSlider实现广告轮播条

前言:这是以前一直用的一个开源框架,特别好用 ,推荐

效果图:

这里广告的图片,传到服务器,我自己决定费劲,就借助比如微信编译器,或者其他的平台,把图片传到服务器上,在调用url

嘿嘿,很偷懒。重新走一遍,程序的原因,是为了熟悉以前用,因为以前比较散,写博客也是为了总结自己的一个过程。这个项目我是准备一步一步去写,现在大三下,写着,最后那这个项目做毕设也是可以的嘛,算一步。

Github 网站:

https://github.com/daimajia/AndroidImageSlider

注意:建议全部的依赖项目统一使用最新的版本,因为依赖的项目之间可能也会存在依赖,如果不想出现版本冲突问题,最后全部使用最新版,解决办法就是直接在Android Studio里面搜索下载。

不过我有时懒,就不改了,如果报错 ,及改一下同一版本。

布局

<RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="150dp">

            <com.daimajia.slider.library.SliderLayout
                android:id="@+id/slider"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                >

                <com.daimajia.slider.library.Indicators.PagerIndicator
                    android:id="@+id/custom_indicator"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_centerHorizontal="true"
                    android:layout_marginBottom="10dp"
                    custom:selected_color="@color/colorPrimary"
                    custom:selected_height="3dp"
                    custom:selected_padding_left="2dp"
                    custom:selected_padding_right="2dp"
                    custom:selected_width="16dp"
                    custom:shape="rect"
                    custom:unselected_color="#55333333"
                    custom:unselected_height="3dp"
                    custom:unselected_padding_left="2dp"
                    custom:unselected_padding_right="2dp"
                    custom:unselected_width="16dp"
                    />

            </com.daimajia.slider.library.SliderLayout>
        </RelativeLayout>

但是有可能布局加载报错,但是不影响结果。

/**
     * 初始化首页广告条
     */
    private void initImageSlider(View v) {
        sliderLayout = (SliderLayout) v.findViewById(R.id.slider);
        indicator = (PagerIndicator) v.findViewById(R.id.custom_indicator);
        //准备好要显示的数据
        List<String> imageUrls = new ArrayList<>();
        final List<String> descriptions = new ArrayList<>();
        //传到服务器上
        imageUrls.add("http://pic.96weixin.com/ueditor/20190412/1555053050158909.jpg");
        imageUrls.add("http://pic.96weixin.com/ueditor/20190412/1555053050428485.jpg");
        imageUrls.add("http://pic.96weixin.com/ueditor/20190412/1555053050723139.jpg");
        descriptions.add("两学一做");
        descriptions.add("十九大精神");
        descriptions.add("思想政治工作");

        for (int i = 0; i < imageUrls.size(); i++) {
            //新建三个展示View,并且添加到SliderLayout
            TextSliderView tsv = new TextSliderView(getActivity());
            tsv.image(imageUrls.get(i)).description(descriptions.get(i));
            final int finalI = i;
            tsv.setOnSliderClickListener(new BaseSliderView.OnSliderClickListener() {
                @Override
                public void onSliderClick(BaseSliderView slider) {
                    Toast.makeText(getActivity(), descriptions.get(finalI), Toast.LENGTH_SHORT).show();
                }
            });
            sliderLayout.addSlider(tsv);
        }
        //对SliderLayout进行一些自定义的配置
        sliderLayout.setCustomAnimation(new DescriptionAnimation());
        sliderLayout.setPresetTransformer(SliderLayout.Transformer.Accordion);
        sliderLayout.setDuration(3000);
        sliderLayout.setCustomIndicator(indicator);
    }

方式有多种 我一直用这一种 ,挺好用的

代码实现,主要步骤是

·   准备好要显示的数据,包括图片和图片描述等。

·   新建若干个TextSliderView并且设置好数据以及相应的监听,最后添加到SliderLayout里面。

·   对SliderLayout进行一些个性化的设置,比如动画,自定义PagerIndicator,每一个广告的延时时间等。

·   最后别忘了在布局摧毁的时候,调用sliderLayout.stopAutoCycle();方法停止广告的轮播,以释放资源。

发布了71 篇原创文章 · 获赞 19 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_39131246/article/details/89497569
今日推荐