Java Code Examples for android.graphics.Canvas#clipPath()

The following examples show how to use android.graphics.Canvas#clipPath() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FlowingMenuLayout.java    From Cybernet-VPN with GNU General Public License v3.0 7 votes vote down vote up
@Override
protected void dispatchDraw(Canvas canvas) {

    width = getWidth();
    height = getHeight();
    mClipPath.reset();
    if (position == ElasticDrawer.Position.LEFT) {
        drawLeftMenu();
    } else {
        drawRightMenu();
    }
    canvas.save();
    canvas.drawPath(mClipPath, mPaint);
    canvas.clipPath(mClipPath, Region.Op.REPLACE);
    super.dispatchDraw(canvas);
    canvas.restore();
}
 
Example 2
Source File: LineRadarRenderer.java    From android-kline with Apache License 2.0 6 votes vote down vote up
/**
 * Draws the provided path in filled mode with the provided drawable.
 *
 * @param c
 * @param filledPath
 * @param drawable
 */
protected void drawFilledPath(Canvas c, Path filledPath, Drawable drawable) {

    if (clipPathSupported()) {

        int save = c.save();
        c.clipPath(filledPath);

        drawable.setBounds((int) mViewPortHandler.contentLeft(),
                (int) mViewPortHandler.contentTop(),
                (int) mViewPortHandler.contentRight(),
                (int) mViewPortHandler.contentBottom());
        drawable.draw(c);

        c.restoreToCount(save);
    } else {
        throw new RuntimeException("Fill-drawables not (yet) supported below API level 18, " +
                "this code was run on API level " + Utils.getSDKInt() + ".");
    }
}
 
Example 3
Source File: CropView.java    From ImagePicker with Apache License 2.0 6 votes vote down vote up
@Override
protected void onDraw(Canvas canvas)
{
    super.onDraw(canvas);

    if (mCropRect == null)
        return;

    path.reset();
    path.addRect(mCropRect.left, mCropRect.top, mCropRect.right, mCropRect.bottom, Path.Direction.CW);

    if (isClipPathSupported(canvas))
    {
        getDrawingRect(viewDrawingRect);
        canvas.clipPath(path, android.graphics.Region.Op.DIFFERENCE);
        canvas.drawRect(viewDrawingRect, outsidePaint);
    } else
    {
        drawOutsideFallback(canvas);
    }

    canvas.drawPath(path, outlinePaint);
}
 
Example 4
Source File: MaterialRippleLayout.java    From material-ripple with Apache License 2.0 6 votes vote down vote up
@Override
public void draw(Canvas canvas) {
    final boolean positionChanged = adapterPositionChanged();
    if (rippleOverlay) {
        if (!positionChanged) {
            rippleBackground.draw(canvas);
        }
        super.draw(canvas);
        if (!positionChanged) {
            if (rippleRoundedCorners != 0) {
                Path clipPath = new Path();
                RectF rect = new RectF(0, 0, canvas.getWidth(), canvas.getHeight());
                clipPath.addRoundRect(rect, rippleRoundedCorners, rippleRoundedCorners, Path.Direction.CW);
                canvas.clipPath(clipPath);
            }
            canvas.drawCircle(currentCoords.x, currentCoords.y, radius, paint);
        }
    } else {
        if (!positionChanged) {
            rippleBackground.draw(canvas);
            canvas.drawCircle(currentCoords.x, currentCoords.y, radius, paint);
        }
        super.draw(canvas);
    }
}
 
