当前位置: 首页 > 工具软件 > Themed Slider > 使用案例 >

Slider框架简单使用

平光明
2023-12-01

我们呢都会做一些广告,但是做轮播图呢比较麻烦,有现成封装好的东西,为啥不用呢?今天来整个Slider玩一下

首先导入依赖

//三级缓存的图片加载器
compile 'com.squareup.picasso:picasso:2.3.2'
//属性动画的兼容包
compile 'com.nineoldandroids:library:2.4.0'
//依赖开源的轮播大图控件
compile 'com.daimajia.slider:library:1.1.5@aar'

Activity部分

private SliderLayout sliderLayout;
private PagerIndicator indicator;
//加载图片网址
private String[]ImageUrl = {"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1529296205958&di=48d4177b37e5f8ef35fb1817006cb6d1&imgtype=0&src=http%3A%2F%2Fimg4.duitang.com%2Fuploads%2Fitem%2F201508%2F06%2F20150806122050_d2Qfh.jpeg",
"https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=3731997375,2461734094&fm=27&gp=0.jpg",
"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1529891036&di=0c9ef2484c9918be2edf6ef21d5dab96&imgtype=jpg&er=1&src=http%3A%2F%2Fimg.edu-hb.com%2FNewsImg%2F201310%2F10%2F131010190637le6ts.jpg",
"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1529296486682&di=1f61e5af3c14374684a426390b642aa6&imgtype=0&src=http%3A%2F%2Fg.hiphotos.baidu.com%2Fzhidao%2Fpic%2Fitem%2F8644ebf81a4c510f7f4b489c6859252dd52aa5ad.jpg"};
//图片名字
private String[]ImageDesc = {"迪丽热巴","杨幂","高圆圆","杨颖"};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initImageSlider();
}

/**
 * 初始化首页的商品广告条
 */
private void initImageSlider() {
    sliderLayout = (SliderLayout) findViewById(R.id.slider);
    indicator = (PagerIndicator) findViewById(R.id.custom_indicator);
    for (int i = 0; i < ImageUrl.length; i++) {
        //新建三个展示View,并且添加到SliderLayout
        TextSliderView tsv = new TextSliderView(this);
        //设置图片和名字相对应
        tsv.image(ImageUrl[i]).description(ImageDesc[i]);
        //for循环中,i不能直接设置到点击事件上,需要拿一个数据接受
        final int finalI = i;
        tsv.setOnSliderClickListener(new BaseSliderView.OnSliderClickListener() {
            @Override
            public void onSliderClick(BaseSliderView slider) {
                Toast.makeText(MainActivity.this, ImageDesc[finalI], Toast.LENGTH_SHORT).show();
            }
        });
        //控件添加View
        sliderLayout.addSlider(tsv);
    }
    //对SliderLayout进行一些自定义的配置
    sliderLayout.setCustomAnimation(new DescriptionAnimation());
    sliderLayout.setPresetTransformer(SliderLayout.Transformer.Accordion);
    //翻转时间
    sliderLayout.setDuration(2000);
    //sliderLayout.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
    sliderLayout.setCustomIndicator(indicator);
}

Xml视图部分

<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"
        app:selected_height="3dp"
        app:selected_padding_left="2dp"
        app:selected_padding_right="2dp"
        app:selected_width="16dp"
        app:shape="rect"
        app:unselected_color="#55333333"
        app:unselected_height="3dp"
        app:unselected_padding_left="2dp"
        app:unselected_padding_right="2dp"
        app:unselected_width="16dp"></com.daimajia.slider.library.Indicators.PagerIndicator>
</com.daimajia.slider.library.SliderLayout>

谢谢观看,欢迎评论,

杨幂图片网址变了,所以出不来图片,不是写的问题.......

 

 

 类似资料: