Android实现Banner的过程代码(二)

/**

  • 自己定义的一个类 封装指示器的功能
  • 1.最外层是线性布局 所以继承于LinearLayout
  • 2.重写构造方法
  • -----更改构造方法 依次 访问参数多的那个,初始化代码在最后一个写
  • ----实现功能 -> 功能在哪里写
  •            -创建控件 就默认有了(构造方法)
    
  •           -用户设置 (对应的set方法)
    
  •            -数据源  (接口里面
    
  • 自定义控件容易犯的错误
  • -调用属性的时候还没有值,值是后面再赋值的(时机不对)
  • 解决方式
  • 1.如果创建控件时就能确定的值,放在构造方法里面
  • 2.自定义属性在xml里面配置
    */
    目录结构:
    在这里插入图片描述

MainActivity.java的代码:

PagerController.java的代码:

package com.example.grouppager;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

public class PagerController extends LinearLayout {

    private int numberOfPagers;
    private float padding; //设置间距
    public int normalResource;
    public int selectedResource;
    //private int normalImageResource;
    //private int selectedIamgeResource;

    //当使用java代码创建控件时,用这个构造方法


    public PagerController(Context context, int normalRes, int selectRes, float padding) {
        super(context, null);

        //保存外部传来的数据
        normalResource = normalRes;
        selectedResource = selectRes;
        this.padding = padding;
        setOrientation(LinearLayout.HORIZONTAL);
    }

    public PagerController(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PagerController(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOrientation(LinearLayout.HORIZONTAL);
    }

    public float getPadding() {
        return padding;
    }

    public void setPadding(float padding) {
        this.padding = padding;
    }

    public int getNumberOfPagers() {
        return numberOfPagers;
    }

    public void setNumberOfPagers(int numberOfPagers) {
        this.numberOfPagers = numberOfPagers;
        for (int i = 0; i < numberOfPagers; i++) {
            //getContext 是指这个界面
            //创建控件
            ImageView dotView = new ImageView(getContext());
            //创建布局参数
            LayoutParams params = new LayoutParams
                    (ViewGroup.LayoutParams.WRAP_CONTENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT);
            //设置资源
            if (i == 0) {
                dotView.setImageResource(selectedResource);
            } else {
                dotView.setImageResource(normalResource);
                params.leftMargin = (int) padding;
            }

            //垂直居中
            params.gravity = Gravity.CENTER_VERTICAL;
            //添加控件
            addView(dotView, params);
        }
    }
    }

dot_gray_shape.xml的代码:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="#666666"/>
    <size android:width="20dp" android:height="20dp"/>
**dot_red_shape.xml的代码:**
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="#ff0000"/>
    <size android:width="30dp" android:height="30dp"/>
</shape>

activity_main.xml的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:id="@+id/rl_container">
</RelativeLayout>

效果图:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值