/**
 * Difficult the first time, easy the second.
 *
 * @author xulin
 * @description:
 * @date :2019/10/12
 */
public class LxTimeLineView extends AppCompatImageView {
    private int itemCount;//Item总数量
    private int itemPosition;//Item位置
    private Paint paint;
    private int paintColor = 0xff08a1ef;

    private float width;
    private float height;
    private float centerX;
    private float centerY;
    private int startY;
    private int moveXY;

    public int getItemCount() {
        return itemCount;
    }

    public void setItemCount(int itemCount) {
        this.itemCount = itemCount;
        invalidate();
    }

    public int getItemPosition() {
        return itemPosition;
    }

    public void setItemPosition(int itemPosition) {
        this.itemPosition = itemPosition;
        invalidate();
    }

    public LxTimeLineView(Context context) {
        this(context, null);
    }

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

    public LxTimeLineView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        paint = new Paint();
        paint.setAntiAlias(true);
        //线宽
        paint.setStrokeWidth(DimenUtils.dp2px(getContext(), 2));

    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = getMeasuredWidth();
        height = getMeasuredHeight();
        centerY = getMeasuredHeight() / 2;
        centerX = getMeasuredWidth() / 2;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        paint.setColor(paintColor);
        startY = DimenUtils.dp2px(getContext(), getPaddingTop());
        moveXY = DimenUtils.dp2px(getContext(), 6);
//开始位置
        if (itemPosition == 0) {
            canvas.drawLine(centerX, startY, centerX, centerY + (height - centerY), paint);
            canvas.drawCircle(centerX, startY, DimenUtils.dp2px(getContext(), 10), paint);
            paint.setColor(Color.WHITE);
//绘制对号图形
            canvas.drawLine(centerX / 2, startY, centerX, startY + moveXY, paint);
            canvas.drawLine(centerX, startY + moveXY, centerX + moveXY, centerY / 2, paint);
//结束位置
        } else if (itemPosition == (itemCount - 1)) {
            canvas.drawLine(centerX, 0, centerX, centerY, paint);
            canvas.drawCircle(centerX, startY, DimenUtils.dp2px(getContext(), 10), paint);
            paint.setColor(Color.WHITE);
            canvas.drawCircle(centerX, startY, DimenUtils.dp2px(getContext(), 8), paint);
//中间部分
        } else {
            canvas.drawLine(centerX, 0, centerX, height, paint);
            canvas.drawCircle(centerX, startY, DimenUtils.dp2px(getContext(), 10), paint);
            paint.setColor(Color.WHITE);
            canvas.drawCircle(centerX, startY, DimenUtils.dp2px(getContext(), 8), paint);
        }
        super.onDraw(canvas);

    }


}
10-13 14:38