Example usage for android.graphics Canvas drawPath

List of usage examples for android.graphics Canvas drawPath

Introduction

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

Prototype

public void drawPath(@NonNull Path path, @NonNull Paint paint) 

Source Link

Document

Draw the specified path using the specified paint.

Usage

From source file:com.lovejjfg.demo.TouchCircleView.java

public void drawTriangle(Canvas c, float startAngle, float sweepAngle) {
    if (mArrow == null) {
        mArrow = new Path();
        mArrow.setFillType(Path.FillType.EVEN_ODD);
    } else {/*www .j a  v a2  s .co  m*/
        mArrow.reset();
    }

    // Adjust the position of the triangle so that it is inset as
    // much as the arc, but also centered on the arc.
    //        float inset = (int) mStrokeInset / 2 * mArrowScale;
    float x = (float) (mRingCenterRadius * Math.cos(0) + innerRectf.centerX());
    float y = (float) (mRingCenterRadius * Math.sin(0) + innerRectf.centerY());

    // Update the path each time. This works around an issue in SKIA
    // where concatenating a rotation matrix to a scale matrix
    // ignored a starting negative rotation. This appears to have
    // been fixed as of API 21.
    mArrow.moveTo(0, 0);
    mArrow.lineTo(ARROW_WIDTH * mArrowScale, 0);
    mArrow.lineTo((ARROW_WIDTH * mArrowScale / 2), (ARROW_HEIGHT * mArrowScale));
    mArrow.offset(x, y);
    mArrow.close();
    // draw a triangle
    c.rotate(startAngle + sweepAngle, innerRectf.centerX(), innerRectf.centerY());
    c.drawPath(mArrow, innerPaint);

}

From source file:com.android.screenspeak.contextmenu.RadialMenuView.java

private void drawWedge(Canvas canvas, float center, int i, RadialMenu menu, float degrees) {
    final float offset = mSubMenu != null ? mSubMenuOffset : mRootMenuOffset;

    final RadialMenuItem wedge = menu.getItem(i);
    final String title = wedge.getTitle().toString();
    final float rotation = ((degrees * i) + offset);
    final boolean selected = wedge.equals(mFocusedItem);

    // Apply the appropriate color filters.
    if (wedge.hasSubMenu()) {
        mPaint.setColorFilter(mSubMenuFilter);
    } else {// w ww  . j ava  2  s. c o  m
        mPaint.setColorFilter(null);
    }

    wedge.offset = rotation;

    mTempMatrix.reset();
    mTempMatrix.setRotate(rotation, center, center);
    mTempMatrix.postTranslate((mCenter.x - center), (mCenter.y - center));
    canvas.setMatrix(mTempMatrix);

    mPaint.setStyle(Style.FILL);
    mPaint.setColor(selected ? mSelectionColor : mOuterFillColor);
    mPaint.setShadowLayer(mShadowRadius, 0, 0, (selected ? mSelectionShadowColor : mTextShadowColor));
    canvas.drawPath(mCachedOuterPath, mPaint);
    mPaint.setShadowLayer(0, 0, 0, 0);

    mPaint.setStyle(Style.FILL);
    mPaint.setColor(selected ? mSelectionTextFillColor : mTextFillColor);
    mPaint.setTextAlign(Align.CENTER);
    mPaint.setTextSize(mTextSize);
    mPaint.setShadowLayer(mTextShadowRadius, 0, 0, mTextShadowColor);

    final String renderText = getEllipsizedText(mPaint, title, mCachedOuterPathWidth);

    // Orient text differently depending on the angle.
    if ((rotation < 90) || (rotation > 270)) {
        canvas.drawTextOnPath(renderText, mCachedOuterPathReverse, 0, (2 * mTextSize), mPaint);
    } else {
        canvas.drawTextOnPath(renderText, mCachedOuterPath, 0, -mTextSize, mPaint);
    }

    mPaint.setShadowLayer(0, 0, 0, 0);
    mPaint.setColorFilter(null);
}

