Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 22nd, 2012  |  syntax: None  |  size: 1.47 KB  |  hits: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. PorterDuff and Path
  2. android.graphics.Canvas.drawPath(Path path, Paint paint)
  3.        
  4. mPaintPath.setARGB(100, 100, 100, 100);// (100, 100, 100, 100)
  5.     mPaintPath.setStyle(Paint.Style.FILL_AND_STROKE);
  6.     mPaintPath.setAntiAlias(true);
  7.     mPath.moveTo(x0, y0));
  8.     mPath.lineTo(x1, y1);
  9.     mPath.lineTo(x2, y2);
  10.     mPath.lineTo(x3, y3);
  11.     mPath.lineTo(x0, y0);
  12.     mPath.close();
  13.     c.drawPath(mPath, mPaintPath);
  14.        
  15. // Create an offscreen buffer
  16. int layer = c.saveLayer(0, 0, width, height, null,
  17.         Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG);
  18.  
  19. // Setup a paint object for the path
  20. mPaintPath.setARGB(255, 255, 255, 255);
  21. mPaintPath.setStyle(Paint.Style.FILL_AND_STROKE);
  22. mPaintPath.setAntiAlias(true);
  23.  
  24. // Draw the path onto the offscreen buffer
  25. mPath.moveTo(x0, y0);
  26. mPath.lineTo(x1, y1);
  27. mPath.lineTo(x2, y2);
  28. mPath.lineTo(x3, y3);
  29. mPath.lineTo(x0, y0);
  30. mPath.close();
  31. c.drawPath(mPath, mPaintPath);
  32.  
  33. // Draw a bitmap on the offscreen buffer and use the path that's already
  34. // there as a mask
  35. mBitmapPaint.setXfermode(new PorterDuffXfermode(Mode.SRC_OUT));
  36. c.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
  37.  
  38. // Composit the offscreen buffer (a masked bitmap) to the canvas
  39. c.restoreToCount(layer);
  40.        
  41. // Setup a clip path
  42. mPath.moveTo(x0, y0);
  43. mPath.lineTo(x1, y1);
  44. mPath.lineTo(x2, y2);
  45. mPath.lineTo(x3, y3);
  46. mPath.lineTo(x0, y0);
  47. mPath.close();
  48. c.clipPath(mPath, Op.DIFFERENCE);
  49.  
  50. // Draw the bitmap using the path clip
  51. c.drawBitmap(mBitmap, 0, 0, mBitmapPaint);