Android自定义view七时间轴(二)---横向的图表时间轴

横向的时间轴图表

public class CharterXLabels extends CharterLabelsBase {
public CharterXLabels(Context context) {
    this(context, null);
}

public CharterXLabels(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public CharterXLabels(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public CharterXLabels(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
}

@Override
public void draw(Canvas canvas) {
    super.draw(canvas);

    if (values == null || values.length == 0) {
        return;
    }

    final int valuesLength = values.length;

    final float height = getMeasuredHeight();
    final float width  = getMeasuredWidth();

    final float gap = stickyEdges ? width / (valuesLength - 1) : width / valuesLength;

    int visibilityPatternPos = -1;

    for (int i = 0; i < valuesLength; i++) {
        if (visibilityPatternPos + 1 >= visibilityPattern.length) {
            visibilityPatternPos = 0;
        } else {
            visibilityPatternPos++;
        }

        if (visibilityPattern[visibilityPatternPos]) {
            Rect textBounds = new Rect();
            paintLabel.getTextBounds(values[i]!=null?values[i]:" ", 0, values[i]!=null? values[i].length() : 1, textBounds);
            int   textHeight = 2 * textBounds.bottom - textBounds.top;
            float textWidth  = textBounds.right;

            float x;
            float y;

            switch (verticalGravity) {
                case VERTICAL_GRAVITY_TOP:
                    y = 0;
                    break;

                case VERTICAL_GRAVITY_BOTTOM:
                    y = height - textHeight;
                    break;

                default:
                    // VERTICAL_GRAVITY_CENTER
                    y = (height + textHeight) / 2;
                    break;
            }

            if (stickyEdges) {
                if (i == 0) {
                    x = 0;
                } else if (i == valuesLength - 1) {
                    x = width - textWidth;
                } else {
                    x = gap * (i - 1) + gap - (textWidth / 2);
                }
                canvas.drawText(values[i], x, y, paintLabel);
            } else {
                x = gap * i + (gap / 2) - (textWidth / 2);
                canvas.drawText(values[i], x, y, paintLabel);
            }
        }
    }
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值