コード例 #1
0
  /**
   * Drawing src bitmap to dest bitmap with round mask. Dest might be squared, src is recommended to
   * be square
   *
   * @param src source bitmap
   * @param dest destination bitmap
   * @param clearColor clear color
   */
  public static void drawInRound(Bitmap src, Bitmap dest, int clearColor) {
    if (dest.getWidth() != dest.getHeight()) {
      throw new RuntimeException("dest Bitmap must have square size");
    }
    clearBitmap(dest, clearColor);
    Canvas canvas = new Canvas(dest);

    int r = dest.getWidth() / 2;
    Rect sourceRect = WorkCache.RECT1.get();
    Rect destRect = WorkCache.RECT2.get();
    sourceRect.set(0, 0, src.getWidth(), src.getHeight());
    destRect.set(0, 0, dest.getWidth(), dest.getHeight());

    Paint paint = WorkCache.PAINT.get();
    paint.reset();
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.RED);
    paint.setAntiAlias(true);
    canvas.drawCircle(r, r, r, paint);

    paint.reset();
    paint.setFilterBitmap(true);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(src, sourceRect, destRect, paint);

    canvas.setBitmap(null);
  }
コード例 #2
0
  /**
   * Drawing src bitmap to dest bitmap with rounded corners
   *
   * @param src source bitmap
   * @param dest destination bitmap
   * @param radius radius in destination bitmap scale
   * @param clearColor clear color
   */
  public static void drawRoundedCorners(Bitmap src, Bitmap dest, int radius, int clearColor) {
    clearBitmap(dest, clearColor);
    Canvas canvas = new Canvas(dest);

    Rect sourceRect = WorkCache.RECT1.get();
    Rect destRect = WorkCache.RECT2.get();
    sourceRect.set(0, 0, src.getWidth(), src.getHeight());
    destRect.set(0, 0, dest.getWidth(), dest.getHeight());

    RectF roundRect = WorkCache.RECTF1.get();
    roundRect.set(0, 0, dest.getWidth(), dest.getHeight());

    Paint paint = WorkCache.PAINT.get();
    paint.reset();
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.RED);
    paint.setAntiAlias(true);
    canvas.drawRoundRect(roundRect, radius, radius, paint);

    paint.reset();
    paint.setFilterBitmap(true);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(src, sourceRect, destRect, paint);

    canvas.setBitmap(null);
  }