Example 5
Source File: SimulationPageAnim.java    From MyBookshelf with GNU General Public License v3.0 6 votes vote down vote up
private void drawCurrentPageArea(Canvas canvas, Bitmap bitmap) {
    mPath0.reset();
    mPath0.moveTo(mBezierStart1.x, mBezierStart1.y);
    mPath0.quadTo(mBezierControl1.x, mBezierControl1.y, mBezierEnd1.x, mBezierEnd1.y);
    mPath0.lineTo(mTouchX, mTouchY);
    mPath0.lineTo(mBezierEnd2.x, mBezierEnd2.y);
    mPath0.quadTo(mBezierControl2.x, mBezierControl2.y, mBezierStart2.x, mBezierStart2.y);
    mPath0.lineTo(mCornerX, mCornerY);
    mPath0.close();

    canvas.save();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        canvas.clipOutPath(mPath0);
    } else {
        canvas.clipPath(mPath0, Region.Op.XOR);
    }
    canvas.drawBitmap(bitmap, 0, 0, null);
    try {
        canvas.restore();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 6
Source File: RadialTimePickerView.java    From DateTimePicker with Apache License 2.0 6 votes vote down vote up
private void drawHours(Canvas canvas, Path selectorPath, float alphaMod) {
    final int hoursAlpha = (int) (255f * (1f - mHoursToMinutes) * alphaMod + 0.5f);
    if (hoursAlpha > 0) {
        // Exclude the selector region, then draw inner/outer hours with no
        // activated states.
        canvas.save();
        canvas.clipPath(selectorPath, Region.Op.DIFFERENCE);
        drawHoursClipped(canvas, hoursAlpha, false);
        canvas.restore();

        // Intersect the selector region, then draw minutes with only
        // activated states.
        canvas.save();
        canvas.clipPath(selectorPath, Region.Op.INTERSECT);
        drawHoursClipped(canvas, hoursAlpha, true);
        canvas.restore();
    }
}
 
Example 7
Source File: BookPageView.java    From BookPage with MIT License 6 votes vote down vote up
/**
 * 绘制A区域水平翻页阴影
 * @param canvas
 */
private void drawPathAHorizontalShadow(Canvas canvas, Path pathA){
    canvas.restore();
    canvas.save();
    canvas.clipPath(pathA, Region.Op.INTERSECT);

    int maxShadowWidth = 30;//阴影矩形最大的宽度
    int left = (int) (a.x - Math.min(maxShadowWidth,(rPathAShadowDis/2)));
    int right = (int) (a.x);
    int top = 0;
    int bottom = viewHeight;
    GradientDrawable gradientDrawable = drawableHorizontalLowerRight;
    gradientDrawable.setBounds(left,top,right,bottom);

    float mDegrees = (float) Math.toDegrees(Math.atan2(f.x-a.x,f.y-h.y));
    canvas.rotate(mDegrees, a.x, a.y);
    gradientDrawable.draw(canvas);
}
 
Example 8
Source File: ReadLongPressPop.java    From MyBookshelf with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void dispatchDraw(Canvas canvas) {
    Path path = new Path();
    path.addRoundRect(new RectF(0, 0, getMeasuredWidth(), getMeasuredHeight()), DensityUtil.dp2px(getContext(), 4), DensityUtil.dp2px(getContext(), 4), Path.Direction.CW);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        canvas.clipPath(path);
    } else {
        canvas.clipPath(path, Region.Op.REPLACE);
    }

    super.dispatchDraw(canvas);
}
 
Example 9
Source File: OverlappedWidget.java    From BookReader with Apache License 2.0 5 votes vote down vote up
@Override
protected void drawCurrentPageArea(Canvas canvas) {
    mPath0.reset();

    canvas.save();
    if (actiondownX > mScreenWidth >> 1) {
        mPath0.moveTo(mScreenWidth + touch_down, 0);
        mPath0.lineTo(mScreenWidth + touch_down, mScreenHeight);
        mPath0.lineTo(mScreenWidth, mScreenHeight);
        mPath0.lineTo(mScreenWidth, 0);
        mPath0.lineTo(mScreenWidth + touch_down, 0);
        mPath0.close();
        canvas.clipPath(mPath0, Region.Op.XOR);
        canvas.drawBitmap(mCurPageBitmap, touch_down, 0, null);
    } else {
        mPath0.moveTo(touch_down, 0);
        mPath0.lineTo(touch_down, mScreenHeight);
        mPath0.lineTo(mScreenWidth, mScreenHeight);
        mPath0.lineTo(mScreenWidth, 0);
        mPath0.lineTo(touch_down, 0);
        mPath0.close();
        canvas.clipPath(mPath0);
        canvas.drawBitmap(mCurPageBitmap, touch_down, 0, null);
    }
    try {
        canvas.restore();
    } catch (Exception e) {

    }
}
 
Example 10
Source File: LineRadarRenderer.java    From Stayfit with Apache License 2.0 5 votes vote down vote up
/**
 * Draws the provided path in filled mode with the provided drawable.
 *
 * @param c
 * @param filledPath
 * @param drawable
 */
protected void drawFilledPath(Canvas c, Path filledPath, Drawable drawable) {
    c.save();
    c.clipPath(filledPath);

    drawable.setBounds((int) mViewPortHandler.contentLeft(),
            (int) mViewPortHandler.contentTop(),
            (int) mViewPortHandler.contentRight(),
            (int) mViewPortHandler.contentBottom());
    drawable.draw(c);

    c.restore();
}
 
Example 11
Source File: BookPageView.java    From CrawlerForReader with Apache License 2.0 5 votes vote down vote up
/**
 * 绘制B区域内容
 *
 * @param canvas
 * @param pathA
 */
private void drawPathBContent(Canvas canvas, Path pathA) {
    canvas.save();
    canvas.clipPath(pathA);//裁剪出A区域
    canvas.clipPath(getPathC(), Region.Op.UNION);//裁剪出A和C区域的全集
    canvas.clipPath(getPathB(), Region.Op.REVERSE_DIFFERENCE);//裁剪出B区域中不同于与AC区域的部分
    canvas.drawBitmap(pathBContentBitmap, 0, 0, null);

    drawPathBShadow(canvas);
    canvas.restore();
}
 
Example 12
Source File: LineRadarRenderer.java    From StockChart-MPAndroidChart with MIT License 5 votes vote down vote up
/**
 * Draws the provided path in filled mode with the provided color and alpha.
 * Special thanks to Angelo Suzuki (https://github.com/tinsukE) for this.
 *
 * @param c
 * @param filledPath
 * @param fillColor
 * @param fillAlpha
 */
protected void drawFilledPath(Canvas c, Path filledPath, int fillColor, int fillAlpha) {

    int color = (fillAlpha << 24) | (fillColor & 0xffffff);

    if (clipPathSupported()) {

        int save = c.save();

        c.clipPath(filledPath);

        c.drawColor(color);
        c.restoreToCount(save);
    } else {

        // save
        Paint.Style previous = mRenderPaint.getStyle();
        int previousColor = mRenderPaint.getColor();

        // set
        mRenderPaint.setStyle(Paint.Style.FILL);
        mRenderPaint.setColor(color);

        c.drawPath(filledPath, mRenderPaint);

        // restore
        mRenderPaint.setColor(previousColor);
        mRenderPaint.setStyle(previous);
    }
}
 
Example 13
Source File: WaveView.java    From BlueBoard with Apache License 2.0 5 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    mFrontPath.reset();
    mFrontPath.moveTo(mFrontPointsList.get(0).x, mFrontPointsList.get(0).y);

    mBackPath.reset();
    mBackPath.moveTo(mBackPointsList.get(0).x, mBackPointsList.get(0).y);

    // 画笔已经实现移动到初始位置
    int size = mFrontPointsList.size() - 2;
    // 在连续绘制时,起始点需要跳过,只需要控制点和结束点
    for (int i = 0; i < size; i = i + 2) {
        mFrontPath.quadTo(mFrontPointsList.get(i + 1).x, mFrontPointsList.get(i + 1).y,
                mFrontPointsList.get(i + 2).x, mFrontPointsList.get(i + 2).y);

        mBackPath.quadTo(mBackPointsList.get(i + 1).x, mBackPointsList.get(i + 1).y,
                mBackPointsList.get(i + 2).x, mBackPointsList.get(i + 2).y);
    }

    mFrontPath.lineTo(getWidth(), getHeight());
    mFrontPath.lineTo(0, getHeight());
    mFrontPath.close();

    mBackPath.lineTo(getWidth() + mWaveWidth, getHeight());
    mBackPath.lineTo(0, getHeight());
    mBackPath.close();

    canvas.clipPath(mOutBorderPath, Region.Op.INTERSECT);

    // 白色
    canvas.drawPath(mOutBorderPath, mWhitePaint);
    // 前波浪
    canvas.drawPath(mBackPath, mBackPaint);
    // 后波浪
    canvas.drawPath(mFrontPath, mFrontPaint);
    // 外边框
    canvas.drawPath(mOutBorderPath, mOutBorderPaint);
}
 
Example 14
Source File: FireworkRefreshDrawable.java    From FireworkyPullToRefresh with MIT License 5 votes vote down vote up
/**
 * Curve
 * *********************************************************************************************
 */
private void drawCurve(Canvas canvas) {
    mPath.reset();
    mPath.moveTo(0, getCurveYStart());
    mPath.lineTo(getCurveXStart(), getCurveYStart());
    mPath.quadTo(getCurveTargetPointX(), getCurveTargetPointY(), getCurveXEnd(), getCurveYEnd());
    mPath.lineTo(getCurveXEnd(), canvas.getWidth());
    mPath.lineTo(canvas.getWidth(), canvas.getHeight());
    mPath.lineTo(0, canvas.getHeight());
    mPath.close();
    canvas.clipPath(mPath, Region.Op.DIFFERENCE);
}
 
