android轮播图

android轮播图,第1张

记录学习android的时刻,最近在学导播图这一块,但是在原生android中一般用法就是用ViewPager来实现,这里因为我又上github查询了一些资料,可以通过其他大佬的依赖来写,代码如如下:

在第二条build.gradle(Module: .....)中的dependencies下添加依赖

implementation 'com.synnapps:carouselview:0.1.5'

        于是就可以在xml中使用大佬写的view了


fillColor代表当前页面选中圆圈里填充的颜色,pageColor代表圆圈里填充的颜色,

strokeColor代表了圆圈边框的颜色,strokeWidth是边框的宽,silenInterval代表是多少毫秒执行一次,还有其他的方法,indicatorMarginVertical是设置距离水平距离底部有多少;autoplay snap等等。

        代码如下

public class CarouselActivity extends AppCompatActivity {
    CarouselView carouselView;
    private int[] images = {R.drawable.animal1, R.drawable.animal2, R.drawable.animal3
            , R.drawable.animal4, R.drawable.animal5};

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.carousel_view);

        carouselView = findViewById(R.id.carouselView);

        // 给轮播图设置页面数量
        carouselView.setPageCount(images.length);

        // carouselView 的滑动监听事件
        carouselView.setImageListener(imageListener);

        // carouselView 的点击监听事件
        carouselView.setImageClickListener(new ImageClickListener() {
            @Override
            public void onClick(int position) {
                Toast.makeText(CarouselActivity.this,
                        "当前点击了第"+position+"个页面",Toast.LENGTH_SHORT).show();
            }
        });
    }

    // 将ImageListener new 出来
    ImageListener imageListener = new ImageListener() {
        @Override
        public void setImageForPosition(int position, ImageView imageView) {
            // 当前滑动的位置和相对图片
            imageView.setImageResource(images[position]);

        }
    };
}

到这里就介绍完了,如果问题请指出,可以相互学习。

如有不懂,可以查询github:sayyam/carouselview: A simple library to add carousel view in android app. (github.com)

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/langs/790009.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-05
下一篇2022-05-05

发表评论

登录后才能评论

评论列表(0条)

    保存