美文网首页
带小点点图片轮播显示

带小点点图片轮播显示

作者: 小沫沫儿 | 来源:发表于2019-04-11 16:34 被阅读0次

在开发中,时常要用到图片轮播的功能,现记录一下:
首先自定义ImageLayout

public class ImagesShowLayout extends RelativeLayout {
    private Context mContext;
    private ViewPager mVpImages;
    private LinearLayout mLlImagesHintGroup;
    private ImageView[] mHintViews; 点点数组
    private List<ImageView> mImageViewsList = new ArrayList<>();//ImageView列表
    private List<String> mImageUrlList = new ArrayList<>(); //图片资源
    private PagerAdapter pagerAdapter;

    public ImagesShowLayout(Context context) {
        super(context);
        this.mContext = context;
        initView();
    }

    public ImagesShowLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        initView();
    }

    public ImagesShowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initView();
    }

    public void setImagesList(List<String> imagesList) {
        mImageUrlList.addAll(imagesList);
        initInvitationImages();
        initInvitationHintImages();
        mVpImages.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return mImageUrlList.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                container.addView(mImageViewsList.get(position));
                return mImageViewsList.get(position);
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView(mImageViewsList.get(position));
            }
        });

        initListener();
    }

    private void initView() {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context
                .LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.view_images_show_layout, this);
        mVpImages = (ViewPager) findViewById(R.id.vp_images);
        mLlImagesHintGroup = (LinearLayout) findViewById(R.id.ll_images_hint_group);
    }

    private void initListener() {
        //图片滑动监听
        mVpImages.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                //设置下方点点图片
                mHintViews[position].setBackgroundResource(R.drawable.shape_round_rec_white_3dp);
                for (int i = 0; i < mHintViews.length; i++) {
                    if (position != i) {
                        mHintViews[i].setBackgroundResource(R.drawable.shape_round_white_6dp);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /**
     * 初始化图片
     */
    private void initInvitationImages() {
        LinearLayout.LayoutParams mLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        mLayoutParams.setMargins(0, 0, 0, 0);//setMargins(left, top, right, bottom);
        for (String imgUrl : mImageUrlList) {
            ImageView imageView = new ImageView(mContext);
            imageView.setScaleType(ImageView.ScaleType.CENTER);
            Glide.with(mContext).load(imgUrl).asBitmap()
                    .centerCrop().into(imageView);//
//            imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT
//                    , ViewGroup.LayoutParams.WRAP_CONTENT));
            imageView.setLayoutParams(mLayoutParams);
            mImageViewsList.add(imageView);
        }
    }

    /**
     * 动态生成图片底部圆点
     */
    private void initInvitationHintImages() {
        mHintViews = new ImageView[mImageUrlList.size()];
        //动态生成ImageView,实现底部圆点(圆点数量根据左右滑动View而确定)
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(10, 0, 0, 0);//setMargins(left, top, right, bottom);
        for (int i = 0; i < mImageUrlList.size(); i++) {
            ImageView hintView = new ImageView(mContext);
            hintView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT
                    , ViewGroup.LayoutParams.WRAP_CONTENT));
            hintView.setLayoutParams(layoutParams);
            //imageView.setPadding(10, 0, 10 , 0);
            mHintViews[i] = hintView;
            if (i == 0) {
                // 默认选中第一张图片
                mHintViews[i].setBackgroundResource(R.drawable.shape_round_rec_white_3dp);
            } else {
                mHintViews[i].setBackgroundResource(R.drawable.shape_round_white_6dp);
            }
            mLlImagesHintGroup.addView(mHintViews[i]);
        }
    }
}

shape_round_rec_white_3dpx.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#ffffffff" />
    <corners android:radius="3dp" />
    <size android:width="13dp"
          android:height="6dp"/>
</shape>

view_images_show_layout.xml布局如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="@dimen/xxx">
    <android.support.v4.view.ViewPager
        android:id="@+id/vp_images"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    </android.support.v4.view.ViewPager>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="13dp"
        android:gravity="center_horizontal|center_vertical">
    </LinearLayout>
</RelativeLayout>

相关文章

  • 带小点点图片轮播显示

    在开发中,时常要用到图片轮播的功能,现记录一下:首先自定义ImageLayout shape_round_rec_...

  • h5学习笔记之图片轮播

    需求: 五张尺寸为700*400的图片 实现自动轮播 时间三秒 显示轮播指示索引 点击索引显示对应图片 增加左右...

  • 27.jquery 实战-轮播

    代码 1.实现如下轮播效果 要求:渐变轮播,图片淡入淡出轮播会自动循环像左向右点击会展示前/后图片底部显示轮播当前...

  • 原生Js的三个demo

    轮播图(左右轮播) 1.实现功能:  - 鼠标不在图片上方时进行自动轮播,并且箭头不会显示,当鼠标放在图片上方时停...

  • 轮播图

    ios美女小同事写了一个可以支持显示一张图片,两张图片左右轮播,三张以及三张以上图片无限轮播的轮子,支持加载本地图...

  • 如何编写一个轮播

    轮播DEMO 效果 编写一个轮播的思路 1.在页面上写一个div,该div用于显示所需轮播的所有图片,先让所有图片...

  • IOS11遇到的问题总结

    1、SDCyclyImage轮播图片不显示前端图片存储位置不对,SSL证书无效,那么IOS端显示图片的时候使用SD...

  • 2019-03-19

    完美解决轮播图 swiper 图片自适应宽高居中显示

  • Android实现轮播图点击图片放大效果

    最近项目中需要实现轮播图显示商品图片,当用户点击商品图片的时候,需要图片放大显示,当然用户还能进行多张图片的滑动切...

  • Android实现轮播图点击图片放大效果

    最近项目中需要实现轮播图显示商品图片,当用户点击商品图片的时候,需要图片放大显示,当然用户还能进行多张图片的滑动切...

网友评论

      本文标题:带小点点图片轮播显示

      本文链接:https://www.haomeiwen.com/subject/qkxxwqtx.html