Skip to content

Instantly share code, notes, and snippets.

@grigor-aramyan
Created August 25, 2017 13:34
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 grigor-aramyan/d3eb0abaeaa32fe88c940f6a481fe68a to your computer and use it in GitHub Desktop.
Save grigor-aramyan/d3eb0abaeaa32fe88c940f6a481fe68a to your computer and use it in GitHub Desktop.
private Bitmap drawOnImage() {
int inlineMargins = 20, initialY = 20, linesDrawn = 0;
// canvas points 1/72 of an inch
Bitmap bitmap = getClearBitmap();
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
Paint paintForComments = new Paint();
paint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
paintForComments.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.ITALIC));
paintForComments.setTextSize(14);
paint.setTextSize(17);
Bitmap logo = BitmapFactory.decodeResource(getResources(), R.drawable.logo);
Bitmap logoScaled = Bitmap.createScaledBitmap(logo, 124, 84, false);
canvas.drawBitmap(logoScaled, 80, initialY + inlineMargins * linesDrawn, paint);
linesDrawn += 6;
canvas.drawText("EATFULLY", 110, initialY + inlineMargins * linesDrawn, paint);
linesDrawn++;
paint.setTextSize(13);
canvas.drawText("Marmaris Grill", 105, initialY + inlineMargins * linesDrawn, paint);
linesDrawn++;
canvas.drawText("tel. " + mActiveOrder.getTelephone(), 101, initialY + inlineMargins * linesDrawn, paint);
linesDrawn++;
canvas.drawLine(15.0f, initialY + inlineMargins * linesDrawn, bitmap.getWidth() - 15.0f, initialY + inlineMargins * linesDrawn, paint);
linesDrawn++;
paint.setTextSize(16);
if (mIsCollection) {
canvas.drawText("Collection", 119, initialY + inlineMargins * linesDrawn, paint);
linesDrawn++;
} else {
canvas.drawText("Delivery", 119, initialY + inlineMargins * linesDrawn, paint);
linesDrawn++;
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM HH:mm");
canvas.drawText("Due " + formatter.format(new Date()), 95, initialY + inlineMargins * linesDrawn, paint);
linesDrawn++;
}
paint.setTextSize(14);
canvas.drawText("Status: " + ActiveOrdersListAdapter.getStatusLabel(mActiveOrder.getStatus()).toUpperCase(),
95, initialY + inlineMargins * linesDrawn, paint);
linesDrawn++;
paint.setTextSize(16);
canvas.drawText("Order number: " + mCurrentOrderID, 95, initialY + inlineMargins * linesDrawn, paint);
linesDrawn++;
canvas.drawLine(15.0f, initialY + inlineMargins * linesDrawn, bitmap.getWidth() - 15.0f, initialY + inlineMargins * linesDrawn, paint);
linesDrawn++;
paint.setTextSize(15);
canvas.drawText("GBP", bitmap.getWidth() - 35.0f, initialY + inlineMargins * linesDrawn, paint);
linesDrawn++;
int betweenLineMargin = 15, lineCount = 0;
float yCoord = 120.0f, totalDue = 0.0f;
for (String s: mData) {
// encoded scheme name---qty---price
String[] details = s.split("---");
String nameNQnt = details[1] + " X " + details[0];
String comment = "";
if (mComments.containsKey(details[0]))
comment = mComments.get(details[0]);
if (nameNQnt.length() > 30)
nameNQnt = nameNQnt.substring(0, 29) + "...";
canvas.drawText(nameNQnt, 5, initialY + inlineMargins * linesDrawn, paint);
canvas.drawText(DigitsAfterComaFloat.digitsAfterComa(Float.parseFloat(details[2]) + "", 2) + "", bitmap.getWidth() - 39, initialY + inlineMargins * linesDrawn, paint);
if (!comment.isEmpty()) {
initialY += 12;
canvas.drawText(comment, 12, initialY + inlineMargins * linesDrawn, paintForComments);
}
totalDue += Float.parseFloat(details[2]);
linesDrawn++;
}
Log.e("xxx", "ycoord: " + yCoord);
canvas.drawText("Total Due", 5, initialY + inlineMargins * linesDrawn + 10, paint);
canvas.drawText("" + DigitsAfterComaFloat.digitsAfterComa(totalDue + "", 2), bitmap.getWidth() - 39, initialY + inlineMargins * linesDrawn + 10, paint);
Log.e("xxx", "ycoord: " + yCoord);
linesDrawn++;
canvas.drawLine(15.0f, initialY + inlineMargins * linesDrawn, bitmap.getWidth() - 15.0f, initialY + inlineMargins * linesDrawn, paint);
linesDrawn++;
paint.setTextSize(16);
if (mOrderPayed) {
canvas.drawText("MONEY RECEIVED", 90, initialY + inlineMargins * linesDrawn, paint);
} else {
canvas.drawText("ORDER NOT PAID", 90, initialY + inlineMargins * linesDrawn, paint);
}
linesDrawn++;
canvas.drawLine(15.0f, initialY + inlineMargins * linesDrawn, bitmap.getWidth() - 15.0f, initialY + inlineMargins * linesDrawn, paint);
linesDrawn++;
if (!mActiveOrder.getStreet().equals("default collection")) {
paint.setTextSize(13);
canvas.drawText("Customer details:", 5, initialY + inlineMargins * linesDrawn, paint);
linesDrawn++;
paint.setTextSize(14);
canvas.drawText(mActiveOrder.getFirstname(), 5, initialY + inlineMargins * linesDrawn, paint);
linesDrawn++;
}
if (!mActiveOrder.getStreet().contains("collection")) {
canvas.drawText(mActiveOrder.getStreet() + ", " + mActiveOrder.getCity(), 5, initialY + inlineMargins * linesDrawn, paint);
linesDrawn++;
}
paint.setTextSize(13);
canvas.drawText("Order placed at", 5, initialY + inlineMargins * linesDrawn, paint);
canvas.drawText(mActiveOrder.getDate(), bitmap.getWidth() - 125, initialY + inlineMargins * linesDrawn, paint);
linesDrawn++;
return bitmap;
}
public Bitmap getClearBitmap() {
int w = 94 * 3, h = 150 * 3;
Bitmap.Config conf = Bitmap.Config.ARGB_8888; // see other conf types
Bitmap bmp = Bitmap.createBitmap(w, h, conf);
return bmp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment