Last active
August 29, 2015 14:15
-
-
Save drcarter/7693c870a0dcc3447af3 to your computer and use it in GitHub Desktop.
hexagon bitmap image converter.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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