Code example for Canvas

Methods: clipPathclipRectdrawPathrestoresavetranslate

0
	public void draw(Canvas canvas) {
		if (mFillPath.isEmpty()) {
			return; 
		} 
		 
		canvas.drawPath(mFillPath, mFillPaint);
		 
		// no fill path == no indicator at all 
		if (mIndicatorFillPath.isEmpty()) {
			if (mStrokeWidth > 0) {
				canvas.drawPath(mStrokePath, mStrokePaint);
			} 
			 
			return; 
		} 
		 
		final float iP = mIndicatorPosition;
		final float iW = mIndicatorWidth;
		final float cR = mCornerRadius;
		final float sW = mStrokeWidth;
		final float iVSW = mIndicatorVerticalStrokeWidth;
		 
		float offset;
		final boolean isHorizontal = mIndicatorDirection == IndicatorDirection.TOP
				|| mIndicatorDirection == IndicatorDirection.BOTTOM;
		 
		// get the needed offset to move indicator by calculating it by given position 
		if (isHorizontal) {
			final float w = getBounds().width();
			offset = Math.min(Math.max(
					w * iP - iVSW - iW / 2f, Math.max(cR, sW)), w - cR - iVSW * 2 - iW);
		} else { 
			final float h = getBounds().height();
			offset = Math.min(Math.max(
					h * iP - iVSW - iW / 2f, Math.max(cR, sW)), h - cR - iVSW * 2 - iW);
		} 
		 
		// no need for complicated stroke clipping, because there's no stroke 
		// just draw the indicator without stroke and that's it 
		if (mStrokeWidth < 0) {
			canvas.save();
			canvas.translate(isHorizontal ? offset : 0, isHorizontal ? 0 : offset);
			canvas.drawPath(mIndicatorFillPath, mFillPaint);
			canvas.restore();
		} 
		 
		if (mStrokeWidth < 0) {
			 
		} else { 
			if (mIndicatorFillPath.isEmpty()) {
				canvas.drawPath(mStrokePath, mStrokePaint);
			} else { 
				 
				 
				canvas.translate(isHorizontal ? offset : 0, isHorizontal ? 0 : offset);
				canvas.clipPath(mIndicatorFillPath, Op.DIFFERENCE);
				canvas.translate(isHorizontal ? -offset : 0, isHorizontal ? 0 : -offset);
				canvas.drawPath(mStrokePath, mStrokePaint);
				canvas.clipRect(getBounds(), Op.REPLACE);
				canvas.translate(isHorizontal ? offset : 0, isHorizontal ? 0 : offset);
				canvas.drawPath(mIndicatorFillPath, mFillPaint);
				canvas.drawPath(mIndicatorStrokePath, mStrokePaint);
				 
				canvas.restore();
			} 
		} 
	} 
	 
	/** 
Connect your IDE to all the code out there  Get Codota for Java