Skip to content

Commit

Permalink
add getCircularBitmap and getRoundedEdgeBitmap from the main project …
Browse files Browse the repository at this point in the history
…merge in 80014b7
  • Loading branch information
maxme committed Jul 16, 2014
1 parent d2a76e2 commit d68fb87
Showing 1 changed file with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.AsyncTask;
Expand Down Expand Up @@ -490,4 +497,58 @@ public static byte[] createThumbnailFromUri(Context context,

return stream.toByteArray();
}

public static Bitmap getCircularBitmap(final Bitmap bitmap) {
if (bitmap==null)
return null;

final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(output);
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);

paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(Color.RED);
canvas.drawOval(rectF, paint);

paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);

// outline
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(1f);
paint.setColor(Color.DKGRAY);
canvas.drawOval(rectF, paint);

return output;
}

public static Bitmap getRoundedEdgeBitmap(final Bitmap bitmap, int radius) {
if (bitmap == null) {
return null;
}

final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(output);
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);

paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(Color.RED);
canvas.drawRoundRect(rectF, radius, radius, paint);

paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);

paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(1f);
paint.setColor(Color.DKGRAY);
canvas.drawRoundRect(rectF, radius, radius, paint);

return output;
}
}

0 comments on commit d68fb87

Please sign in to comment.