Skip to content

Instantly share code, notes, and snippets.

@mancdevcarl
Last active December 20, 2015 21:59
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 mancdevcarl/6201438 to your computer and use it in GitHub Desktop.
Save mancdevcarl/6201438 to your computer and use it in GitHub Desktop.
Draw text in center of bitmap using canvas
class CreateShareImage extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... params) {
Bitmap bkgImage = BitmapFactory.decodeResource(
MainActivity.this.getResources(), R.drawable.stork);
Bitmap mutableBitmap = bkgImage.copy(Bitmap.Config.ARGB_8888, true);
Canvas c = new Canvas(mutableBitmap);
Paint p = new Paint();
p.setAntiAlias(true);
p.setTypeface(Typeface.createFromAsset(
MainActivity.this.getAssets(), "fonts/itckrist.ttf"));
p.setColor(Color.BLACK);
p.setTextSize(50);
p.setTextAlign(Align.CENTER);
c.drawText("Some Text", c.getWidth() / 2, c.getHeight() / 2, p);
String fileName = Environment.getExternalStorageDirectory()
+ "/babytest.png";
OutputStream stream = null;
try {
stream = new FileOutputStream(fileName);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
mutableBitmap.compress(CompressFormat.PNG, 80, stream);
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
return "error";
}
return "";
}
@Override
protected void onPostExecute(String result) {
}
@Override
protected void onProgressUpdate(Void... values) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment