我尝试在图像上写文字:
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;
}
结果是一个全黑的图像。我做错了什么?
最佳答案
免责声明:我不是 Android 开发人员。我从来没有写过这样的代码。这只是我对文档的解释...
我怀疑您实际上想要将位图设置为更早地绘制,然后将另一个位图绘制到 Canvas 中。所以类似:
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;
}
关于java - 在图像上写入文本时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22531929/