I try to write a text on my image:
private Bitmap drawText() {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.test);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(5);
canvas.drawText("Some Text here", 5, 5, paint);
Bitmap resBitmap = Bitmap.createBitmap(canvas.getWidth(),
canvas.getHeight(), Config.RGB_565);
canvas.setBitmap(resBitmap);
return resBitmap;
}
the result is a completely black image. What am I doing wrong ?
Disclaimer: I'm not an Android developer. I've never written code like this. It's just my interpretation of the documentation...
I suspect you actually want to set the bitmap to draw onto much earlier, and then draw the other bitmap into the canvas. So something like:
private Bitmap drawText() {
// Load the existing image to get some dimensions
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.test);
// Create a result bitmap and a canvas which draws onto it
Bitmap resBitmap = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.RGB_565);
Canvas canvas = new Canvas(resBitmap);
// Draw the existing image into the canvas
canvas.drawBitmap(bitmap, 0f, 0f, null);
// Draw text on top
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(5);
canvas.drawText("Some Text here", 5, 5, paint);
return resBitmap;
}
See more on this question at Stackoverflow