Example usage for android.graphics Canvas drawLine

List of usage examples for android.graphics Canvas drawLine

Introduction

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

Prototype

public void drawLine(float startX, float startY, float stopX, float stopY, @NonNull Paint paint) 

Source Link

Document

Draw a line segment with the specified start and stop x,y coordinates, using the specified paint.

Usage

From source file:sk.gryfonnlair.priznaj.view.ui.LinePageIndicator.java

@Override
protected void onDraw(final Canvas canvas) {
    super.onDraw(canvas);

    if (mViewPager == null) {
        return;//  www. j  a  v a  2  s .c  o  m
    }
    final int count = mViewPager.getAdapter().getCount();
    if (count == 0) {
        return;
    }

    if (mCurrentPage >= count) {
        setCurrentItem(count - 1);
        return;
    }

    final float lineWidthAndGap = mLineWidth + mGapWidth;
    final float indicatorWidth = (count * lineWidthAndGap) - mGapWidth;
    final float paddingTop = getPaddingTop();
    final float paddingLeft = getPaddingLeft();
    final float paddingRight = getPaddingRight();

    final float verticalOffset = paddingTop + ((getHeight() - paddingTop - getPaddingBottom()) / 2.0f);
    float horizontalOffset = paddingLeft;
    if (mCentered) {
        horizontalOffset += ((getWidth() - paddingLeft - paddingRight) / 2.0f) - (indicatorWidth / 2.0f);
    }

    //Draw stroked circles
    for (int i = 0; i < count; i++) {
        final float dx1 = horizontalOffset + (i * lineWidthAndGap);
        final float dx2 = dx1 + mLineWidth;
        canvas.drawLine(dx1, verticalOffset, dx2, verticalOffset,
                (i == mCurrentPage) ? mPaintSelected : mPaintUnselected);
    }
}

From source file:com.viewpagerindicator.as.library.indicator.RecyclerLinePageIndicator.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if (mRecyclerView == null) {
        return;//from w  ww  . j  a v a 2 s . c o m
    }
    final int count = mRecyclerView.getAdapter().getItemCount();
    if (count == 0) {
        return;
    }

    if (mCurrentPage >= count) {
        setCurrentItem(count - 1);
        return;
    }

    final float lineWidthAndGap = mLineWidth + mGapWidth;
    final float indicatorWidth = (count * lineWidthAndGap) - mGapWidth;
    final float paddingTop = getPaddingTop();
    final float paddingLeft = getPaddingLeft();
    final float paddingRight = getPaddingRight();

    float verticalOffset = paddingTop + ((getHeight() - paddingTop - getPaddingBottom()) / 2.0f);
    float horizontalOffset = paddingLeft;
    if (mCentered) {
        horizontalOffset += ((getWidth() - paddingLeft - paddingRight) / 2.0f) - (indicatorWidth / 2.0f);
    }

    //Draw stroked circles
    for (int i = 0; i < count; i++) {
        float dx1 = horizontalOffset + (i * lineWidthAndGap);
        float dx2 = dx1 + mLineWidth;
        canvas.drawLine(dx1, verticalOffset, dx2, verticalOffset,
                (i == mCurrentPage) ? mPaintSelected : mPaintUnselected);
    }
}

From source file:com.maxleap.mall.widget.FlexibleDividerDecoration.java

@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
    int lastChildPosition = -1;
    int childCount = mShowLastDivider ? parent.getChildCount() : parent.getChildCount() - 1;
    for (int i = 0; i < childCount; i++) {
        View child = parent.getChildAt(i);
        int childPosition = parent.getChildAdapterPosition(child);

        if (childPosition < lastChildPosition) {
            // Avoid remaining divider when animation starts
            continue;
        }// w w  w  .  j  a  v a2 s  .c om
        lastChildPosition = childPosition;

        if (ViewCompat.getAlpha(child) < 1) {
            // Avoid remaining divider when animation starts
            continue;
        }

        if (mVisibilityProvider.shouldHideDivider(childPosition, parent)) {
            continue;
        }

        Rect bounds = getDividerBound(childPosition, parent, child);
        switch (mDividerType) {
        case DRAWABLE:
            Drawable drawable = mDrawableProvider.drawableProvider(childPosition, parent);
            drawable.setBounds(bounds);
            drawable.draw(c);
            break;
        case PAINT:
            mPaint = mPaintProvider.dividerPaint(childPosition, parent);
            c.drawLine(bounds.left, bounds.top, bounds.right, bounds.bottom, mPaint);
            break;
        case COLOR:
            mPaint.setColor(mColorProvider.dividerColor(childPosition, parent));
            mPaint.setStrokeWidth(mSizeProvider.dividerSize(childPosition, parent));
            c.drawLine(bounds.left, bounds.top, bounds.right, bounds.bottom, mPaint);
            break;
        }
    }
}

