Android 实现轮播图效果(二) 底部圆点布局实现

1、准备底部圆点的素材、利用Drawable的功能,去实现一个圆点图片的展示

drawable下新建两个Drawable resource file:dot_normal.xml和dot_select.xml

Android 实现轮播图效果(二) 底部圆点布局实现_第1张图片

dot_normal.xml



    

    

 dot_select.xml



    

    

 新建类ImageBannerFramLayout继承FrameLayout并Create constructor matching super(实现前面三个构造方法就行)

 Android 实现轮播图效果(二) 底部圆点布局实现_第2张图片

实现方法initImageBannerViewGroup()和initDotLinearlayout()并在三个构造方法中调用

    //构造方法
    public ImageBannerFramLayout(@NonNull Context context) {
        super(context);
        initImageBannerViewGroup();
        initDotLinearlayout();
    }

    public ImageBannerFramLayout(@NonNull Context context, @Nullable AttributeSet attrs){
        super(context, attrs);
        initImageBannerViewGroup();
        initDotLinearlayout();
    }

    public ImageBannerFramLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initImageBannerViewGroup();
        initDotLinearlayout();
    }

    /**
     * 初始化自定义的图片轮播核心类
     */
    private void initImageBannerViewGroup() {
        imageBannerViewGroup = new ImageBannerViewGroup(getContext());
        //设置宽度和高度
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        imageBannerViewGroup.setLayoutParams(lp);
        imageBannerViewGroup.setDotChangeListener(this);//这里将Listener传递给Framlayout
        imageBannerViewGroup.setImageBannerLister(this);
        addView(imageBannerViewGroup);

    }

    /**
     * 初始化底部圆点布局
     */
    private void initDotLinearlayout() {
        linearLayout = new LinearLayout(getContext());
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, 40);
        linearLayout.setLayoutParams(lp);

        //水平并居中
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        linearLayout.setGravity(Gravity.CENTER);

        //设置背景颜色
//        linearLayout.setBackgroundColor(Color.RED);

        addView(linearLayout);

        //将圆点放到底部
        FrameLayout.LayoutParams layoutParams = (LayoutParams) linearLayout.getLayoutParams();
        layoutParams.gravity = Gravity.BOTTOM;
        linearLayout.setLayoutParams(layoutParams);

        //设置底部圆点透明度 3.0之后使用setAlpha(),3.0之前也是使用这个方法,但调用者不同
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            linearLayout.setAlpha(0.5f);//传入float值
        } else {
            linearLayout.getBackground().setAlpha(100);//传入int值0~255
        }


    }

添加方法addDotToLinearlayout()和addBitmapToImageBannerViewGroup()并在addBitmaps()中调用

public void addBitmaps(List list) {
        for (int i = 0; i < list.size(); i++) {
            Bitmap bitmap = list.get(i);
            addBitmapToImageBannerViewGroup(bitmap);
            addDotToLinearlayout();
        }
    }

    private void addDotToLinearlayout() {
        ImageView imageView = new ImageView(getContext());
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        //设置圆点的距离
        lp.setMargins(5, 5, 5, 5);

        imageView.setLayoutParams(lp);
        imageView.setImageResource(R.drawable.dot_normal);
        linearLayout.addView(imageView);

    }

    private void addBitmapToImageBannerViewGroup(Bitmap bitmap) {
        ImageView imageView = new ImageView(getContext());

        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

        //设置宽度(设备的宽度存储在全局变量C.WIDTH中)和高度
        imageView.setLayoutParams(new ViewGroup.LayoutParams(C.WIDTH, ViewGroup.LayoutParams.WRAP_CONTENT));

        imageView.setImageBitmap(bitmap);
        imageBannerViewGroup.addView(imageView);
    }

 

 

你可能感兴趣的:(Android)