Banner是我们经常会遇到的一个功能,有很多是需要支持滑动是有个当前的指示器界面,一般如果为了图省事的方案可以用LinearLayout然后不断的在里面添加View,把背景通过shape来设置圆形。这种做法虽然比较简单,但是用到的控件太多,对性能并不好。为了解决这个问题通过对需求的思考,用自定义的View来做就很容易了。下面是我的写的这个View,你可以直接复制粘贴到代码中;
代码如下:
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;
/***
*@date 创建时间 2018/3/3
*@author 作者: 壹树
*@description Banner的当前索引指示器自定义View
*/
public class BannerIndicatorView extends View {
public static final int INDICATOR_STYLE_CIRCLE = 0;
public static final int INDICATOR_STYLE_RECT = 1;
/*** 指示器中的总数**/
private int cellCount = 0;
/*** 当前选中的位置**/
private int currentPosition;
/*** 小点之间的间距*/
private int cellMarginSize = 10;
/*** 指示器元素的半径(注意是半径,不是直径)**/
private int cellRadius = 10;
/****未选中的颜色*/
private int normalColor;
/*** 选中的颜色**/
private int selectedColor;
/*** 小点的样式,默认是圆形*/
<