From source file:com.andexert.calendarlistview.library.SimpleMonthView.java

/**
 * /*w w  w.j a va  2  s.  c om*/
 * @param canvas
 */
private void drawMonthTitle(Canvas canvas) {
    StringBuilder stringBuilder = new StringBuilder(getMonthAndYearString().toLowerCase());
    stringBuilder.setCharAt(0, Character.toUpperCase(stringBuilder.charAt(0)));
    String text = stringBuilder.toString();
    Rect rect = new Rect();
    mMonthTitlePaint.getTextBounds(text, 0, text.length(), rect);
    //        int x = (mWidth + 2 * mPadding) / 2;
    int leftPadding = getResources().getDimensionPixelSize(R.dimen.month_day_left_padding);
    int x = rect.width() / 2 + leftPadding;
    //        int y = (MONTH_HEADER_SIZE - MONTH_DAY_LABEL_TEXT_SIZE) / 2 + (MONTH_LABEL_TEXT_SIZE / 3);
    int y = (MONTH_HEADER_SIZE - MONTH_DAY_LABEL_TEXT_SIZE) + (MONTH_LABEL_TEXT_SIZE / 4);
    if (mIsShowMonthDay) {
        y = MONTH_HEADER_SIZE - MONTH_DAY_LABEL_TEXT_SIZE - (MONTH_LABEL_TEXT_SIZE * 2);
    }
    canvas.drawText(text, x, y, mMonthTitlePaint);
    int linePadding = getResources().getDimensionPixelSize(R.dimen.month_day_line_padding);
    canvas.drawLine(leftPadding, y + linePadding, rect.width() + leftPadding, y + linePadding,
            mMonthNumLinePaint);
}

From source file:com.aksalj.viewpagerslideshow.LinePageIndicator.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if (mViewPager == null) {
        return;//  w w  w . j  a v a 2 s.c o  m
    }
    final int count = mViewPager.getAdapter().getCount();
    if (count == 0) {
        return;
    }

    if (mCurrentPage >= count) {
        setCurrentItem(count - 1);
        return;
    }

    if (mBoxStyle)
        mLineWidth = (getWidth() / count);

    final float lineWidthAndGap = mLineWidth + mGapWidth;
    final float indicatorWidth = (count * lineWidthAndGap) - mGapWidth;
    final float paddingTop = getPaddingTop();
    final float paddingLeft = getPaddingLeft();
    final float paddingRight = getPaddingRight();

    float verticalOffset = paddingTop + ((getHeight() - paddingTop - getPaddingBottom()) / 2.0f);
    float horizontalOffset = paddingLeft;
    if (mCentered) {
        horizontalOffset += ((getWidth() - paddingLeft - paddingRight) / 2.0f) - (indicatorWidth / 2.0f);
    }

    //Draw stroked circles
    for (int i = 0; i < count; i++) {
        float dx1 = horizontalOffset + (i * lineWidthAndGap);
        float dx2 = dx1 + mLineWidth;
        canvas.drawLine(dx1, verticalOffset, dx2, verticalOffset,
                (i == mCurrentPage) ? mPaintSelected : mPaintUnselected);
    }
}

From source file:com.dirkgassen.wator.ui.view.RollingGraphView.java

/**
 * Paints the data points of one series horizontally.
 *
 * @param c canvas to paint to/*w  w w .  ja  v a2  s  .com*/
 * @param nameBounds bounds of the label
 * @param seriesNo index of the series to paint
 * @param width width of the view
 * @param height height of the view
 * @param maxValue calculated possible maximum data value
 */