From source file:com.android.settings.widget.DotsPageIndicator.java

private void drawUnselected(Canvas canvas) {
    combinedUnselectedPath.rewind();/*from  ww w.ja  va2s.c  o m*/

    // draw any settled, revealing or joining dots
    for (int page = 0; page < pageCount; page++) {
        int nextXIndex = page == pageCount - 1 ? page : page + 1;
        // todo Path.op should be supported in KitKat but causes the app to hang for Nexus 5.
        // For now disabling for all pre-L devices.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Path unselectedPath = getUnselectedPath(page, dotCenterX[page], dotCenterX[nextXIndex],
                    page == pageCount - 1 ? INVALID_FRACTION : joiningFractions[page],
                    dotRevealFractions[page]);
            combinedUnselectedPath.op(unselectedPath, Path.Op.UNION);
        } else {
            canvas.drawCircle(dotCenterX[page], dotCenterY, dotRadius, unselectedPaint);
        }
    }

    // draw any retreating joins
    if (retreatingJoinX1 != INVALID_FRACTION) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            combinedUnselectedPath.op(getRetreatingJoinPath(), Path.Op.UNION);
        }
    }
    canvas.drawPath(combinedUnselectedPath, unselectedPaint);
}

From source file:com.morninz.ninepinview.widget.NinePINView.java

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

    // draw circles around center points and connection line
    int drawnCount = mDrawnPoints.size();
    for (int j = 0; j < drawnCount; j++) {
        Point p1 = mDrawnPoints.get(j);
        canvas.drawCircle(p1.x, p1.y, mCircleRadius, mCirclePaint);
        if (j + 1 < drawnCount) {
            Point p2 = mDrawnPoints.get(j + 1);
            canvas.drawCircle(p2.x, p2.y, mCircleRadius, mCirclePaint);
            canvas.drawLine(p1.x, p1.y, p2.x, p2.y, mLinePaint);
            if (mWillDrawWrongTriangle) {
                // compute the wrong triangle's direction of this point.
                float angle = 0.f;
                if (p2.y == p1.y) {// x-axis
                    angle = p2.x > p1.x ? 0.f : 180.f;
                } else if (p2.x == p1.x) { // y-axis
                    angle = p2.y > p1.y ? 90.f : -90.f;
                } else {// in quadrants
                    double tanA = ((double) p2.y - (double) p1.y) / ((double) p2.x - (double) p1.x);
                    // in 1 or 4 quadrant
                    angle = (float) (Math.atan(tanA) * 180 / Math.PI);
                    // in 2 or 3 quadrant
                    if (p2.x < p1.x) {
                        angle += 180.f;/*from w  w  w .j a  v a2 s .  c om*/
                    }
                }
                Log.d(TAG, "angle " + angle);
                canvas.save();
                canvas.rotate(angle, p1.x, p1.y);
                canvas.drawPath(mWrongPaths[p1.index], mWrongPaint);
                canvas.restore();
            }
        }
    }

    // draw extra connection line
    if (mLastDrawnPoint != null) {
        canvas.drawLine(mLastDrawnPoint.x, mLastDrawnPoint.y, mCurrX, mCurrY, mLinePaint);
    }

    // draw 9 center points
    for (int i = 0; i < POINT_COUNT; i++) {
        Point p = mCenterPoints[i];
        canvas.drawCircle(p.x, p.y, mPointSize, mPointPaint);
    }
}

From source file:com.googlecode.eyesfree.widget.RadialMenuView.java

