Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@dbof10
Created August 10, 2016 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbof10/53de82c95242893ae931eeb647e338b0 to your computer and use it in GitHub Desktop.
Save dbof10/53de82c95242893ae931eeb647e338b0 to your computer and use it in GitHub Desktop.
Vietnam flag
public class FlagPainter {
private static final int PENTAGON = 5;
private static final float ROTATE = -18F;
private Bitmap drawStar(int size) {
Path path;
Paint paint = new Paint();
Matrix matrix = new Matrix();
paint.setColor(Color.YELLOW);
paint.setAntiAlias(true);
Bitmap bitmap;
bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
float halfWidth = bitmap.getWidth() / 2.0F;
path = createStarBySyze(bitmap.getWidth(), PENTAGON);
matrix.postRotate(ROTATE, halfWidth * 1.3F, halfWidth * 1.05F);
path.transform(matrix);
canvas.drawPath(path, paint);
return bitmap;
}
public Bitmap drawFlag(int size) {
Bitmap bitmap;
bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.RGB_565);
int halfSize = size / 2;
Bitmap star = drawStar(halfSize);
Canvas canvas = new Canvas(bitmap);
int midPos = halfSize / 2;
canvas.drawColor(Color.RED);
canvas.drawBitmap(star, midPos, midPos, null);
return bitmap;
}
private Path createStarBySyze(float width, int steps) {
float halfWidth = width / 2.0F;
float bigRadius = halfWidth;
float radius = halfWidth / 2.5F;
float degreesPerStep = (float) Math.toRadians(360.0F / (float) steps);
float halfDegreesPerStep = degreesPerStep / 2.0F;
Path ret = new Path();
ret.setFillType(Path.FillType.EVEN_ODD);
float max = (float) (2.0F * Math.PI);
ret.moveTo(width, halfWidth);
for (double step = 0; step < max; step += degreesPerStep) {
ret.lineTo((float) (halfWidth + bigRadius * Math.cos(step)), (float) (halfWidth + bigRadius * Math.sin(step)));
ret.lineTo((float) (halfWidth + radius * Math.cos(step + halfDegreesPerStep)), (float) (halfWidth + radius * Math.sin(step + halfDegreesPerStep)));
}
ret.close();
return ret;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment