ViewPager的索引控件

/**
 *
 * 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();
    }

}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值