private void drawWedge(Canvas canvas, int width, int height, float center, int i, RadialMenu menu,
        float degrees) {
    final float offset = mSubMenu != null ? mSubMenuOffset : mRootMenuOffset;

    final RadialMenuItem wedge = menu.getItem(i);
    final String title = wedge.getTitle().toString();
    final float rotation = ((degrees * i) + offset);
    final boolean selected = wedge.equals(mFocusedItem);

    // Apply the appropriate color filters.
    if (wedge.hasSubMenu()) {
        mPaint.setColorFilter(mSubMenuFilter);
    } else {/*from   w  w w.  j  a va2s  . c om*/
        mPaint.setColorFilter(null);
    }

    wedge.offset = rotation;

    mTempMatrix.reset();
    mTempMatrix.setRotate(rotation, center, center);
    mTempMatrix.postTranslate((mCenter.x - center), (mCenter.y - center));
    canvas.setMatrix(mTempMatrix);

    mPaint.setStyle(Style.FILL);
    mPaint.setColor(selected ? mSelectionColor : mOuterFillColor);
    mPaint.setShadowLayer(mShadowRadius, 0, 0, (selected ? mSelectionShadowColor : mTextShadowColor));
    canvas.drawPath(mCachedOuterPath, mPaint);
    mPaint.setShadowLayer(0, 0, 0, 0);

    mPaint.setStyle(Style.FILL);
    mPaint.setColor(selected ? mSelectionTextFillColor : mTextFillColor);
    mPaint.setTextAlign(Align.CENTER);
    mPaint.setTextSize(mTextSize);
    mPaint.setShadowLayer(mTextShadowRadius, 0, 0, mTextShadowColor);

    final String renderText = getEllipsizedText(mPaint, title, mCachedOuterPathWidth);

    // Orient text differently depending on the angle.
    if ((rotation < 90) || (rotation > 270)) {
        canvas.drawTextOnPath(renderText, mCachedOuterPathReverse, 0, (2 * mTextSize), mPaint);
    } else {
        canvas.drawTextOnPath(renderText, mCachedOuterPath, 0, -mTextSize, mPaint);
    }

    mPaint.setShadowLayer(0, 0, 0, 0);
    mPaint.setColorFilter(null);
}

From source file:com.irccloud.android.DrawerArrowDrawable.java

@Override
public void draw(Canvas canvas) {
    Rect bounds = getBounds();/*from  w w w. j a v a2  s  .c o  m*/
    final boolean isRtl = isLayoutRtl();
    // Interpolated widths of arrow bars
    final float arrowSize = lerp(mBarSize, mTopBottomArrowSize, mProgress);
    final float middleBarSize = lerp(mBarSize, mMiddleArrowSize, mProgress);
    // Interpolated size of middle bar
    final float middleBarCut = lerp(0, mBarThickness / 2, mProgress);
    // The rotation of the top and bottom bars (that make the arrow head)
    final float rotation = lerp(0, ARROW_HEAD_ANGLE, mProgress);

    // The whole canvas rotates as the transition happens
    final float canvasRotate = lerp(isRtl ? 0 : -180, isRtl ? 180 : 0, mProgress);
    final float topBottomBarOffset = lerp(mBarGap + mBarThickness, 0, mProgress);
    mPath.rewind();

    final float arrowEdge = -middleBarSize / 2;
    // draw middle bar
    mPath.moveTo(arrowEdge + middleBarCut, 0);
    mPath.rLineTo(middleBarSize - middleBarCut, 0);

    final float arrowWidth = Math.round(arrowSize * Math.cos(rotation));
    final float arrowHeight = Math.round(arrowSize * Math.sin(rotation));

    // top bar
    mPath.moveTo(arrowEdge, topBottomBarOffset);
    mPath.rLineTo(arrowWidth, arrowHeight);

    // bottom bar
    mPath.moveTo(arrowEdge, -topBottomBarOffset);
    mPath.rLineTo(arrowWidth, -arrowHeight);
    mPath.moveTo(0, 0);
    mPath.close();

    canvas.save();
    // Rotate the whole canvas if spinning, if not, rotate it 180 to get
    // the arrow pointing the other way for RTL.
    if (mSpin) {
        canvas.rotate(canvasRotate * ((mVerticalMirror ^ isRtl) ? -1 : 1), bounds.centerX(), bounds.centerY());
    } else if (isRtl) {
        canvas.rotate(180, bounds.centerX(), bounds.centerY());
    }
    canvas.translate(bounds.centerX(), bounds.centerY());
    canvas.drawPath(mPath, mPaint);

    canvas.restore();
}

