图片轮播,小圆点可支持居中或居右设置,颜色可自定义,效果如下:
关键代码如下:
public class BannerView<T> extends RelativeLayout {
private boolean isRight;
private int point_size;
private int point_bg;
private int nomalColor;
private int selectColor;
private ViewPager vp;
private RelativeLayout pointLayout;
private List<T> pictureList;
private T t;
//是否在没有图片的情况下占位
private boolean isNeedStaticLocaltion;
//是否轮播
private boolean isPlay = false;
private static int MARGIN = ScreenUtil.dip2px(BaseApplication.getBaseApplication(), 5);
private List<MyCilcleView> myCilcleViews;
private List<ImageView> imageViews;
private UserControal<T> userControal;
private int FLAG = 1;
private int currentPosition = 0;
private float currentX = 0;
private float currentY = 0;
private float MINE_XY = 20;
public static final int MARGIN_LEFtT = ScreenUtil.dip2px(BaseApplication.getBaseApplication(), 19);
private int mDefaultImg = R.mipmap.ic_launcher;
//宽高比例
private float proportion = 0.6F;
private Handler hander = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (null != msg) {
if (msg.what == FLAG) {
hander.removeMessages(FLAG);
vp.setCurrentItem(currentPosition + 1);
hander.sendEmptyMessageDelayed(FLAG, 3000);
}
}
}
};
public BannerView(Context context) {
this(context, null);
}
public BannerView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public BannerView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.BannerView);
point_size = (int) array.getDimension(R.styleable.BannerView_point_size, 15);
//point_bg = array.getColor(R.styleable.BannerView_point_bg, 0x00000000);
point_bg = Color.parseColor("#00000000");
isRight = array.getBoolean(R.styleable.BannerView_is_right, false);
isPlay = array.getBoolean(R.styleable.BannerView_is_play, true);
isNeedStaticLocaltion = array.getBoolean(R.styleable.BannerView_is_static_location, true);
nomalColor = array.getColor(R.styleable.BannerView_point_color_unselect, Color.WHITE);
selectColor = array.getColor(R.styleable.BannerView_point_color_select, Color.BLACK);
proportion = array.getFloat(R.styleable.BannerView_banner_proportion, 0.6f);
array.recycle();
}
/**
* 添加ViewPager和指向器布局
*/
private void init() {
vp = new ViewPager(getContext());
pointLayout = new RelativeLayout(getContext());
addView(vp);
addView(pointLayout);
LayoutParams lpVp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp.rightMargin = 38;
pointLayout.setBackgroundColor(point_bg);