Skip to content

Instantly share code, notes, and snippets.

@drcarter
Last active August 29, 2015 14:15
Show Gist options
  • Save drcarter/7693c870a0dcc3447af3 to your computer and use it in GitHub Desktop.
Save drcarter/7693c870a0dcc3447af3 to your computer and use it in GitHub Desktop.
hexagon bitmap image converter.
private Bitmap getHexagonImage(Bitmap toTransform) {
Bitmap output = Bitmap.createBitmap(toTransform.getWidth(),
toTransform.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
int radius = 0;
int width = toTransform.getWidth();
int height = toTransform.getHeight();
int centerX = width / 2;
int centerY = height / 2;
if (width >= height) {
radius = height / 2;
} else {
radius = width / 2;
}
Paint paint = new Paint();
final Rect rect = new Rect(0, 0, toTransform.getWidth(),
toTransform.getHeight());
Path path = new Path();
paint.setPathEffect(new CornerPathEffect(10)); //corner
for (int i = 0; i < 6; i++) {
int posX = (int) (centerX + radius * Math.cos(Math.toRadians(i * 60 - 30)));
int posY = (int) (centerY + radius * Math.sin(Math.toRadians(i * 60 - 30)));
if (i == 0) {
path.moveTo(posX, posY);
} else {
path.lineTo(posX, posY);
}
}
path.close();
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(Color.parseColor("#FFFFFF"));
canvas.drawPath(path, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(toTransform, rect, rect, paint);
return output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment