PorterDuff and Path

In my project, I have a bitmap that fills the entire screen. In this raster, I draw a path using

android.graphics.Canvas.drawPath(Path path, Paint paint) 

paint is set to stroke and fill the contents of the path. What I have achieved is to remove the part of the bit that crosses the path. I managed to get the same behavior as on the other bitmapp instead of the path, and using the rules of the porter. Is there a chance to do the same with the road?

  mPaintPath.setARGB(100, 100, 100, 100);// (100, 100, 100, 100) mPaintPath.setStyle(Paint.Style.FILL_AND_STROKE); mPaintPath.setAntiAlias(true); mPath.moveTo(x0, y0)); mPath.lineTo(x1, y1); mPath.lineTo(x2, y2); mPath.lineTo(x3, y3); mPath.lineTo(x0, y0); mPath.close(); c.drawPath(mPath, mPaintPath); 
+4
source share
1 answer

Of course, just draw the path to the buffer off-screen so you can use it as a mask when drawing a bitmap, something like this:

 // Create an offscreen buffer int layer = c.saveLayer(0, 0, width, height, null, Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG); // Setup a paint object for the path mPaintPath.setARGB(255, 255, 255, 255); mPaintPath.setStyle(Paint.Style.FILL_AND_STROKE); mPaintPath.setAntiAlias(true); // Draw the path onto the offscreen buffer mPath.moveTo(x0, y0); mPath.lineTo(x1, y1); mPath.lineTo(x2, y2); mPath.lineTo(x3, y3); mPath.lineTo(x0, y0); mPath.close(); c.drawPath(mPath, mPaintPath); // Draw a bitmap on the offscreen buffer and use the path that already // there as a mask mBitmapPaint.setXfermode(new PorterDuffXfermode(Mode.SRC_OUT)); c.drawBitmap(mBitmap, 0, 0, mBitmapPaint); // Composit the offscreen buffer (a masked bitmap) to the canvas c.restoreToCount(layer); 

If you can withstand aliasing, it will be easier for you: just adjust the clip path (note the use of Region.Op.DIFFERENCE , which eliminates clipping of the path of the path, rather than clipping everything outside the path):

 // Setup a clip path mPath.moveTo(x0, y0); mPath.lineTo(x1, y1); mPath.lineTo(x2, y2); mPath.lineTo(x3, y3); mPath.lineTo(x0, y0); mPath.close(); c.clipPath(mPath, Op.DIFFERENCE); // Draw the bitmap using the path clip c.drawBitmap(mBitmap, 0, 0, mBitmapPaint); 
+6
source

Source: https://habr.com/ru/post/1381096/


All Articles