Example 15
Source File: PageWidget.java    From coolreader with MIT License 5 votes vote down vote up
private void drawNextPageAreaAndShadow(Canvas canvas, Bitmap bitmap) {
	mPath1.reset();
	mPath1.moveTo(mBezierStart1.x, mBezierStart1.y);
	mPath1.lineTo(mBeziervertex1.x, mBeziervertex1.y);
	mPath1.lineTo(mBeziervertex2.x, mBeziervertex2.y);
	mPath1.lineTo(mBezierStart2.x, mBezierStart2.y);
	mPath1.lineTo(mCornerX, mCornerY);
	mPath1.close();

	mDegrees = (float) Math.toDegrees(Math.atan2(mBezierControl1.x
			- mCornerX, mBezierControl2.y - mCornerY));
	int leftx;
	int rightx;
	GradientDrawable mBackShadowDrawable;
	if (mIsRTandLB) {
		leftx = (int) (mBezierStart1.x);
		rightx = (int) (mBezierStart1.x + mTouchToCornerDis / 4);
		mBackShadowDrawable = mBackShadowDrawableLR;
	} else {
		leftx = (int) (mBezierStart1.x - mTouchToCornerDis / 4);
		rightx = (int) mBezierStart1.x;
		mBackShadowDrawable = mBackShadowDrawableRL;
	}
	canvas.save();
	canvas.clipPath(mPath0);
	canvas.clipPath(mPath1, Region.Op.INTERSECT);
	canvas.drawBitmap(bitmap, 0, 0, null);
	canvas.rotate(mDegrees, mBezierStart1.x, mBezierStart1.y);
	mBackShadowDrawable.setBounds(leftx, (int) mBezierStart1.y, rightx,
			(int) (mMaxLength + mBezierStart1.y));
	mBackShadowDrawable.draw(canvas);
	canvas.restore();
}
 
Example 16
Source File: PinView.java    From PinView with Apache License 2.0 4 votes vote down vote up
private void drawPinView(Canvas canvas) {
    int highlightIdx = getText().length();
    for (int i = 0; i < mPinItemCount; i++) {
        boolean highlight = isFocused() && highlightIdx == i;
        mPaint.setColor(highlight ? getLineColorForState(HIGHLIGHT_STATES) : mCurLineColor);

        updateItemRectF(i);
        updateCenterPoint();

        canvas.save();
        if (mViewType == VIEW_TYPE_RECTANGLE) {
            updatePinBoxPath(i);
            canvas.clipPath(mPath);
        }
        drawItemBackground(canvas, highlight);
        canvas.restore();

        if (highlight) {
            drawCursor(canvas);
        }

        if (mViewType == VIEW_TYPE_RECTANGLE) {
            drawPinBox(canvas, i);
        } else if (mViewType == VIEW_TYPE_LINE) {
            drawPinLine(canvas, i);
        }

        if (DBG) {
            drawAnchorLine(canvas);
        }

        if (getText().length() > i) {
            if (isPasswordInputType(getInputType())) {
                drawCircle(canvas, i);
            } else {
                drawText(canvas, i);
            }
        } else if (!TextUtils.isEmpty(getHint()) && getHint().length() == mPinItemCount) {
            drawHint(canvas, i);
        }
    }

    // highlight the next item
    if (isFocused() && getText().length() != mPinItemCount && mViewType == VIEW_TYPE_RECTANGLE) {
        int index = getText().length();
        updateItemRectF(index);
        updateCenterPoint();
        updatePinBoxPath(index);
        mPaint.setColor(getLineColorForState(HIGHLIGHT_STATES));
        drawPinBox(canvas, index);
    }
}
 
Example 17
Source File: RevealLinearLayout.java    From WeGit with Apache License 2.0 4 votes vote down vote up
@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    if (!mClipOutlines && child != mTarget)
        return super.drawChild(canvas, child, drawingTime);

    final int state = canvas.save();

    mRevealPath.reset();
    mRevealPath.addCircle(mCenterX, mCenterY, mRadius, Path.Direction.CW);

    canvas.clipPath(mRevealPath);

    boolean isInvalided = super.drawChild(canvas, child, drawingTime);

    canvas.restoreToCount(state);

    return isInvalided;
}
 
Example 18
Source File: pageView.java    From Jreader with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 绘制翻起页背面
 *
 * @param canvas
 * @param bitmap
 */
