Example usage for android.graphics Canvas drawLines

List of usage examples for android.graphics Canvas drawLines

Introduction

In this page you can find the example usage for android.graphics Canvas drawLines.

Prototype

public void drawLines(@Size(multiple = 4) @NonNull float[] pts, int offset, int count, @NonNull Paint paint) 

Source Link

Document

Draw a series of lines.

Usage

From source file:cn.androidy.androiddevelopmentpatterns.interactivechart.InteractiveLineGraphView.java

/**
 * Draws the chart axes and labels onto the canvas.
 *///  w ww  .  j  a v a2s .com
private void drawAxes(Canvas canvas) {
    // Computes axis stops (in terms of numerical value and position on screen)
    int i;

    computeAxisStops(mCurrentViewport.left, mCurrentViewport.right, mContentRect.width() / mMaxLabelWidth / 2,
            mXStopsBuffer);
    computeAxisStops(mCurrentViewport.top, mCurrentViewport.bottom, mContentRect.height() / mLabelHeight / 2,
            mYStopsBuffer);

    // Avoid unnecessary allocations during drawing. Re-use allocated
    // arrays and only reallocate if the number of stops grows.
    if (mAxisXPositionsBuffer.length < mXStopsBuffer.numStops) {
        mAxisXPositionsBuffer = new float[mXStopsBuffer.numStops];
    }
    if (mAxisYPositionsBuffer.length < mYStopsBuffer.numStops) {
        mAxisYPositionsBuffer = new float[mYStopsBuffer.numStops];
    }
    if (mAxisXLinesBuffer.length < mXStopsBuffer.numStops * 4) {
        mAxisXLinesBuffer = new float[mXStopsBuffer.numStops * 4];
    }
    if (mAxisYLinesBuffer.length < mYStopsBuffer.numStops * 4) {
        mAxisYLinesBuffer = new float[mYStopsBuffer.numStops * 4];
    }

    // Compute positions
    for (i = 0; i < mXStopsBuffer.numStops; i++) {
        mAxisXPositionsBuffer[i] = getDrawX(mXStopsBuffer.stops[i]);
    }
    for (i = 0; i < mYStopsBuffer.numStops; i++) {
        mAxisYPositionsBuffer[i] = getDrawY(mYStopsBuffer.stops[i]);
    }

    // Draws grid lines using drawLines (faster than individual drawLine calls)
    for (i = 0; i < mXStopsBuffer.numStops; i++) {
        mAxisXLinesBuffer[i * 4 + 0] = (float) Math.floor(mAxisXPositionsBuffer[i]);
        mAxisXLinesBuffer[i * 4 + 1] = mContentRect.top;
        mAxisXLinesBuffer[i * 4 + 2] = (float) Math.floor(mAxisXPositionsBuffer[i]);
        mAxisXLinesBuffer[i * 4 + 3] = mContentRect.bottom;
    }
    canvas.drawLines(mAxisXLinesBuffer, 0, mXStopsBuffer.numStops * 4, mGridPaint);

    for (i = 0; i < mYStopsBuffer.numStops; i++) {
        mAxisYLinesBuffer[i * 4 + 0] = mContentRect.left;
        mAxisYLinesBuffer[i * 4 + 1] = (float) Math.floor(mAxisYPositionsBuffer[i]);
        mAxisYLinesBuffer[i * 4 + 2] = mContentRect.right;
        mAxisYLinesBuffer[i * 4 + 3] = (float) Math.floor(mAxisYPositionsBuffer[i]);
    }
    canvas.drawLines(mAxisYLinesBuffer, 0, mYStopsBuffer.numStops * 4, mGridPaint);

    // Draws X labels
    int labelOffset;
    int labelLength;
    mLabelTextPaint.setTextAlign(Paint.Align.CENTER);
    for (i = 0; i < mXStopsBuffer.numStops; i++) {
        // Do not use String.format in high-performance code such as onDraw code.
        labelLength = formatFloat(mLabelBuffer, mXStopsBuffer.stops[i], mXStopsBuffer.decimals);
        labelOffset = mLabelBuffer.length - labelLength;
        canvas.drawText(mLabelBuffer, labelOffset, labelLength, mAxisXPositionsBuffer[i],
                mContentRect.bottom + mLabelHeight + mLabelSeparation, mLabelTextPaint);
    }

    // Draws Y labels
    mLabelTextPaint.setTextAlign(Paint.Align.RIGHT);
    for (i = 0; i < mYStopsBuffer.numStops; i++) {
        // Do not use String.format in high-performance code such as onDraw code.
        labelLength = formatFloat(mLabelBuffer, mYStopsBuffer.stops[i], mYStopsBuffer.decimals);
        labelOffset = mLabelBuffer.length - labelLength;
        canvas.drawText(mLabelBuffer, labelOffset, labelLength, mContentRect.left - mLabelSeparation,
                mAxisYPositionsBuffer[i] + mLabelHeight / 2, mLabelTextPaint);
    }
}

From source file:de.tum.in.tumcampus.auxiliary.calendar.DayView.java

private void drawGridBackground(Rect r, Canvas canvas, Paint p) {
    Style savedStyle = p.getStyle();

    final float stopX = computeDayLeftPosition(mNumDays);
    float y = 0;/*from   ww  w .  j  a v  a2s.c  o m*/
    final float deltaY = mCellHeight + HOUR_GAP;
    int linesIndex = 0;
    final float startY = 0;
    final float stopY = HOUR_GAP + 24 * (mCellHeight + HOUR_GAP);
    float x = mHoursWidth;

    // Draw the inner horizontal grid lines
    p.setColor(mCalendarGridLineInnerHorizontalColor);
    p.setStrokeWidth(GRID_LINE_INNER_WIDTH);
    p.setAntiAlias(false);
    y = 0;
    linesIndex = 0;
    for (int hour = 0; hour <= 24; hour++) {
        mLines[linesIndex++] = GRID_LINE_LEFT_MARGIN;
        mLines[linesIndex++] = y;
        mLines[linesIndex++] = stopX;
        mLines[linesIndex++] = y;
        y += deltaY;
    }
    if (mCalendarGridLineInnerVerticalColor != mCalendarGridLineInnerHorizontalColor) {
        canvas.drawLines(mLines, 0, linesIndex, p);
        linesIndex = 0;
        p.setColor(mCalendarGridLineInnerVerticalColor);
    }

    // Draw the inner vertical grid lines
    for (int day = 0; day <= mNumDays; day++) {
        x = computeDayLeftPosition(day);
        mLines[linesIndex++] = x;
        mLines[linesIndex++] = startY;
        mLines[linesIndex++] = x;
        mLines[linesIndex++] = stopY;
    }
    canvas.drawLines(mLines, 0, linesIndex, p);

    // Restore the saved style.
    p.setStyle(savedStyle);
    p.setAntiAlias(true);
}