private void paintSeriesHorizontal(Canvas c, Rect nameBounds, int seriesNo, int width, int height,
        float maxValue) {
    float paddingLeft = getPaddingLeft();
    float paddingTop = getPaddingTop();
    float paddingRight = getPaddingRight();
    float paddingBottom = getPaddingBottom();

    float x = width - nameBounds.width() - paddingRight;
    float y;
    if (currentValue == -1) {
        y = height - (height - paddingBottom - paddingTop) / (seriesNames.length + 1) * (seriesNo + 1)
                + paddingTop;
    } else {
        if (dataValues == null) {
            Log.e("Wa-Tor", "NO DATA VALUES although currentValue is " + currentValue);
        } else if (dataValues[currentValue] == null) {
            Log.e("Wa-Tor", "NO SERIES DATA although currentValue is " + currentValue);
        }
        y = height - paddingBottom
                - dataValues[currentValue][seriesNo] * (height - paddingBottom - paddingTop) / maxValue;
    }
    c.drawText(seriesNames[seriesNo], x, y + nameBounds.height() / 2, seriesPaints[seriesNo]);

    float scale = seriesPaints[seriesNo].getStrokeWidth();
    x -= 6f * scale;
    c.drawLine(x, y, x + 3f * scale, y, seriesPaints[seriesNo]);
    c.drawLine(x, y, x + 1.5f * scale, y + 1.5f * scale, seriesPaints[seriesNo]);
    c.drawLine(x, y, x + 1.5f * scale, y - 1.5f * scale, seriesPaints[seriesNo]);
    if (currentValue != -1) {
        int no = currentValue;
        do {
            if (dataValues[no] == null) {
                break;
            }
            float newX = x - 1;
            float newY = height - paddingBottom
                    - dataValues[no][seriesNo] * (height - paddingBottom - paddingTop) / maxValue;
            c.drawLine(x, y, newX, newY, seriesPaints[seriesNo]);
            x = newX;
            y = newY;
            no = no == 0 ? dataValues.length - 1 : no - 1;
        } while (no != oldestValue && x > paddingLeft);
    }
}

From source file:com.freud.mrzz.views.PagerSlidingTabStrip.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (isInEditMode() || tabCount == 0) {
        return;//from  w w w  . j a  v  a 2  s .c o m
    }

    final int height = getHeight();
    // draw indicator line
    rectPaint.setColor(indicatorColor);
    Pair<Float, Float> lines = getIndicatorCoordinates();
    canvas.drawRect(lines.first + padding, height - indicatorHeight, lines.second + padding, height, rectPaint);
    // draw underline
    rectPaint.setColor(underlineColor);
    canvas.drawRect(padding, height - underlineHeight, tabsContainer.getWidth() + padding, height, rectPaint);
    // draw divider
    if (dividerWidth != 0) {
        dividerPaint.setStrokeWidth(dividerWidth);
        dividerPaint.setColor(dividerColor);
        for (int i = 0; i < tabCount - 1; i++) {
            View tab = tabsContainer.getChildAt(i);
            canvas.drawLine(tab.getRight(), dividerPadding, tab.getRight(), height - dividerPadding,
                    dividerPaint);
        }
    }
}

From source file:base.PagerSlidingTabStrip.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (isInEditMode() || tabCount == 0) {
        return;/*from w ww.j  a v a  2  s  .  c o m*/
    }

    final int height = getHeight();
    // draw indicator line
    rectPaint.setColor(indicatorColor);
    Pair<Float, Float> lines = getIndicatorCoordinates();
    canvas.drawRect(lines.first + paddingLeft, height - indicatorHeight, lines.second + paddingRight, height,
            rectPaint);
    // draw underline
    rectPaint.setColor(underlineColor);
    canvas.drawRect(paddingLeft, height - underlineHeight, tabsContainer.getWidth() + paddingRight, height,
            rectPaint);
    // draw divider
    if (dividerWidth != 0) {
        dividerPaint.setStrokeWidth(dividerWidth);
        dividerPaint.setColor(dividerColor);
        for (int i = 0; i < tabCount - 1; i++) {
            View tab = tabsContainer.getChildAt(i);
            canvas.drawLine(tab.getRight(), dividerPadding, tab.getRight(), height - dividerPadding,
                    dividerPaint);
        }
    }
}