private void drawCurrentBackArea(Canvas canvas, Bitmap bitmap) {
    int i = (int) (mBezierStart1.x + mBezierControl1.x) / 2;
    float f1 = Math.abs(i - mBezierControl1.x);
    int i1 = (int) (mBezierStart2.y + mBezierControl2.y) / 2;
    float f2 = Math.abs(i1 - mBezierControl2.y);
    float f3 = Math.min(f1, f2);
    mPath1.reset();
    mPath1.moveTo(mBeziervertex2.x, mBeziervertex2.y);
    mPath1.lineTo(mBeziervertex1.x, mBeziervertex1.y);
    mPath1.lineTo(mBezierEnd1.x, mBezierEnd1.y);
    mPath1.lineTo(mTouch.x, mTouch.y);
    mPath1.lineTo(mBezierEnd2.x, mBezierEnd2.y);
    mPath1.close();
    GradientDrawable mFolderShadowDrawable;
    int left;
    int right;
    if (mIsRTandLB) {
        left = (int) (mBezierStart1.x - 1);
        right = (int) (mBezierStart1.x + f3 + 1);
        mFolderShadowDrawable = mFolderShadowDrawableLR;
    } else {
        left = (int) (mBezierStart1.x - f3 - 1);
        right = (int) (mBezierStart1.x + 1);
        mFolderShadowDrawable = mFolderShadowDrawableRL;
    }
    canvas.save();
    try {
        canvas.clipPath(mPath0);
        canvas.clipPath(mPath1, Region.Op.INTERSECT);
    } catch (Exception e) {
    }


    mPaint.setColorFilter(mColorMatrixFilter);

    float dis = (float) Math.hypot(mCornerX - mBezierControl1.x,
            mBezierControl2.y - mCornerY);
    float f8 = (mCornerX - mBezierControl1.x) / dis;
    float f9 = (mBezierControl2.y - mCornerY) / dis;
    mMatrixArray[0] = 1 - 2 * f9 * f9;
    mMatrixArray[1] = 2 * f8 * f9;
    mMatrixArray[3] = mMatrixArray[1];
    mMatrixArray[4] = 1 - 2 * f8 * f8;
    mMatrix.reset();
    mMatrix.setValues(mMatrixArray);
    mMatrix.preTranslate(-mBezierControl1.x, -mBezierControl1.y);
    mMatrix.postTranslate(mBezierControl1.x, mBezierControl1.y);
    canvas.drawBitmap(bitmap, mMatrix, mPaint);
    // canvas.drawBitmap(bitmap, mMatrix, null);
    mPaint.setColorFilter(null);
    canvas.rotate(mDegrees, mBezierStart1.x, mBezierStart1.y);
    mFolderShadowDrawable.setBounds(left, (int) mBezierStart1.y, right,
            (int) (mBezierStart1.y + mMaxLength));
    mFolderShadowDrawable.draw(canvas);
    canvas.restore();
}
 
Example 19
Source File: LobsterPicker.java    From Lobsterpicker with Apache License 2.0 4 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    canvas.translate(translationOffset, translationOffset);

    int nbOfSegments = adapter.size();
    int segmentWidth = 360 / nbOfSegments;
    for (int i = 0; i < nbOfSegments; i++) {
        wheelPaint.setColor(adapter.color(i, shadePosition));

        //Add an extra degree to the angle so the arc doesn't have a wedge in between
        //the start should sweep one back and the last one should not have an extra degree
        canvas.drawArc(
                wheelRectangle,
                (segmentWidth * i - 90) - (i == 0 ? 1 : 0),
                segmentWidth + (i < nbOfSegments - 1 ? 1 : 0),
                false, wheelPaint
        );
    }

    if (colorHistoryEnabled) {
        historyPaint.setColor(historicColor);
        canvas.drawArc(historyRectangle, -90, 180, true, historyPaint);

        historyPaint.setColor(chainedColor);
        canvas.drawArc(historyRectangle, 90, 180, true, historyPaint);
    }

    canvas.save();

    canvas.clipPath(largeRadiusPath);
    canvas.clipPath(smallRadiusPath, Region.Op.DIFFERENCE);

    canvas.drawBitmap(pointerShadow,
            pointerPosition.x - (pointerShadow.getWidth() / 2),
            pointerPosition.y - (pointerShadow.getHeight() / 2), null);

    canvas.restore();

    canvas.drawCircle(
            pointerPosition.x, pointerPosition.y,
            pointerRadius, pointerPaint
    );

}
 
Example 20
Source File: RevealViewGroup.java    From PersistentSearchView with Apache License 2.0 4 votes vote down vote up
@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    if(mRunning && child == mRevealInfo.getTarget()){
        final int state = canvas.save();

        mRevealPath.reset();
        mRevealPath.addCircle(mRevealInfo.centerX, mRevealInfo.centerY, mRadius, Path.Direction.CW);

        canvas.clipPath(mRevealPath);

        boolean isInvalided = super.drawChild(canvas, child, drawingTime);

        canvas.restoreToCount(state);

        return isInvalided;
    }

    return super.drawChild(canvas, child, drawingTime);
}