ViewPager的索引控件

来源:互联网 发布:织梦404页面制作 编辑:程序博客网 时间:2024/06/06 18:00
/** * * ViewPager的索引 * * Created by Liu Jianping * * @date : 16/6/6. */public class IndexView extends View {    /**     * 几个圆?     */    private int count = 4;    /**     * 圆点的半径     */    private float radius = 10;    /**     *  默认的颜色     */    private int defaultColor = Color.GRAY;    /**     *  选中的颜色     */    private int selectedColor = Color.RED;    /**     * 圆点的间距     */    private float circlePadding = 20;    /**     * 第一个圆点最左侧的座标     */    private float fromX,fromY;    private Paint paint = new Paint();    /**     * 当前选中的索引     */    private int currIndex;    public IndexView(Context context) {        super(context);    }    public IndexView(Context context, AttributeSet attrs) {        super(context, attrs);        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.index_view);        count = typedArray.getInt(R.styleable.index_view_count, 4);        radius = typedArray.getFloat(R.styleable.index_view_radius, 10);        defaultColor = typedArray.getColor(R.styleable.index_view_defaultColor, Color.GRAY);        selectedColor = typedArray.getColor(R.styleable.index_view_selectedColor, Color.RED);        circlePadding =typedArray.getFloat(R.styleable.index_view_circlePadding, 20);        typedArray.recycle();    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        //计算起始点坐标        fromX = (getWidth() - radius * 2 * count - circlePadding * (count - 1)) / 2;        fromY = getHeight() / 2;        //抗锯齿        paint.setAntiAlias(true);        //画圆点        for (int i = 0; i < count; i++)        {            if (i == currIndex)            {                paint.setColor(selectedColor);                paint.setStyle(Paint.Style.FILL);            }            else            {                paint.setColor(defaultColor);                paint.setStyle(Paint.Style.STROKE);            }            canvas.drawCircle(fromX + radius + (radius * 2 + circlePadding) * i, fromY, radius , paint);        }    }    public void setCurrIndex(int index)    {        //改变当前选中的值        currIndex = index;        //重新绘制一次        invalidate();    }}
0 0
原创粉丝点击