From source file:org.telegram.ui.Cells.SharedLinkCell.java

@Override
protected void onDraw(Canvas canvas) {
    if (titleLayout != null) {
        canvas.save();/*from w  ww  .  jav a2  s  .  co m*/
        canvas.translate(AndroidUtilities.dp(LocaleController.isRTL ? 8 : AndroidUtilities.leftBaseline),
                titleY);
        titleLayout.draw(canvas);
        canvas.restore();
    }

    if (descriptionLayout != null) {
        descriptionTextPaint.setColor(TEXT_COLOR);
        canvas.save();
        canvas.translate(AndroidUtilities.dp(LocaleController.isRTL ? 8 : AndroidUtilities.leftBaseline),
                descriptionY);
        descriptionLayout.draw(canvas);
        canvas.restore();
    }

    if (descriptionLayout2 != null) {
        descriptionTextPaint.setColor(TEXT_COLOR);
        canvas.save();
        canvas.translate(AndroidUtilities.dp(LocaleController.isRTL ? 8 : AndroidUtilities.leftBaseline),
                description2Y);
        descriptionLayout2.draw(canvas);
        canvas.restore();
    }

    if (!linkLayout.isEmpty()) {
        descriptionTextPaint.setColor(Theme.MSG_LINK_TEXT_COLOR);
        int offset = 0;
        for (int a = 0; a < linkLayout.size(); a++) {
            StaticLayout layout = linkLayout.get(a);
            if (layout.getLineCount() > 0) {
                canvas.save();
                canvas.translate(
                        AndroidUtilities.dp(LocaleController.isRTL ? 8 : AndroidUtilities.leftBaseline),
                        linkY + offset);
                if (pressedLink == a) {
                    canvas.drawPath(urlPath, urlPaint);
                }
                layout.draw(canvas);
                canvas.restore();
                offset += layout.getLineBottom(layout.getLineCount() - 1);
            }
        }
    }

    letterDrawable.draw(canvas);
    if (drawLinkImageView) {
        linkImageView.draw(canvas);
    }

    if (needDivider) {
        if (LocaleController.isRTL) {
            canvas.drawLine(0, getMeasuredHeight() - 1,
                    getMeasuredWidth() - AndroidUtilities.dp(AndroidUtilities.leftBaseline),
                    getMeasuredHeight() - 1, paint);
        } else {
            canvas.drawLine(AndroidUtilities.dp(AndroidUtilities.leftBaseline), getMeasuredHeight() - 1,
                    getMeasuredWidth(), getMeasuredHeight() - 1, paint);
        }
    }
}

From source file:com.lovejjfg.demo.TouchCircleView.java

private void drawCirclePath(Canvas canvas, float distance) {
    boolean isDrawPath = distance != 0;
    if (isDrawPath) {
        if (distance > 0) {
            p0.y -= changeDy;//from   w  w w.  j  av a2  s  . c om
            p11.y = p1.y = p0.y;//??
            // +
            p5.y += distance;
            p7.y = p6.y = p5.y;
            // +
            p8.x += changeDy / 2;
            p10.x = p9.x = p8.x;

            //? -
            p2.x -= changeDy / 2;
            p3.x = p4.x = p2.x;
        } else {
            //
            p0.y += distance;
            p11.y = p1.y = p0.y;//??
            // +
            p5.y += changeDy;
            p7.y = p6.y = p5.y;
            // +
            p8.x -= changeDy / 2;
            p10.x = p9.x = p8.x;

            //? -
            p2.x += changeDy / 2;
            p3.x = p4.x = p2.x;
        }
    }
    path.reset();
    path.moveTo(p0.x, p0.y);
    path.cubicTo(p1.x, p1.y, p2.x, p2.y, p3.x, p3.y);
    path.cubicTo(p4.x, p4.y, p5.x, p5.y, p6.x, p6.y);
    path.cubicTo(p7.x, p7.y, p8.x, p8.y, p9.x, p9.y);
    path.cubicTo(p10.x, p10.y, p11.x, p11.y, p0.x, p0.y);
    canvas.drawPath(path, mCurrentPaint);
}