From source file:com.tomeokin.widget.jotablayout2.JoTabLayout.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    // draw underline
    if (mUnderlineHeight > 0) {
        mUnderlinePaint.setColor(mUnderlineColor);

        // ? GRAVITY_BOTTOM
        // getMeasuredHeight  view 
        // getHeight ? android:clipToPadding="true" getHeight()

        if (mUnderlineGravity == GRAVITY_BOTTOM) {
            canvas.drawRect(getPaddingLeft(), getHeight() - mUnderlineHeight, getWidth() + getPaddingLeft(),
                    getHeight(), mUnderlinePaint);
        } else {/*from w  w w.  j  av a  2 s .  c om*/
            canvas.drawRect(getPaddingLeft(), 0, getWidth() + getPaddingLeft(), mUnderlineHeight,
                    mUnderlinePaint);
        }
    }

    // draw divider
    if (mDividerWidth > 0) {
        mDividerPaint.setStrokeWidth(mDividerWidth);
        mDividerPaint.setColor(mDividerColor);
        View tab;
        for (int i = 0; i < mTabCount - 1; i++) {
            tab = getChildAt(i);
            canvas.drawLine(getPaddingLeft() + tab.getRight(), mDividerVerticalPadding,
                    getPaddingLeft() + tab.getRight(), getHeight() - mDividerVerticalPadding, mDividerPaint);
        }
    }

    calIndicatorRect();
    mIndicatorRect.left += mIndicatorAnimOffset;
    mIndicatorRect.right = mIndicatorRect.left + (int) mIndicatorWidth;

    if (mIndicatorShape != SHAPE_NONE) {
        if (mIndicatorShape == SHAPE_TRIANGLE) {
            mTrianglePaint.setColor(mIndicatorColor);
            mTrianglePath.reset();
            mTrianglePath.moveTo(mIndicatorRect.left, getHeight());
            mTrianglePath.lineTo(mIndicatorRect.left / 2 + mIndicatorRect.right / 2,
                    getHeight() - mIndicatorHeight);
            mTrianglePath.lineTo(mIndicatorRect.right, getHeight());
            mTrianglePath.close();
            canvas.drawPath(mTrianglePath, mTrianglePaint);
        } else if (mIndicatorShape == SHAPE_LINE) {
            mIndicatorDrawable.setColor(mIndicatorColor);
            if (mIndicatorGravity == GRAVITY_BOTTOM) {
                mIndicatorDrawable.setBounds(mIndicatorRect.left, getHeight() - (int) mIndicatorHeight,
                        mIndicatorRect.right, getHeight());
            } else {
                mIndicatorDrawable.setBounds(mIndicatorRect.left, 0, mIndicatorRect.right,
                        (int) mIndicatorHeight);
            }
            mIndicatorDrawable.setCornerRadius(mIndicatorCornerRadius);
            mIndicatorDrawable.draw(canvas);
        } else if (mIndicatorShape == SHAPE_SQUARE) {
            if (mIndicatorCornerRadius <= 0 || mIndicatorCornerRadius > mIndicatorHeight / 2) {
                mIndicatorCornerRadius = mIndicatorHeight / 2;
            }
            mIndicatorRect.top = (int) ((getHeight() - mIndicatorHeight) / 2);
            mIndicatorRect.bottom = mIndicatorRect.top + (int) mIndicatorHeight;

            mIndicatorDrawable.setColor(mIndicatorColor);
            mIndicatorDrawable.setBounds(mIndicatorRect.left, mIndicatorRect.top, mIndicatorRect.right,
                    mIndicatorRect.bottom);
            mIndicatorDrawable.setCornerRadius(mIndicatorCornerRadius);
            mIndicatorDrawable.draw(canvas);
        }
    }
}

From source file:com.ashish.routofy.PagerSlidingTabStrip.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (isInEditMode() || tabCount == 0) {
        return;// w  w  w  .  ja v  a  2  s  .co  m
    }

    final int height = getHeight();
    // draw indicator line
    rectPaint.setColor(indicatorColor);
    Pair<Float, Float> lines = getIndicatorCoordinates();
    canvas.drawRect(lines.first + paddingLeft, height - indicatorHeight, lines.second + paddingLeft, height,
            rectPaint);
    // draw underline
    rectPaint.setColor(underlineColor);
    canvas.drawRect(paddingLeft, height - underlineHeight, tabsContainer.getWidth() + paddingRight, height,
            rectPaint);
    // draw divider
    if (dividerWidth != 0) {
        dividerPaint.setStrokeWidth(dividerWidth);
        dividerPaint.setColor(dividerColor);
        for (int i = 0; i < tabCount - 1; i++) {
            View tab = tabsContainer.getChildAt(i);
            canvas.drawLine(tab.getRight(), dividerPadding, tab.getRight(), height - dividerPadding,
                    dividerPaint);
        }
    }
}