From source file:com.knightweng.android.takemehome.presentation.view.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);

    // default: line below current tab
    View currentTab = tabsContainer.getChildAt(currentPosition);
    float lineLeft = currentTab.getLeft();
    float lineRight = currentTab.getRight();

    // if there is an offset, start interpolating left and right coordinates
    // between current and next tab
    if (currentPositionOffset > 0f && currentPosition < tabCount - 1) {

        View nextTab = tabsContainer.getChildAt(currentPosition + 1);
        final float nextTabLeft = nextTab.getLeft();
        final float nextTabRight = nextTab.getRight();

        lineLeft = (currentPositionOffset * nextTabLeft + (1f - currentPositionOffset) * lineLeft);
        lineRight = (currentPositionOffset * nextTabRight + (1f - currentPositionOffset) * lineRight);
    }

    canvas.drawRect(lineLeft, height - indicatorHeight, lineRight, height, rectPaint);

    // Draw the triangle in middle
    path.reset();
    path.moveTo(lineLeft + (lineRight - lineLeft) / 2, height - indicatorHeight - 12);
    path.lineTo(lineLeft + (lineRight - lineLeft) / 2 - 10, height - indicatorHeight);
    path.lineTo(lineLeft + (lineRight - lineLeft) / 2 + 10, height - indicatorHeight);
    path.lineTo(lineLeft + (lineRight - lineLeft) / 2, height - indicatorHeight - 12);
    path.close();
    canvas.drawPath(path, rectPaint);

    rectPaint.setColor(underlineColor);
    canvas.drawRect(0, height - underlineHeight, tabsContainer.getWidth(), height, rectPaint);

    // draw divider

    // 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:ingbank.com.tr.happybanking.common.ui.controls.PagerSlidingTabStrip.java

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

    if (isInEditMode() || tabCount == 0) {
        return;/*  ww  w .j a v a2 s.  c om*/
    }

    final int height = getHeight();

    // draw indicator line

    rectPaint.setColor(indicatorColor);

    // default: line below current tab
    View currentTab = tabsContainer.getChildAt(currentPosition);
    float lineLeft = currentTab.getLeft();
    float lineRight = currentTab.getRight();

    // if there is an offset, start interpolating left and right coordinates between current and next tab
    if (currentPositionOffset > 0f && currentPosition < tabCount - 1) {

        View nextTab = tabsContainer.getChildAt(currentPosition + 1);
        final float nextTabLeft = nextTab.getLeft();
        final float nextTabRight = nextTab.getRight();

        lineLeft = (currentPositionOffset * nextTabLeft + (1f - currentPositionOffset) * lineLeft);
        lineRight = (currentPositionOffset * nextTabRight + (1f - currentPositionOffset) * lineRight);
    }

    //canvas.drawRect(lineLeft, height - indicatorHeight, lineRight, height, rectPaint);

    if (triangleIndicator) {
        Rect r = new Rect();
        int left = (int) lineLeft + (int) (((lineRight - lineLeft) / 2) - (indicatorHeight / 2)) - 20;
        int top = height - indicatorHeight;
        int right = (int) left + indicatorHeight + 30;
        int bottom = height;
        r.set(left, top, right, bottom);
        Path path = getEquilateralTriangle(r);
        canvas.drawPath(path, rectPaint);
    } else {
        canvas.drawRect(lineLeft, height - indicatorHeight, lineRight, height, rectPaint);
    }

    // draw underline

    rectPaint.setColor(underlineColor);
    canvas.drawRect(0, height - underlineHeight, tabsContainer.getWidth(), height, rectPaint);

    // draw divider

    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);
    }
}